////////////////////////////////////////////////////////////////////// // Project 2 for CS 1704 Spring 2004 // // Programmer: Michael Tuozzo // OS: Windows XP Professional // System: Mobile Pentium 4, 1.2/2.2 GHz, 512 MB Memory // Compiler: Visual C++ 6.0, Service Pack 4 // Last modified: February 18, 2004 // // Class Name - mmcontroller // Description - This class is a multimedia organizer class. It // maintains a list of students and a list of items // supports insertion, deletion, and printing of info // stored in the lists. ////////////////////////////////////////////////////////////////////// #include "mmcontroller.h" ///////////////////////////////////////////////// // Function Name - mmcontroller // Description - default constructor // // Parameters: none // Pre: none // Post: object is initialized // Returns: none // // Calls: this->inputstudent(ifstream&) // this->insert_student(student) // this->inputitem(ifstream&) // this->insert_item(multimedia) // ///////////////////////////////////////////////// mmcontroller::mmcontroller(){ studentsin.open("students.data"); if( !studentsin.is_open() ) { cout << "Error! Student file not found!\n"; exit(1); } mediain.open("items.data"); assert( mediain.is_open() ); //make sure the input file opened output.open("output.data"); assert( output.is_open() ); //make sure the input file opened for(int i = 0; i < MAXBUF; i++){ studentlist[i].status = vacant; medialist[i].status = vacant; } while(studentsin.peek() != EOF){ //insert all students in file "students.data" student tempstudent; if( inputstudent(studentsin, tempstudent) ) insert_student(tempstudent, false); } while(mediain.peek() != EOF){ //insert all items in file "items.data" multimedia tempmedia; if( inputitem(mediain, tempmedia) ) insert_item(tempmedia, false); } studentsin.close(); mediain.close(); } ///////////////////////////////////////////////// // Function Name - mmcontroller(const mmcontroller&) // Description - copy constructor // // Parameters: object to copy by reference // Pre: none // Post: object is initialized // Returns: none // // Calls: none // ///////////////////////////////////////////////// mmcontroller::mmcontroller(const mmcontroller &tocopy){ for(int i = 0; i < MAXBUF; i++){ studentlist[i] = tocopy.studentlist[i]; studentlist[i].status = tocopy.studentlist[i].status; } for(int j = 0; j < MAXBUF; j++){ medialist[j].data = tocopy.medialist[j].data; medialist[j].status = tocopy.medialist[j].status; } } ///////////////////////////////////////////////// // Function Name - print(string) // Description - prints specified student and all associated items // // Parameters: string email (email of student) // Pre: none // Post: if found, student's info will have been printed // Returns: none // // Calls: student::getemail() // student::print(ofstream&, bool) // multimedia::getowner() // multimedia::print(ofstream&, bool) // ///////////////////////////////////////////////// void mmcontroller::print(string email){ bool success = false; output << "print " << email << endl; for(int i = 0; i < MAXBUF; i++){ if( studentlist[i].status == occupied && studentlist[i].data.getemail() == email ){ studentlist[i].data.print(output); success = true; break; } } for(int j = 0; j < MAXBUF; j++){ if( medialist[j].status == occupied && medialist[j].data.getowner() == email ) medialist[j].data.print(output, false); } if( !success ) output << email << " not found.\n"; } ///////////////////////////////////////////////// // Function Name - print_items() // Description - prints list of items // // Parameters: none // Pre: none // Post: list of items will have been printed // Returns: none // // Calls: multimedia::print(ofstream&, bool) // ///////////////////////////////////////////////// void mmcontroller::print_items(){ output << "print items\n"; for(int i = 0; i < MAXBUF; i++){ if( medialist[i].status == occupied ) medialist[i].data.print(output); } } ///////////////////////////////////////////////// // Function Name - print_students() // Description - prints list of students // // Parameters: none // Pre: none // Post: list of students will have been printed // Returns: none // // Calls: student::print(ofstream&, bool) // ///////////////////////////////////////////////// void mmcontroller::print_students(){ output << "print students\n"; for(int i = 0; i < MAXBUF; i++){ if( studentlist[i].status == occupied ) studentlist[i].data.print(output); } } ///////////////////////////////////////////////// // Function Name - insert_item(multimedia, bool) // Description - inserts a new item into the list // // Parameters: multimedia toinsert, bool print // Pre: none // Post: none // Returns: true if successful insert, false if not // // Calls: multimedia::getowner() // ///////////////////////////////////////////////// bool mmcontroller::insert_item(multimedia toinsert, bool print){ if( print == true ) output << "add item\n"; bool foundowner = false; for(int i = 0; i < MAXBUF; i++){ if( studentlist[i].status == occupied && studentlist[i].data.getemail() == toinsert.getowner() ){ foundowner = true; break; } } if(foundowner == false){ if( print == true ) output << "Failure\n"; return false; } for(i = 0; i < MAXBUF; i++){ if(medialist[i].status == vacant){ medialist[i].status = occupied; medialist[i].data = toinsert; if( print == true ) output << "Success\n"; return true; } } if( print == true ) output << "Failure\n"; return false; } ///////////////////////////////////////////////// // Function Name - insert_student(student, bool) // Description - inserts a new student into the list // // Parameters: student toinsert, bool print // Pre: none // Post: none // Returns: true if successful insert, false if not // // Calls: none // ///////////////////////////////////////////////// bool mmcontroller::insert_student(student toinsert, bool print){ if( print == true ) output << "add student\n"; for(int i = 0; i < MAXBUF; i++){ if(studentlist[i].status == vacant){ studentlist[i].status = occupied; studentlist[i].data = toinsert; if( print == true ) output << "Success\n"; return true; } } if( print == true ) output << "Failure\n"; return false; } ///////////////////////////////////////////////// // Function Name - delete_student(string) // Description - searches for specified student // if found, removes student from // student list and all associated // items from media list // // Parameters: string email // Pre: none // Post: none // Returns: true if successful delete, false if not // // Calls: student::getemail() // student::clear() // multimedia::getowner() // multimedia::clear() // ///////////////////////////////////////////////// bool mmcontroller::delete_student(string email){ bool success = false; output << "delete " << email << endl; for(int i = 0; i < MAXBUF; i++){ if((studentlist[i].status == occupied) && (studentlist[i].data.getemail() == email)){ studentlist[i].status = vacant; studentlist[i].data.clear(); success = true; } if(medialist[i].status == occupied && medialist[i].data.getowner() == email){ medialist[i].status = vacant; medialist[i].data.clear(); } } if(success){ output << "Success\n"; return true; } else{ output << "Failure\n"; return false; } } ///////////////////////////////////////////////// // Function Name - inputstudent(ifstream&) // Description - inputs formatted student info from // the given file stream // // Parameters: ifstream& ifile // Pre: none // Post: none // Returns: student object created by function // with all new info set // // Calls: student::setname(string) // student::setphnumber(string) // student::setemail(string) // student::setaddress1(string) // student::setaddress2(string) // student::setcity(string) // student::setstate(string) // student::setzipcode(string) // student::setother(string) // ///////////////////////////////////////////////// bool mmcontroller::inputstudent(ifstream &ifile, student &tempstudent){ char buffer[513], buffer2[513]; buffer[512] = buffer2[512] = '\0'; ifile.getline(buffer, 512); while(0 != strcmp(buffer, "@@")){ ifile.getline(buffer2, 512); if(0 == strcmp(buffer, "@Name:")) tempstudent.setname(buffer2); else if(0 == strcmp(buffer, "@Phone Number:")) tempstudent.setphnumber(buffer2); else if(0 == strcmp(buffer, "@E-mail:")) tempstudent.setemail(buffer2); else if(0 == strcmp(buffer, "@Address Line 1:")) tempstudent.setaddress1(buffer2); else if(0 == strcmp(buffer, "@Address Line 2:")) tempstudent.setaddress2(buffer2); else if(0 == strcmp(buffer, "@City:")) tempstudent.setcity(buffer2); else if(0 == strcmp(buffer, "@State:")) tempstudent.setstate(buffer2); else if(0 == strcmp(buffer, "@Zipcode:")){ string tempzip(buffer2); bool valid = true; if( tempzip.length() == 5 ){ for( int i = 0; i < 5; i++ ){ if( tempzip[i] > '9' || tempzip[i] < '0' ){ valid = false; break; } } } else valid = false; if( valid ) tempstudent.setzipcode(buffer2); else cout << "The zipcode " << buffer2 << " is invalid format!\n"; } else if(0 == strcmp(buffer, "@Other:")) tempstudent.setother(buffer2); else; //field not recoginzed\ ifile.getline(buffer, 512); } ifile.ignore(1024, '\n'); if( tempstudent.getemail().length() > 0 ) return true; output << "Failure\n"; return false; } ///////////////////////////////////////////////// // Function Name - inputitem(ifstream&) // Description - inputs formatted item info from // the given file stream // // Parameters: ifstream& ifile // Pre: none // Post: none // Returns: multimedia object created by function // with all new info set // // Calls: multimedia::setname(string) // multimedia::settype(string) // multimedia::setauthor(string) // multimedia::setowner(string) // multimedia::setprice(string) // multimedia::setstatus(string) // ///////////////////////////////////////////////// bool mmcontroller::inputitem(ifstream &ifile, multimedia &tempmedia){ char buffer[513], buffer2[513]; buffer[512] = buffer2[512] = '\0'; ifile.getline(buffer, 512); while(0 != strcmp(buffer, "@@")){ ifile.getline(buffer2, 512); if(0 == strcmp(buffer, "@Item:")) tempmedia.setname(buffer2); else if(0 == strcmp(buffer, "@Media Type:")) tempmedia.settype(buffer2); else if(0 == strcmp(buffer, "@by:")) tempmedia.setauthor(buffer2); else if(0 == strcmp(buffer, "@Owner:")) tempmedia.setowner(buffer2); else if(0 == strcmp(buffer, "@Price:")){ bool valid = false; int count = 0; while( buffer2[count] != '\0' ){ if( buffer2[count] == '.' ){ if( buffer2[count+1] != '\0' && buffer2[count+1] >= '0' && buffer2[count+1] <= '9' ){ if( buffer2[count+2] != '\0' && buffer2[count+2] >= '0' && buffer2[count+2] <= '9' ){ if( buffer2[count+3] == '\0' ){ valid = true; break; } else {cout << "check 4 failed!\n"; break;} } else {cout << "check 3 failed!\n"; break;} } else {cout << "check 2 failed!\n"; break;} } else if( (buffer2[count] > '9' || buffer2[count] < '0') && buffer2[count] != '$' ){ cout << "check 1 failed!\n"; break; } count++; } if( valid ) tempmedia.setprice(buffer2); else cout << "Price " << buffer2 << " is invalid format!\n"; } else if(0 == strcmp(buffer, "@Availability:")) tempmedia.setstatus(buffer2); else; //field not recoginzed ifile.getline(buffer, 512); } ifile.ignore(1024, '\n'); if( tempmedia.getowner().length() > 0 ) return true; output << "Failure\n"; return false; } ///////////////////////////////////////////////// // Function Name - ~mmcontroller // Description - destructor // // Parameters: none // Pre: none // Post: object is destroyed // Returns: none // // Calls: none ///////////////////////////////////////////////// mmcontroller::~mmcontroller(){ studentsin.close(); mediain.close(); output.flush(); output.close(); }