#include "mlist.h" //default constructor mlist::mlist() { //Open the data files... ifstream stfile("students.data"); ifstream itfile("items.data"); if((stfile)&&(itfile)) //...if possible init(stfile,itfile); } //if given arguments mlist::mlist(ifstream &stfile,ifstream &itfile) { init(stfile,itfile); } //Array initialization function void mlist::init(ifstream &stfile,ifstream &itfile) { string tempc; //temp variable, hold commands. for(ui i=0;i<100;i++) { string sName,sNum,sEmail,sA1,sA2,sCity,sState,sZip,sOther; //fields for student class do{ //loop to read all of entry for student getline(stfile,tempc); //read command if(tempc[0]=='@'&&tempc[1]!='@') //if command, but not end of listing, check type, respond appropriately { if(tempc[1]=='N') //@Name: commcatch(stfile,sName); if(tempc[1]=='P') //@Phone Number: commcatch(stfile,sNum); if(tempc[1]=='E') //@E-mail: commcatch(stfile,sEmail); if(tempc[1]=='A'&&tempc.length()>14) { if(tempc[14]=='1') //@Address Line 1: commcatch(stfile,sA1); if(tempc[14]=='2') //@Address Line 2: commcatch(stfile,sA2); } if(tempc[1]=='C') //@City: commcatch(stfile,sCity); if(tempc[1]=='S') //@State: commcatch(stfile,sState); if(tempc[1]=='Z') //@Zipcode: commcatch(stfile,sZip); if(tempc[1]=='O') //@Other: commcatch(stfile,sOther); } }while (!(tempc[1]=='@'&&tempc[0]=='@')&&stfile); if(stfile) { if(sEmail.length()>0) { //cout<<"sNum["<='0'&&tiPrice[x]<='9') stiPrice+=tiPrice[x]; //converts string of numbers to ui; istringstream sPrice(stiPrice); sPrice>>iPrice; //If file still valid if(itfile) { if(iOwn.length()>0) //if an email was given medList[i].setitem(iTitle,iType,iAuth,iOwn,iPrice,iAvail); else //Else, don't add to list i--; } } } //Prints list of items. ui mlist::iPrint(ofstream &ofile) { for(ui i=0;i<100;i++) medList[i].print(1,ofile); return 1; } //Prints a student w/ given email, followed by items owned by said student. ui mlist::ePrint(string targE,ofstream &ofile) { bool fStudent=false; ui gPrint=0; for(ui i=0;i<100;i++) if(targE==sList[i].eDisp()) //the comparison { fStudent=true; //if student is found.... sList[i].print(ofile); break; } if(fStudent) //...then proceed to print owned items. { for(ui i=0;i<100;i++) if(targE==medList[i].eDisp()) //the other comparison { gPrint=medList[i].print(0,ofile); assert(gPrint==1); //if print doesn't work properly, serious issues. } return 1; } else return 0; } //Prints list of students. ui mlist::sPrint(ofstream &ofile) { for(ui i=0;i<100;i++) sList[i].print(ofile); return 1; } //Add an item to the list ui mlist::additem(string iName,string iType,string iAuth,string iOwn,ui iPrice,string iAvail) { int free=-1; //marks open spot. -1 if nothing int sExist=false; for(ui i=0;i<100;i++) if(sList[i].eDisp()==iOwn) { sExist=true; break; } if(sExist) { //Searches for Open spot for(ui i=0;i<100;i++) if(medList[i].eDisp()=="blank_at_vt_dot_edu") { free=i; break; } //if open spot found, add student there if(free>=0) medList[free].setitem(iName,iType,iAuth,iOwn,iPrice,iAvail); return 1; } else return 0; } //Add a student to the list ui mlist::addstudent(string sName,string sNum,string sEmail,string sA1,string sA2,string sCity,string sState,string sZip,string sOther) { int free=-1; //marks open spot. -1 if nothing //Searches for open spot for(ui i=0;i<100;i++) if(sList[i].eDisp()=="blank_at_vt_dot_edu") { free=i; break; } //if open spot found, add student there. if(free>=0) sList[i].setstudent(sName,sNum,sEmail,sA1,sA2,sCity,sState,sZip,sOther); return 1; } //delete a student and any items he/she/it had ui mlist::eDel(string email) { int ssucc=0; //Search students for matches. Eliminate. for(ui i=0;i<100;i++) if(sList[i].eDisp()==email) { ssucc=1; sList[i].delstudent(); } //Search items for matches. Eliminate. for(ui i=0;i<100;i++) if(medList[i].eDisp()==email) medList[i].delitem(); if(ssucc==1) return 1; else return 0; } //Catches the appropriate command from the line void mlist::commcatch(ifstream &file,string &line) { getline(file,line); assert(line[0]!='@'); //if catch a command w/ this read, whole program is stuck, 'cause I'm lazy and short on time. if(line[line.length()-1]=='\n') line[line.length()-1]=' '; //eliminates any pesky, unexpected endlines. }