#include "Controller.h" #include //////////////////////////////////////////////////////////////// // // // Parameters: // // Pre: None. // // Post: initializes the controller and opens the output // // Returns: none // // Called by: // Calls: // Controller::Controller() { Snum = 0; Mnum = 0; output.open("output.data"); } //////////////////////////////////////////////////////////////// // // // Parameters: // // Pre: None. // // Post: reads in all student data // // Returns: none // // Called by: main() // Calls: // void Controller::readSdata() { ifstream infile; // input varibale string data; // string which is read in and passed to the other functions string eolch; // dummy variable to get the white space string line; // dummy variable used for the address string num; // variable used in reading the address infile.open("students.data"); // opens the infile while(infile) { if(!infile.eof()) { infile >> data; if(data == "@E-mail:") { getline(infile, eolch, '\n'); getline(infile, data, '\n'); Sarray[Snum].setEmail(data); } if(data == "@Address") { infile >> line; infile >> num; if(num == "1:") { getline(infile, eolch, '\n'); getline(infile, data, '\n'); Sarray[Snum].setStreetAdd(data); } else { getline(infile, eolch, '\n'); getline(infile, data, '\n'); Sarray[Snum].setApt(data); } } if(data == "@City:") { getline(infile, eolch, '\n'); getline(infile, data, '\n'); Sarray[Snum].setCity(data); } if(data == "@State:") { getline(infile, eolch, '\n'); getline(infile, data, '\n'); Sarray[Snum].setState(data); } if(data == "@Phone") { infile >> line; getline(infile, eolch, '\n'); getline(infile, data, '\n'); Sarray[Snum].setPhone(data); } if(data == "@Zipcode:") { getline(infile, eolch, '\n'); getline(infile, data, '\n'); Sarray[Snum].setZip(data); } if(data == "@Other:") { getline(infile, eolch, '\n'); getline(infile, data, '\n'); Sarray[Snum].setOther(data); } if(data == "@Name:") { getline(infile, eolch, '\n'); getline(infile, data, '\n'); Sarray[Snum].setSname(data); } if(data == "@@") { getline(infile, eolch, '\n'); getline(infile, data, '\n'); Snum++; } } } infile.close(); cout << Snum; } //////////////////////////////////////////////////////////////// // // // Parameters: // // Pre: None. // // Post: reads in all item data // // Returns: none // // Called by: main() // Calls: // void Controller::readMdata() { ifstream infile; // input variable string data; // string that is read in and passed into the functions infile.open("items.data"); // opens the infile while(infile) { if(!infile.eof()) { getline(infile, data, '\n'); if(data == "@Item:") { getline(infile, data, '\n'); Marray[Mnum].setMname(data); } if(data == "@Media Type:") { getline(infile, data, '\n'); Marray[Mnum].setType(data); } if(data == "@by:") { getline(infile, data, '\n'); Marray[Mnum].setAuthor(data); } if(data == "@Owner:") { getline(infile, data, '\n'); Marray[Mnum].setOwner(data); } if(data == "@Price:") { getline(infile, data, '\n'); Marray[Mnum].setPrice(data); } if(data == "@Availability:") { getline(infile, data, '\n'); Marray[Mnum].setAvailability(data); } if(data == "@@") { getline(infile, data, '\n'); Mnum++; } } } infile.close(); } //////////////////////////////////////////////////////////////// // // // Parameters: // // Pre: None. // // Post: prints out all students in the array // // Returns: none // // Called by: main() // Calls: // void Controller::printStudents() { output << "print students" << endl; // prints the students for(int i = 0; i < Snum; i++) { if(!Sarray[i].getDel()) { if(Sarray[i].getSname() != "") { output << "Name:" << endl; output << Sarray[i].getSname() << endl; } if(Sarray[i].getPhone() != "") { output << "Phone Number:" << endl; output << Sarray[i].getPhone() << endl; } if(Sarray[i].getEmail() != "") { output << "Email:" << endl; output << Sarray[i].getEmail() << endl; } if( (Sarray[i].getStreetAdd() != "") || (Sarray[i].getApt() != "") || (Sarray[i].getCity() != "") || (Sarray[i].getState() != "") ) { output << "Address:" << endl; if(Sarray[i].getStreetAdd() != "") { output << Sarray[i].getStreetAdd() << endl; } if(Sarray[i].getApt() != "") { output << Sarray[i].getApt() << endl; } if( (Sarray[i].getCity() != "") && (Sarray[i].getState() != "") && (Sarray[i].getZip() != "") ) { output << Sarray[i].getCity() << ", "; output << Sarray[i].getState() << '\t' << Sarray[i].getZip() << endl; } else if( (Sarray[i].getCity() == "") && ((Sarray[i].getState() != "") && (Sarray[i].getZip() != "")) ) { output << Sarray[i].getState() << '\t' << Sarray[i].getZip() << endl; } else if( (Sarray[i].getCity() != "") && (Sarray[i].getState() == "") && (Sarray[i].getZip() != "") ) { output << Sarray[i].getCity() << ", " << Sarray[i].getZip() << endl; } else if((Sarray[i].getCity() == "") && (Sarray[i].getState() == "") && (Sarray[i].getZip() != "") ) { output << Sarray[i].getZip() << endl; } else if((Sarray[i].getCity() != "") && (Sarray[i].getState() == "") && (Sarray[i].getZip() == "") ) { output << Sarray[i].getCity() << endl; } else if((Sarray[i].getCity() == "") && (Sarray[i].getState() != "") && (Sarray[i].getZip() == "") ) { output << Sarray[i].getState() << endl; } else if((Sarray[i].getCity() != "") && (Sarray[i].getState() != "") &&(Sarray[i].getZip() == "") ) output << Sarray[i].getCity() << ", " << Sarray[i].getState() << endl; } if(Sarray[i].getOther() != "") { output << "Other:" << endl; output << Sarray[i].getOther() << endl; } output << endl; } } } //////////////////////////////////////////////////////////////// // // // Parameters: // // Pre: None. // // Post: prints out all items in the array // // Returns: none // // Called by: main() // Calls: // void Controller::printItems() { output << "print items" << endl; for(int i = 0; i < Mnum; i++) //prints the items { if(!Marray[i].getDel()) { if(Marray[i].getMname() != "") { output << "Item:" << endl; output << Marray[i].getMname() << endl; } if(Marray[i].getType() != "") { output << "Media Type:" << endl; output << Marray[i].getType() << endl; } if(Marray[i].getAuthor() != "") { output << "by:" << endl; output << Marray[i].getAuthor() << endl; } if(Marray[i].getOwner() != "") { output << "Owner:" << endl; output << Marray[i].getOwner() << endl; } if(Marray[i].getPrice() != "") { output << "Price:" << endl; output << Marray[i].getPrice() << endl; } if(Marray[i].getAvailability() != "") { output << "Availability:" << endl; output << Marray[i].getAvailability() << endl; } output << endl; } } } //////////////////////////////////////////////////////////////// // // // Parameters: // // Pre: None. // // Post: prints out one students info and all the items that they own // // Returns: none // // Called by: main() // Calls: // void Controller::printInfo(string email) { bool found = false; // if found, then true int i; // loop variable output << "print " << email << endl; if(!found) { if(!Sarray[Snum].getDel()) { for(i = 0; i < 100; i++) { if(Sarray[i].getEmail() == email) { found = true; break; } } } } if(found) { if(!Sarray[i].getDel()) { if(Sarray[i].getSname() != "") { output << "Name:" << endl; output << Sarray[i].getSname() << endl; } if(Sarray[i].getPhone() != "") { output << "Phone Number:" << endl; output << Sarray[i].getPhone() << endl; } if(Sarray[i].getEmail() != "") { output << "Email:" << endl; output << Sarray[i].getEmail() << endl; } if( (Sarray[i].getStreetAdd() != "") || (Sarray[i].getApt() != "") || (Sarray[i].getCity() != "") || (Sarray[i].getState() != "") ) { output << "Address:" << endl; if(Sarray[i].getStreetAdd() != "") { output << Sarray[i].getStreetAdd() << endl; } if(Sarray[i].getApt() != "") { output << Sarray[i].getApt() << endl; } if( (Sarray[i].getCity() != "") && (Sarray[i].getState() != "") && (Sarray[i].getZip() != "") ) { output << Sarray[i].getCity() << ", "; output << Sarray[i].getState() << '\t' << Sarray[i].getZip() << endl; } else if( (Sarray[i].getCity() == "") && ((Sarray[i].getState() != "") && (Sarray[i].getZip() != "")) ) { output << Sarray[i].getState() << '\t' << Sarray[i].getZip() << endl; } else if( (Sarray[i].getCity() != "") && (Sarray[i].getState() == "") && (Sarray[i].getZip() != "") ) { output << Sarray[i].getCity() << ", " << Sarray[i].getZip() << endl; } else if((Sarray[i].getCity() == "") && (Sarray[i].getState() == "") && (Sarray[i].getZip() != "") ) { output << Sarray[i].getZip() << endl; } else if((Sarray[i].getCity() != "") && (Sarray[i].getState() == "") && (Sarray[i].getZip() == "") ) { output << Sarray[i].getCity() << endl; } else if((Sarray[i].getCity() == "") && (Sarray[i].getState() != "") && (Sarray[i].getZip() == "") ) { output << Sarray[i].getState() << endl; } else if((Sarray[i].getCity() != "") && (Sarray[i].getState() != "") &&(Sarray[i].getZip() == "") ) output << Sarray[i].getCity() << ", " << Sarray[i].getState() << endl; } if(Sarray[i].getOther() != "") { output << "Other:" << endl; output << Sarray[i].getOther() << endl; } output << endl; for(int j = 0; j < 100; j++) { if(Marray[j].getOwner() == email) { if(Marray[j].getMname() != "") { output << "Item:" << endl; output << Marray[j].getMname() << endl; } if(Marray[j].getType() != "") { output << "Media Type:" << endl; output << Marray[j].getType() << endl; } if(Marray[j].getAuthor() != "") { output << "by:" << endl; output << Marray[j].getAuthor() << endl; } if(Marray[j].getPrice() != "") { output << "Price:" << endl; output << Marray[j].getPrice() << endl; } if(Marray[j].getAvailability() != "") { output << "Availability:" << endl; output << Marray[j].getAvailability() << endl; } output << endl; } } } } if(!found) output << email << " not found." << endl; } //////////////////////////////////////////////////////////////// // // // Parameters: // // Pre: None. // // Post: adds an item to a students collection // // Returns: none // // Called by: main() // Calls: // void Controller::addMItem(MultItem addItem) { output << "add item" << endl; bool found = false; // if found, then true int i; // loop variable if(!found) { for(i = 0; i < 100; i++) { if( (Sarray[i].getEmail() == addItem.getOwner()) && (!Sarray[i].getDel()) ) { found = true; break; } } } if(found) { for(i = 0; i < 100; i++) { if(Marray[i].getDel()) { Marray[i] = addItem; output << "Success" << endl; break; } if(Marray[i].getOwner() == "") { Marray[i] = addItem; output << "Success" << endl; break; } if(Marray[i] == addItem) { output << "Failure" << endl; break; } } } else output << "Failure" << endl; } //////////////////////////////////////////////////////////////// // // // Parameters: // // Pre: None. // // Post: adds an student to the array // // Returns: none // // Called by: main() // Calls: // void Controller::addStudent(Students addStud) { output << "add student" << endl; bool found = false; // if found, true int i; // loop variable for(i = 0; i < 100; i++) { if(Sarray[i].getDel()) { cout << addStud.getSname() << endl; cout << addStud.getCity() << endl; cout << addStud.getApt() << endl; Sarray[i] = addStud; output << "Success" << endl; found = true; break; } if(Sarray[i].getEmail() == "") { Sarray[i].deleteStudent(); Sarray[i] = addStud; output << "Success" << endl; found = true; break; } if(Sarray[i] == addStud) { output << "Failure" << endl; break; } } if( (!found) && (!(Sarray[i] == addStud)) ) output << "Failure" << endl; } //////////////////////////////////////////////////////////////// // // // Parameters: // // Pre: None. // // Post: flags a student for deletion and deletes all the items they own // // Returns: none // // Called by: main() // Calls: // void Controller::deleteAll(string email) { output << "delete " << email << endl; bool deleted = false; // if deleted, then true for(int i = 0; i < 100; i++) { if(!Sarray[i].getDel()) { if(Sarray[i].getEmail() == email) { Sarray[i].deleteStudent(); deleted = true; output << "Success" << endl; } } } for(int j = 0; j < 100; j++) { if(!Marray[j].getDel()) { if(Marray[j].getOwner() == email) { Marray[j].deleteItem(); } } } if(!deleted) output << "Failure" << endl; } //////////////////////////////////////////////////////////////// // // // Parameters: // // Pre: None. // // Post: quits program and closes output file // // Returns: none // // Called by: main() // Calls: // void Controller::quit() { output << "quit"; output.close(); } // DESTRUCTOR! Controller::~Controller() { }