/*

  p231c final project: Periodic Table (spring, 1998): plaint text version
  Zhao, Hong
 
      Please type the following command to compile the code on the sun
  stations in the 1st floor computer lab of FRH:

        g++ search.c PeriodicTable.c -o search

  then type "search" to run it.
        
      Four kinds of constructors are used in this program. They are based
  on three ways that can used to search the element in the periodic table.
  by atomic number, by abbrevation, by row number & column number, and 
  main_group_element constructor.

      Please follow the instruction to run it.

*/

#include "PeriodicTable.h"
#include <iostream.h>
#include <fstream.h>




void printTitle();
void search(int);
bool isInMainGroups(int, int);
void searchByAtomNum(int);
void searchByName(char*);
void searchByRCNum(int, int);
const TablePointer tp;


void main()
{
  int i, j, searchMethod;
  char cq;
  
  printTitle();  // print title.
  do
    {
      // give options
      cout << "\n\n      *****************************************************************";
      cout << "\n      *****************************************************************";
      cout << "\nDo you like to find an Element by: ";
      cout << "\n 1. its atomic number(Please enter 1.), ";
      cout << "\n 2. its abbreviation(Please enter 2.), ";
      cout << "\n 3. its position coordinates(row number, column number)(Please enter 3.)?";
      cout << "\nYour choice: ";
      cin >> searchMethod;
      cout << "\n";

      // search the element according to the input.
      search(searchMethod);
      cout << "\nContinue(c) or Quit(q)? ";
      cin >> cq;
      if(cq!='c' && cq!='C' && cq!='q' && cq!='Q')
	cout << "\n !!!!!!!   Invalid input, quit.   !!!!!!!\n\n\n";
    } while(cq=='c' || cq=='C');
  cout << "\n\n      *********************  Thank you.  Bye!  **************************\n\n\n";
}






void printTitle()
{
  cout << "\n\n";
  cout << "\n             ++++++++++++++++++++++++++++++++++++++++++++++++++++";
  cout << "\n             +                                                  +";
  cout << "\n             +      P231C FINAL PROJECT ( SPRING, 1998 )        +";
  cout << "\n             +                                                  +";
  cout << "\n             +               PERIOCIC TABLE                     +";
  cout << "\n             +                                                  +";
  cout << "\n             +                                                  +";
  cout << "\n             +                 ZHAO, HONG                       +";
  cout << "\n             +                                                  +";
  cout << "\n             ++++++++++++++++++++++++++++++++++++++++++++++++++++";
  cout << "\n\n";
}




void search(int searchMethod)
{
  int i, atomicNum, rowNum, colNum;
  char *name = new char[2];

  if(searchMethod!=1 && searchMethod!=2 && searchMethod!=3)
    cout << "\n !!!!!!!!   Invalid input.   !!!!!!!!\n";
  else
    {
      switch(searchMethod)
	{
	case 1: // search by atomic number.
	  cout<< "\nThen please enter its atomic number: ";
	  cin >> atomicNum;
	  searchByAtomNum(atomicNum);
	  break;
	case 2: // search by abbrevation.
	  cout << "\nPlease enter its abbreviation(1 or 2 letters, first is upper case, second is lower case): ";
	  cin.getline(name, 3);
	  cin.getline(name, 3);
	  cout << "\nWell, the element you are interested in is: " << name[0] << name[1] << ".\n";
	  if(name[1]=='\0') name[1] = ' ';
	  searchByName(name);
	  break;
	case 3: // search by row number & column number.
	  cout << "\nThen please enter its position coordinates(row number, column number): ";
	  cin >> rowNum >> colNum;
	  searchByRCNum(rowNum, colNum);
	  break;
	}
    }  
}


// Tell if the element is in main group.
bool isInMainGroups(int rowNumber, int columnNumber)
{
  if(columnNumber<=2 || (columnNumber>=13 && rowNumber<=6)) return true;
  else return false;
}





void searchByAtomNum(int atomicNum)
{
  int rn = tp.getPRowNumber()[atomicNum-1];
  int cn = tp.getPColumnNumber()[atomicNum-1];
  if(isInMainGroups(rn, cn))
    {
      ElemInMainGroups elemMain(atomicNum, tp);
      elemMain.printAllInfo();
    }
  else
    {
      Element element(atomicNum, tp);
      element.printInfo();
    }  
}



void searchByName(char* nm)
{
  Element element(nm, tp);
  int rn = element.getRowNumber();
  int cn = element.getColumnNumber();
  if(isInMainGroups(rn, cn))
    {
      ElemInMainGroups elemMain(nm, tp);
      elemMain.printAllInfo();
    }
  else
    {
      element.printInfo();
    }
}



void searchByRCNum(int rowNum, int colNum)
{
  if(isInMainGroups(rowNum, colNum))
    {
      ElemInMainGroups elemMain(rowNum, colNum, tp);
      elemMain.printAllInfo();
    }
  else
    {
      Element element(rowNum, colNum, tp);
      element.printInfo();
    }
}









