#include "Controller.h" // constructor Controller :: Controller(){ // initialize first empty student and item cell to 0 firstEmptyStudentCell = 0; firstEmptyItemCell = 0; ifstream studentIn("students.data");// student data input file ifstream itemIn("items.data"); // item data input file assert(studentIn); //assert statement assert(itemIn); // assert statement LoadStudents(studentIn);// load student function LoadItems(itemIn); // load item fucntion studentIn.close(); // close student data input file itemIn.close(); // close item data input file } //////////////////////////////////////////////////////////////// // This function search first available array cell to add new student info // // Parameters: // StudentInfo newStudent: new student information // // Pre: no array cell searched // // Post: array cell found and student added // // Returns: true or false // // Called by: main // Calls: GetEmail() // bool Controller::AddStudent(StudentInfo newStudent){ // preset the foundIt to false bool foundIt = false; // search the entire student array for (int i = 0; i < ARRAYSIZE; i++) { // if you find a matching e-mail address if (myStudents[i].GetEmail() == newStudent.GetEmail()) { // remember that you found one that matched foundIt = true; } } // if you actually didn't found one if (!foundIt) { // then go ahead and add it, IF it's empty! // find the next empty cell for (int i = firstEmptyStudentCell; i < ARRAYSIZE; i++) { if (myStudents[i].GetEmail() == "" ) { myStudents[i] = newStudent; // set the new student info return true; } } } return false; // set that cell to false, so no new info can be added } //////////////////////////////////////////////////////////////// // This function outputs all the Items info under print item command // // Parameters: // ofstream& Out: ouput stream // // Pre: No items were printed // // Post: Items printed // // Returns: none // // Called by: main // Calls: GetItem(), GetType(), GetAuthor(), GetEmail(), GetPrice(), // GetAvailability(), GetEmail() // void Controller::PrintItems(ofstream& Out) { // going through all the cells in the array for ( int i = 0; i < ARRAYSIZE; i ++) { // if the email is not blank if (myItems[i].GetEmail() != "") { // if the item information is available, then output as formatted if (myItems[i].GetItem() != "") { Out <<"Item: "<< endl; Out << " " << myItems[i].GetItem() << endl; } // if the type information is available, then output as formatted if (myItems[i].GetType() != "") { Out <<"Media Type: " << endl; Out << " " << myItems[i].GetType() << endl; } // if the author information is available, then output as formatted if (myItems[i].GetAuthor() != "") { Out << "by: " << endl; Out << " " << myItems[i].GetAuthor() << endl; } // if the e-mail information is available, then output as formatted if (myItems[i].GetEmail() != "") { Out << "Owner: " << endl; Out << " " << myItems[i].GetEmail() << endl; } // if the price information is available, then output as formatted if (myItems[i].GetPrice() != "") { Out << "Price: " << endl; Out << " " << myItems[i].GetPrice() << endl; } // if the status information is available, then output as formatted if (myItems[i].GetAvailability() != "") { Out << "Availability: " << endl; Out << " " << myItems[i].GetAvailability() << endl; } Out << endl;// output an extra blank line as formatted } } } //////////////////////////////////////////////////////////////// // This function outputs all the Students info under print students command // // Parameters: // ofstream& Out: ouput stream // // Pre: No students information were printed // // Post: students information printed // // Returns: none // // Called by: main // Calls: GetEmail(), GetName(), GetPhoneNumber(), GetAddL1(), GetAddL2(), // GetCity(), GetState(), GetZipcode(), GetOther // void Controller::PrintStudents(ofstream& Out){ // going through all the cells in the array for (int i = 0; i < ARRAYSIZE; i++) { // if the e-mail is not blank if (myStudents[i].GetEmail() != "" ) { // if the student's name is available, then output as formatted if (myStudents[i].GetName() != "") { Out << "Name: " << endl; Out << " " << myStudents[i].GetName() << endl; } // if the student's phone number is available, then output as formatted if (myStudents[i].GetPhoneNumber() != "") { Out <<"Phone Number: " << endl; Out << " " << myStudents[i].GetPhoneNumber() << endl; } // if the student's e-mail is available, then output as formatted if (myStudents[i].GetEmail() != "") { Out << "Email: " << endl; Out << " " << myStudents[i].GetEmail() << endl; } // if the student's address info are available, then output address header if (myStudents[i].GetAddL1() != "" || myStudents[i].GetAddL2() != "" || myStudents[i].GetCity() != "" || myStudents[i].GetState() != "" || myStudents[i].GetZipcode() != "") { Out << "Address: " << endl; } // if the student's address line 1 is available, then output as formatted if (myStudents[i].GetAddL1() != "") { Out <<" " << myStudents[i].GetAddL1() << endl; } // if the student's address line 2 is available, then output as formatted if (myStudents[i].GetAddL2() != "") { Out << " " << myStudents[i].GetAddL2() << endl; } // if the student's city is available, then output as formatted if (myStudents[i].GetCity() != "") { Out << " " << myStudents[i].GetCity(); } // if the student's state is available, then output as formatted if (myStudents[i].GetState() != "") { if (myStudents[i].GetCity() != "") { Out << ", " << myStudents[i].GetState(); } else{ Out << " " << myStudents[i].GetState(); } } // if the student's zipcode is available, then output as formatted if (myStudents[i].GetZipcode() != "") { if (myStudents[i].GetCity() != "" && myStudents[i].GetState() =="") { Out << ", " << myStudents[i].GetZipcode(); } else{ Out << " " << myStudents[i].GetZipcode(); } } // if the student's other information is available, then output as formatted if (myStudents[i].GetOther() != "" ) { Out << endl << "Other: " << endl; Out << " "<< myStudents[i].GetOther(); } Out << endl << endl; // ouput the extra blank line as formmated } } } //////////////////////////////////////////////////////////////// // Outputs the student information and students item information by the command // print , according to the student e-mail address // // Parameters: // ofstream& Out: output stream // const string findEmail: find the Email if available // // Pre: no command e-mail address were validated, and no information printed // // Post: command e-mail address validated, information printed // // Returns: none // // Called by: main // Calls: GetItem(), GetType(), GetAuthor(), GetEmail(), GetPrice(), // GetAvailability(), GetEmail(), GetName(), GetPhoneNumber(), // GetAddL1(), GetAddL2(), GetCity(), GetState(), GetZipcode(), GetOther // void Controller::IndividualStatus(ofstream& Out, const string findEmail) { bool found = false;// preset the found to false // search the entire student array for (int i = 0; i < ARRAYSIZE; i++) { // if you find a matching e-mail address if (myStudents[i].GetEmail() == findEmail) { // Output the data // if the student's name is available, then output as formatted if (myStudents[i].GetName() != "") { Out <<"Name:" << endl; Out << " " << myStudents[i].GetName() << endl; } // if the student's phone number is available, then output as formatted if (myStudents[i].GetPhoneNumber() != "") { Out << "Phone Number:" << endl; Out << " " << myStudents[i].GetPhoneNumber() << endl; } // if the student's e-mail is available, then output as formatted if (myStudents[i].GetEmail() != "") { Out << "Email:" << endl; Out << " " << myStudents[i].GetEmail() << endl; } // if the student's address info are available, output the address header if (myStudents[i].GetAddL1() != "" || myStudents[i].GetAddL2() != "" || myStudents[i].GetCity() != "" || myStudents[i].GetState() != "" || myStudents[i].GetZipcode() != "") { Out <<"Address: " << endl; } // if the student's address line 1 is available, then output as formatted if (myStudents[i].GetAddL1() != "") { Out << " " << myStudents[i].GetAddL1() << endl; } // if the student's address line 2 is available, then output as formatted if (myStudents[i].GetAddL2() != "") { Out << " " << myStudents[i].GetAddL2() << endl; } // if the student's city is available, then output as formatted if (myStudents[i].GetCity() != "") { Out << " " << myStudents[i].GetCity(); } // if the student's state is available, then output as formatted if (myStudents[i].GetState() != "") { if (myStudents[i].GetCity() != "") { Out << ", " << myStudents[i].GetState(); } else{ Out << " " << myStudents[i].GetState(); } //Out << " " << myStudents[i].GetState(); } // if the student's zipcode is available, then output as formatted if (myStudents[i].GetZipcode() != "") { if (myStudents[i].GetCity() != "" && myStudents[i].GetState() == "") { Out << ", " << myStudents[i].GetZipcode(); } else{ Out << " " << myStudents[i].GetZipcode(); } //Out << " " << myStudents[i].GetZipcode(); } // if the student's other information is available, then output as formatted if (myStudents[i].GetOther() != "" ) { Out << endl << "Other: " << endl; Out << " "<< myStudents[i].GetOther(); } Out << endl << endl;// output the extra blank line as formatted // Set that we found it found = true; // Go ahead and exit the for loop i = ARRAYSIZE; } } // if the e-mail address was found, then search through the array for items // listed under the student if (found) { // search through the array for (i = 0; i < ARRAYSIZE; i ++) { // if the e-mail matches the student's email then output the items info if (myItems[i].GetEmail() == findEmail) { // if the item information is available, then output as formatted if (myItems[i].GetItem() != "") { Out << "Item: " << endl; Out << " " << myItems[i].GetItem() << endl; } // if the type information is available, then output as formatted if (myItems[i].GetType() != "") { Out <<"Media Type: " << endl; Out << " " << myItems[i].GetType() << endl; } // if the author information is available, then output as formatted if (myItems[i].GetAuthor() != "") { Out << "by: " << endl; Out << " " << myItems[i].GetAuthor() << endl; } // if the price information is available, then output as formatted if (myItems[i].GetPrice() != "") { Out <<"Price: " << endl; Out << " " << myItems[i].GetPrice() << endl; } // if the status information is available, then output as formatted if (myItems[i].GetAvailability() != "") { Out <<"Availability: "<< endl; Out << " " << myItems[i].GetAvailability() << endl; } Out << endl; // output a blank line as formatted } } } else { // else display the e-mail address is not found Out << findEmail << " not found." << endl; } } //////////////////////////////////////////////////////////////// // Validate the e-mail address to be deleted // // Parameters: // const string delAddy: address to be deleted // // Pre: no e-mail address were validated // // Post: e-mail address validated // // Returns: foundIt // // Called by: main // Calls: GetEmail() // bool Controller::Delete(const string delAddy) { // preset foundit to false bool foundIt = false; //initialize deletecell to -1 int deleteCell = -1; // search the entire student array for (int i = 0; i < ARRAYSIZE; i++) { // if you find a matching e-mail address if (myStudents[i].GetEmail() == delAddy) { // remember which cell deleteCell = i; // set foundit to true foundIt = true; } } // if the deleteCell is larger or equal to zero if (deleteCell >= 0) { // Delete that cell. StudentInfo nothing; // set the deleteCell to nothing myStudents[deleteCell] = nothing; // if the deleteCell is less than the first empty student cell if (deleteCell < firstEmptyStudentCell) { // set the first empty student cell to delete cell firstEmptyStudentCell = deleteCell; } } else { // else return foundIt return foundIt; } ItemInfo noItem;// initialize itemInfo noItem // now go delete all the items under that e-mail address for (i = 0; i < ARRAYSIZE; i++) { // if you find a matching e-mail address if (myItems[i].GetEmail() == delAddy) { // remember which cell myItems[i] = noItem; // if the firstempty cell is larger than i, then set it to i if (i < firstEmptyItemCell) { firstEmptyItemCell = i; } } } return foundIt; // return found it } //////////////////////////////////////////////////////////////// // Valid e-mail address and search first availabe cell to add the item // // Parameters: // ItemInfo newItem: new Item to be added // // Pre: no e-mail address were validated, no available cell were searched // // Post: e-mail address validated and first available cell were searched // // Returns: true or false // // Called by: main // Calls: GetEmail() // bool Controller::AddItem(ItemInfo newItem) { // Don't forget to make sure that cell is actually empty! // (first empty cell doesn't necessarily point to an empty cell) bool foundIt = false; // preset foundit to false // search the entire student array for (int i = 0; i < ARRAYSIZE; i++) { // if you find a matching e-mail address if (myStudents[i].GetEmail() == newItem.GetEmail()) { // remember that you found one that matched foundIt = true; // set found it to true } } // if you actually found one if (foundIt) { // then go ahead and add it, IF it's empty! // find the next empty cell for (int i = firstEmptyItemCell; i < ARRAYSIZE; i++) { // if the is not blank, then set the new item to that array if (myItems[i].GetEmail() == "" ) { myItems[i] = newItem; return true; // return true } } } return false; // return false } //////////////////////////////////////////////////////////////// // Load Items informaion from the Item data // // Parameters: // ifstream& itemIn: item input file // // Pre: no items were loaded // // Post: items loaded // // Returns: none // // Called by: main // Calls: SetItem(), SetType(), SetAuthor(), SetEmail(), SetPrice(), // SetAvailability() // void Controller::LoadItems(ifstream& itemIn) { // initialize string variables string infoType; string info; while (itemIn){ // get the info and info type getline(itemIn, infoType, '\n'); getline(itemIn, info, '\n'); // if infotype is @Item, read in item info if (infoType == "@Item:") { myItems[firstEmptyItemCell].SetItem(info); } // if infotype is media type, then read in type info else if (infoType == "@Media Type:") { myItems[firstEmptyItemCell].SetType(info); } // if infotype is by, then read in author info else if (infoType == "@by:") { myItems[firstEmptyItemCell].SetAuthor(info); } // if infotype is owner, then read in owner's e-mail else if (infoType == "@Owner:") { myItems[firstEmptyItemCell].SetEmail(info); } // if infotype is price, make sure all characters in price are valid else if (infoType == "@Price:") { bool valid = true;// preset valid to true unsigned int i; // info.length is unsigned // making sure all the digits in the phone number are valid for (i = 0; i < info.length(); i++) { // include spaces, numbers and ( ) s, in ascii code, obtined from online if ((int(info[i]) == 32) || (int (info[i])==36) || (int (info[i]) == 46) || ((int(info[i]) <= 57) && (int(info[i]) >= 40)) ) { // do nothing. } else { valid = false; // else valid is false } } // if valid, then read in price if (valid) { myItems[firstEmptyItemCell].SetPrice(info); } } // if infotype is availability, then read in the status of the item else if (infoType == "@Availability:") { myItems[firstEmptyItemCell].SetAvailability(info); } // if infotype is @@, then stop reading in else if (infoType == "@@") { // increment first empy cell firstEmptyItemCell++; } } } //////////////////////////////////////////////////////////////// // Load Students informaion from the Students data // // Parameters: // ifstream& studentIn: student input file // // Pre: no students were loaded // // Post: students loaded // // Returns: none // // Called by: main // Calls: SetName(), SetPhoneNumber(), SetEmail(), SetAddL1(), SetAddL2 // SetCity(), SetState(), SetZipcode(), SetOther() // void Controller::LoadStudents (ifstream& studentIn){ // initialize string variables string infoType; string info; // while student input file is valid while (studentIn){ // get the info and infotype getline(studentIn, infoType, '\n'); getline(studentIn, info, '\n'); // if infotype is name, then read in student's name if (infoType == "@Name:") { myStudents[firstEmptyStudentCell].SetName(info); } // if infotype is phoen number, then read in student's phone number else if (infoType == "@Phone Number:") { bool valid = true; // preset valid to true unsigned int i; // info.length is unsigned // making sure all the digits in the phone number are valid for (i = 0; i < info.length(); i++) { // include spaces, numbers and ( ) s, by ascii code obtained online if ((int(info[i]) == 32) || ((int(info[i]) <= 57) && (int(info[i]) >= 40)) ) { // do nothing. } else { valid = false; // valid set to false } } // if valid, then read in phone number if (valid) { myStudents[firstEmptyStudentCell].SetPhoneNumber(info); } } // if infotype is e-mail, then read in email address else if (infoType == "@E-mail:") { myStudents[firstEmptyStudentCell].SetEmail(info); } // if info type is address line 1, then read in address line1 else if (infoType == "@Address Line 1:") { myStudents[firstEmptyStudentCell].SetAddL1(info); } // if infotype is address line 2, then read in address line2 else if (infoType == "@Address Line 2:") { myStudents[firstEmptyStudentCell].SetAddL2(info); } // if infotype is city, then read in city else if (infoType == "@City:") { myStudents[firstEmptyStudentCell].SetCity(info); } // if infotype is state, then read in state else if (infoType == "@State:") { myStudents[firstEmptyStudentCell].SetState(info); } // if infotype is zipcode, then read in zipcode, else if (infoType == "@Zipcode:") { // make sure all zipcode digits are valid bool valid = true; unsigned int i; // info.length is unsigned // similar as phone number above for (i = 0; i < info.length(); i++) { if (int(info[i]) <= 57 && int(info[i]) >= 48) { // we're ok so do nothing } else { valid = false; // set valid to false } } // if zipcode is valid, then read in the zipcode if(valid) { myStudents[firstEmptyStudentCell].SetZipcode(info); } } // if infotype is other, then read in other information else if (infoType == "@Other:") { myStudents[firstEmptyStudentCell].SetOther(info); } // if infotype is @@, then stop reading in else if (infoType == "@@") { // increment the first empty cell firstEmptyStudentCell++; } } }