/******************************************************************** * KYU BAE * PROJECT #2 < Cdinventory.cpp > ********************************************************************* * Cd inventory definitons * member function are defined * *******************************************************************/ #include #include #include #include #include #include #include "slist.h" #include "Cdinventory.h" //global constants const int _MaxCopy=37; const int _STRG_SZ=4; /******************************************************************** * cdinventory class * set data members * return nothing ********************************************************************/ void Cdinventory::Setcategory(char categoryIn) { category=categoryIn; } void Cdinventory::Setcodenum(int codenumIn) { codenum=codenumIn; //strcpy(codenum, codenumIn); } void Cdinventory::Setcopynum(int copynumIn) { copynum = copynumIn; } void Cdinventory::Setartist(char* artistIn) { strcpy(artist, artistIn); } void Cdinventory::Settitle(char* titleIn) { strcpy(title, titleIn); } /******************************************************************** * Cdinventory Class * getting data members * return char, int ********************************************************************/ char Cdinventory::Getcategory(void) { return category; } int Cdinventory:: Getcodenum (void) { return codenum; } int Cdinventory::Getcopynum (void) { return copynum; } char* Cdinventory::Getartist(void) { return artist; } char* Cdinventory::Gettitle (void) { return title; } /******************************************************************** * Cdinventory & Cdlist constructors * initializing the datamembers to zeros * ********************************************************************/ Cdinventory::Cdinventory() { Setcategory ('0'); Setcodenum (000000); Setcopynum (000); Setartist ("000000"); Settitle ("000000"); } Cdlist::Cdlist() { _ListSize = 0; _MaxSize = _MaxCopy; _ListPtr = new Cdinventory [_MaxSize]; if (_ListPtr == NULL) { exit(1); } } /******************************************************************** * _Fileinput() * reads records from the file and check for the success for opening * return nothing ********************************************************************/ void Cdlist::_FileInput(char* fname) { _ListSize = 0; _MaxSize = _MaxCopy; _ListPtr = new Cdinventory[_MaxSize]; ifstream EntryFile(fname, ios::in, ios::nocreate); if (!EntryFile) { cout <<" File could not be opened!!!!\n"; cout <<"\n The program will end because you did n't provide data file names.\n"; cout <<" You must provide data file names at the command line!!!!\n\n"; exit (1); } else { //??ask dan for funny charactersin cout int i=0; char temp[81]; // char temp1[7]; EntryFile.getline(temp, 80, '\n'); while (!EntryFile.eof()) { _ListPtr[i].Setcategory(temp[0]); //cout << temp[0] << endl; EntryFile.getline(temp, 80, '\n'); _ListPtr[i].Setcodenum(atoi(temp)); //strncpy(temp1, temp,7); ??ask dan for funny charactersin cout //_ListPtr[i].Setcodenum(temp); //_ListPtr[i].Setcodenum(temp[7]='\0'); // cout << temp << endl; EntryFile.getline(temp, 80, '\n'); _ListPtr[i].Setcopynum(atoi(temp)); //_ListPtr[i].Setcopynum(temp); //cout << atoi(temp) << endl; EntryFile.getline(temp, 80, '\n'); _ListPtr[i].Setartist(temp); // cout << temp << endl; EntryFile.getline(temp, 80, '\n'); _ListPtr[i].Settitle(temp); // cout << temp << endl; EntryFile.getline(temp, 80, '\n'); i++; } _ListSize = i; EntryFile.close(); } } /******************************************************************** * operator overload>>() * enter info form the keyboard * returns pointer ********************************************************************/ istream& operator>> (istream& input, Cdinventory& ItemRef) { char buff[81]; input.ignore(); cout <<" Enter Category(P, A, or p, a, etc.) => "; input.getline(buff,81,'\n'); ItemRef.category= buff[0]; //if (!(ItemRef.category=='\0')) //{ cout <<" CodeNum => "; input.getline(buff, 81,'\n'); //strcpy(ItemRef.codenum, buff);// ? when strncpy charters were added after numbers ItemRef.codenum=atoi(buff); cout <<" Number of copies => "; input.getline(buff, 81, '\n'); ItemRef.copynum=atoi(buff); cout <<" Artist => "; input.getline(buff,80, '\n'); strncpy(ItemRef.artist, buff, 40); cout <<" Title => "; input.getline(buff, 81, '\n'); strncpy(ItemRef.title, buff, 40); //} // else // ItemRef.category = '0'; return input; } /******************************************************************** * operator overload<<() * print all info * returns pointer ********************************************************************/ ostream& operator<<(ostream& output, const Cdinventory& ItemRef) { output << setprecision(2) << setiosflags(ios::left) << setw(4) << ItemRef.category << setw(9) << ItemRef.codenum << setw(4) << ItemRef.copynum << setw(28) << ItemRef.artist << setw(20) << ItemRef.title << endl; return output; } /*int Cdinventory::Compare_category(char category_) { if (category_== category) return 1; else return 0; }*/ /******************************************************************** * _Printallrecord() * print all records info * returns nothing ********************************************************************/ void Cdlist::_PrintAllRecord(Cdlist& AllCd) { char junk; cout <<"********************\n"; cout <<"* CD INVENTORY *\n"; cout <<"********************\n"; cout <<"------------------------------------------------------------------------------\n"; cout.precision(2); cout.setf(ios::left); cout << setw(10) << "Category" << setw(12) << "CodeNum" << setw(10) << "NumCopy" << setw(16) << "Artist" << setw(11) << "Title" << endl; cout <<"------------------------------------------------------------------------------\n"; if(_ListSize) { for (int i=0; i < _ListSize; i++) { cout <<"#1: " << i+1 << endl; cout <<_ListPtr[i]; if (i==12) { cout <<"Hit Enter key to see next page =>"; junk =cin.get(); cout <<"------------------------------------------------------------------------------\n"; cout.precision(2); cout.setf(ios::left); cout << setw(10) << "Category" << setw(12) << "CodeNum" << setw(10) << "NumCopy" << setw(16) << "Artist" << setw(11) << "Title" << endl; cout <<"------------------------------------------------------------------------------\n"; } if (i==24) { cout <<"Hit Enter key to see next page =>"; junk =cin.get(); cout <<"------------------------------------------------------------------------------\n"; cout.precision(2); cout.setf(ios::left); cout << setw(10) << "Category" << setw(12) << "CodeNum" << setw(10) << "NumCopy" << setw(16) << "Artist" << setw(11) << "Title" << endl; cout <<"------------------------------------------------------------------------------\n"; } } } else { cout <<"List is empty\n"; } cout <<"Hit Enter key to see next page =>\n "; cout << endl; junk = cin.get(); } //These functions are not used in Proj2 /******************************************************************** * CDlist class menu function * menu for entering and displaying menu * returns integer ********************************************************************/ int Cdlist::_Menu() const { int i; char text_str[_STRG_SZ]; cout <<"\n\n\n Here is your menu ==> " << endl; cout <<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl; cout <<" 0 - Quit" << endl; cout <<" 1 - Enter New Data" << endl; if (_ListSize) { cout <<" 2 - Display All CD Inventory" << endl; } int _Valid_choice =0; do { cout <<"\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; cout <<"\nSelect Choice? "; cin.getline(text_str, _STRG_SZ); i = text_str[0] - '0'; if (_ListSize) { _Valid_choice = (i>=0) && (i<=2); } else { _Valid_choice = (i>=0) && (i<=1); } }while (!_Valid_choice); return i; } /******************************************************************** * menu() function * menu for entering the file name and displaying menu * returns integer ********************************************************************/ int Cdlist::_Menutwo() const { int i; char text_str[_STRG_SZ]; cout <<"\n\n\n Here is your menu ==> " << endl; cout <<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl; cout <<" 0 - Quit" << endl; cout <<" 1 - Read from the File" << endl; if (_ListSize) { cout <<" 2 - Display All CD Inventory" << endl; } int _Valid_choice =0; do { cout <<"\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; cout <<"\nSelect Choice? "; cin.getline(text_str, _STRG_SZ); i = text_str[0] - '0'; if (_ListSize) { _Valid_choice = (i>=0) && (i<=2); } else { _Valid_choice = (i>=0) && (i<=1); } }while (!_Valid_choice); return i; } /******************************************************************** * _Enter () function * call overload operator to enter info from the keyboard * returns nothing ********************************************************************/ void Cdlist::_Enter() { int i=0; char answer[_STRG_SZ]; do { cin >> _ListPtr[_ListSize]; //if (_ListPtr[_ListSize].Compare_category(NULL)) //{ _ListSize++; //} if(_ListSize <=_MaxSize) { cin.ignore(); cout <<"\nDO you want to enter more CD Info?\n"; cin.getline(answer, _STRG_SZ); } else { cout <<"The list is full.\n"; answer[0]='n'; } } while (answer[0] != 'n'); }