#include "Organizer.h" #include #include #include using namespace std; organizer::organizer() { studentFile.open("students.data"); //open the file that holds the student data itemFile.open("items.data"); //open the file that holds the multimedia item information } void organizer::fillStudents() { string firstHeader = ""; studentFile >> firstHeader; while(studentFile) { addStudent(studentFile, firstHeader); studentFile >> firstHeader; if(firstHeader == "") { studentFile >> firstHeader; } } firstHeader = ""; } void organizer::fillItems() { string firstHeader = ""; getline(itemFile, firstHeader, '\n'); while(itemFile) { addItem(itemFile, firstHeader); getline(itemFile, firstHeader, '\n'); if(firstHeader == "") { getline(itemFile, firstHeader, '\n'); } } firstHeader = ""; } int organizer::searchStudents(string email) //search the student array for a student, using their email { for (int i = 0; i < 100; i++) //go through each element of the array of students { if(students[i].getEmail() == email) // if the student is found { return i; //return that the student was found and it's position in the array and exit the loop } } return -1; // if the student is found nowhere in the array, return false } int organizer::searchItems(int startingPoint, string email) // search the array of items using the email of its owner { for (int i = startingPoint; i < 100; i++) //go through each element of the array of multimedia items { if(multimedia[i].getOwner() == email) // if the item is found { return i; // return that the item was found and it's position in the array and exit the loop } } return -1; // if the multimedia item is not found return false } int organizer::removeStudent(string email) { if(searchStudents(email) != -1) { string empty = "empty"; students[searchStudents(email)].setAdressLine1(empty); students[searchStudents(email)].setAdressLine2(empty); students[searchStudents(email)].setName(empty); students[searchStudents(email)].setOther(empty); students[searchStudents(email)].setPhoneNum(empty); students[searchStudents(email)].setZipCode(empty); students[searchStudents(email)].setCity(empty); students[searchStudents(email)].setState(empty); students[searchStudents(email)].setEmail(empty); removeItems(email); return 1; } else { return -1; } } void organizer::removeItems(string email) { int startingPoint = 0; string empty = "empty"; while(searchItems(startingPoint, email) != -1) { int locationToSet = searchItems(startingPoint, email); if(locationToSet == -1) { return; } multimedia[locationToSet].setAuthor(empty); multimedia[locationToSet].setItemName(empty); multimedia[locationToSet].setPrice(empty); multimedia[locationToSet].setStatus(empty); multimedia[locationToSet].setType(empty); multimedia[locationToSet].setOwner(empty); startingPoint = searchItems(startingPoint, email); } } void organizer::printStudents(int position, ofstream &outFile) { if(students[position].getEmail() != "empty") { students[position].print(outFile); } } void organizer::printItems(int position, ofstream &outFile, int printEmails) { if(multimedia[position].getOwner() != "empty") { multimedia[position].print(outFile, printEmails); outFile << endl; } } void organizer::printEmail(string email, ofstream &outFile) { for(int i = 0; i < 100; i++) { if(students[i].getEmail() == email) { printStudents(i, outFile); i = 1001; } } if(i == 100) { outFile << email << " not found." << endl; return; } for(int j = 0; j < 100; j++) { if(multimedia[j].getOwner() == email) { int printEmails = 0; printItems(j, outFile, printEmails); } } } int organizer::addStudent(ifstream &studentIn, string firstHeader) { string heading; string tempIn; string empty = "empty"; int studentNum = searchStudents(empty); heading = firstHeader; while(heading != "@@") { string email; if(heading == "@Name:") { studentIn.ignore(1000, '\n'); getline(studentIn, tempIn, '\n'); students[studentNum].setName(tempIn); tempIn = ""; } else if(heading == "@Phone") { studentIn.ignore(1000, '\n'); getline(studentIn, tempIn, '\n'); if(tempIn.length() != 14) { removeStudent(email); return -1; } students[studentNum].setPhoneNum(tempIn); tempIn = ""; } else if(heading == "@E-mail:" ) { studentIn >> tempIn; students[studentNum].setEmail(tempIn); email = tempIn; tempIn = ""; } else if(heading == "@Address") { studentIn >> tempIn; studentIn >> tempIn; if(tempIn == "1:") { studentIn.ignore(1000, '\n'); getline(studentIn, tempIn, '\n'); students[studentNum].setAdressLine1(tempIn); tempIn = ""; } else if(tempIn == "2:") { studentIn.ignore(1000, '\n'); getline(studentIn, tempIn, '\n'); students[studentNum].setAdressLine2(tempIn); tempIn = ""; } } else if(heading == "@City:" ) { studentIn >> tempIn; students[studentNum].setCity(tempIn); tempIn = ""; } else if(heading == "@Zipcode:" ) { studentIn >> tempIn; if(tempIn.length() != 5) { removeStudent(email); return -1; } students[studentNum].setZipCode(tempIn); tempIn = ""; } else if(heading == "@Other:" ) { studentIn.ignore(1000, '\n'); getline(studentIn, tempIn, '\n'); students[studentNum].setOther(tempIn); tempIn = ""; } else if(heading == "@State:") { studentIn >> tempIn; students[studentNum].setState(tempIn); tempIn = ""; } else { return -1; } studentIn >> heading; } return 1; } int organizer::addItem(ifstream &itemIn, string firstHeader) { string heading; string tempIn; string empty = "empty"; string email; int searchStart = 0; int itemNum = searchItems(searchStart, empty); heading = firstHeader; while(heading != "@@") { if(heading == "@Media Type:") { getline(itemIn, tempIn, '\n'); multimedia[itemNum].setType(tempIn); tempIn = ""; } else if(heading == "@Item:") { getline(itemIn, tempIn, '\n'); multimedia[itemNum].setItemName(tempIn); tempIn = ""; } else if(heading == "@by:") { getline(itemIn, tempIn, '\n'); multimedia[itemNum].setAuthor(tempIn); tempIn = ""; } else if(heading == "@Owner:") { getline(itemIn, tempIn, '\n'); if(searchStudents(tempIn) == -1) { multimedia[itemNum].setEmpty(); string endEntryIn; while(endEntryIn != "@@") { itemIn >> endEntryIn; } return -1; } multimedia[itemNum].setOwner(tempIn); email = tempIn; tempIn = ""; } else if(heading == "@Price:") { getline(itemIn, tempIn, '\n'); multimedia[itemNum].setPrice(tempIn); tempIn = ""; } else if(heading == "@Availability:") { getline(itemIn, tempIn, '\n'); multimedia[itemNum].setStatus(tempIn); tempIn = ""; } getline(itemIn, heading, '\n'); } if(email == "empty") { multimedia[itemNum].setEmpty(); } return 1; }