#include "container.h" #include #include #include #include using namespace std; Container::Container() { //this function creates the container class - initialising it through the calling of addstudents and addmedia NStudents = AddStudents(); //cout << NStudents << endl; NMedia = AddMedia(); //cout << NMedia << endl; } int Container::AddStudents() { //this function reads students.data and adds to the students array. char tempch[200];//temporary holding spot for a line inputted before conversion to string. string Command;//holds inputted data int Index = 0;//needed to hold what index of the array we are on. ifstream RouteDataIn("students.data"); //open file stream for input while(!RouteDataIn.eof())//keep on inputting as long as there is an input. { for (int i = 0; i < 200;i++)//this for loop erases what is in the temporary array so there arent any extraneous characters. { tempch[i] = 0;//does the actual erasing } RouteDataIn.getline(tempch, 200);//input data to char for Command = CharToStringer(tempch,200);//convert to string for (int k = Command.length()-1; k >0; k--)//this for loop removes extraneous characters at the end of the string. { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1);//does actual deleting of characters } //cout << "'" << Command << "'"<0; k--)//removes extraneous characters { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setStudentName(Command, Index);//passes it onto the setStudentNAme function. } if (Command == "@Phone Number:")//everything here is same as above. { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setStudentPhone(Command, Index); } if (Command == "@E-mail:")//everything here is same as above. { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setStudentEmail(Command, Index); } if (Command == "@Address Line 1:")//everything here is same as above. { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setStudentAdd1(Command, Index); } if (Command == "@Address Line 2:")//everything here is same as above. { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setStudentAdd2(Command, Index); } if (Command == "@City:")//everything here is same as above. { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setStudentCity(Command, Index); } if (Command == "@State:")//everything here is same as above. { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setStudentState(Command, Index); } if (Command == "@Zipcode:")//everything here is same as above except when noted. { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } int x = StringToInt(Command);//convert the string to an int for data storage reasons. setStudentZip(x, Index); } if (Command == "@Other:")//everything here is same as above. { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setStudentOther(Command, Index); } if (Command == "@@")//if we reach what the input claims is the end of this student's data... { if (getStudentEmail(Index) != "")Index++;//only move onto next student if we have a valid email address else StudentList[Index].Delete();//if we dont have valid email address, delete this student. } } return Index;//used as number of students in array. } int Container::AddMedia() { //this function reads in from items.data to input to the array. char tempch[200];//temporary holding spot for a line inputted before conversion to string. string Command;//holds inputted command int Index = 0;//holds index of the array we are at. ifstream RouteDataIn("items.data"); //open file stream for input int found = 0;//necessary at the end of the function to see if the student we are trying to add item to exists. while(!RouteDataIn.eof()) { for (int i = 0; i < 200;i++)//reformat temporary char array { tempch[i] = 0;//do actual formatting } RouteDataIn.getline(tempch, 200);//input to char Command = CharToStringer(tempch,200);//convert to string for (int k = Command.length()-1; k >0; k--)//removes extraneous characters at end of line. { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1);//actual removing } //these next if statements check on the inputted command and perform more operations based on the command. if (Command == "@Item:") { for (int i = 0; i < 200;i++)//clear temporary char array to prepare for input { tempch[i] = 0; } RouteDataIn.getline(tempch, 200);//input item name Command = CharToStringer(tempch,200);//convert to string for (int k = Command.length()-1; k >0; k--)//format string so no extraneous characters { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setMediaItem(Command, Index);//pass on data so that it can be set. } if (Command == "@Media Type:")//everything here is same as above. { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setMediaType(Command, Index); } if (Command == "@by:")//everything here is same as above. { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setMediaBy(Command, Index); } if (Command == "@Owner:")//everything here is same as above. { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setMediaOwner(Command, Index); } if (Command == "@Price:")//everything here is same as above. { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setMediaPrice(Command, Index); } if (Command == "@Availability:")//everything here is same as above. { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setMediaAvailability(Command, Index); } if (Command == "@@")//if we have reached the end of this item... { found = 0; if (getMediaOwner(Index) != "") { for (int a = 0; a0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } if (Command == "@Item:") { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setMediaItem(Command, Index); } if (Command == "@Media Type:") { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setMediaType(Command, Index); } if (Command == "@by:") { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setMediaBy(Command, Index); } if (Command == "@Owner:") { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setMediaOwner(Command, Index); } if (Command == "@Price:") { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setMediaPrice(Command, Index); } if (Command == "@Availability:") { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setMediaAvailability(Command, Index); } } int found = 0; if (getMediaOwner(Index) != "") { for (int a = 0; a0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } //cout << "'" << Command << "'"<0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setStudentName(Command, Index); } if (Command == "@Phone Number:") { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setStudentPhone(Command, Index); } if (Command == "@E-mail:") { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setStudentEmail(Command, Index); } if (Command == "@Address Line 1:") { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setStudentAdd1(Command, Index); } if (Command == "@Address Line 2:") { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setStudentAdd2(Command, Index); } if (Command == "@City:") { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setStudentCity(Command, Index); } if (Command == "@State:") { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setStudentState(Command, Index); } if (Command == "@Zipcode:") { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } int x = StringToInt(Command); setStudentZip(x, Index); } if (Command == "@Other:") { for (int i = 0; i < 200;i++) { tempch[i] = 0; } RouteDataIn.getline(tempch, 200); Command = CharToStringer(tempch,200); for (int k = Command.length()-1; k >0; k--) { if (Command[k]== 0 ) Command = Command.substr(0,Command.length()-1); } setStudentOther(Command, Index); } } if (getStudentEmail(Index) != "") { NMedia++; Log << "Success" << endl; return 1; } StudentList[Index].Delete(); Log << "Failure" << endl; return 0; } int Container::DeleteStudent(ofstream & Log, string email) { //deletes a student from array based on email address int found = 0;//makes sure we have found the student. for (int i = 0; i < NStudents; i++)//search for student { // cout << StudentList[i].getEmail() <<" != " << email << endl; if (StudentList[i].getEmail() == email)//if we have a match/... { RemoveStudent(i);//remove him/her found = 1; break; } } if (!found) { Log << "Failure" << endl; return 0; } Log << "Success" << endl; DeleteMedia(Log, email);//delete all media items belonging to student. return 1; } int Container::RemoveStudent(int index) { //physically removes student. StudentList[index].Delete(); //NStudents--; return 1; } //simple mutators and acessors string Container::getStudentName(int index) { return StudentList[index].getName(); } int Container::setStudentName(string Input, int index) { StudentList[index].setName(Input); return 1; } string Container::getStudentPhone(int index) { return StudentList[index].getPhone(); } int Container::setStudentPhone(string Input, int index) { StudentList[index].setPhone(Input); return 1; } string Container::getStudentAdd1(int index) { return StudentList[index].getAdd1(); } int Container::setStudentAdd1(string Input, int index) { StudentList[index].setAdd1(Input); return 1; } string Container::getStudentAdd2(int index) { return StudentList[index].getAdd2(); } int Container::setStudentAdd2(string Input, int index) { StudentList[index].setAdd2(Input); return 1; } string Container::getStudentCity(int index) { return StudentList[index].getCity(); } int Container::setStudentCity(string Input, int index) { StudentList[index].setCity(Input); return 1; } string Container::getStudentState(int index) { return StudentList[index].getState(); } int Container::setStudentState(string Input, int index) { StudentList[index].setState(Input); return 1; } int Container::getStudentZip(int index) { return StudentList[index].getZip(); } int Container::setStudentZip(int Input, int index) { if (Input > -1 && Input < 100000) { StudentList[index].setZip(Input); return 1; } return 0; } string Container::getStudentEmail(int index) { return StudentList[index].getEmail(); } int Container::setStudentEmail(string Input, int index) { StudentList[index].setEmail(Input); return 1; } string Container::getStudentOther(int index) { return StudentList[index].getOther(); } int Container::setStudentOther(string Input, int index) { StudentList[index].setOther(Input); return 1; } int Container::StringToInt(string input) { //this function converts strings containing decimal characters into integers so that all the numbers in the city and script files become useable. int output = 0; for(int i = 0; i < (int)input.length(); i++) { output += Power(10, (int)input.length() - i-1)*((int)input[i]-48); } return output; } int Container::Power(int input1, int input2) { int output = 1; for (int i = 0; i < input2; i++) { output *=input1; } return output; } string Container::CharToStringer(char input[], int size) { //converts from a C-style array of chars to a full string. Very simple addition. string output = ""; for(int i =0;i