// Project 2 for CS 1704 Spring 2004 // // Programmer: Hubert Chin // Last modified: February 25, 2004 // // Purpose // This program represent a database where students can share media // items. It can add and delete items, owners, etc. It stores up to // 100 items' information and 100 students' information. The program // then writes what is specified in the input file commands.data to // an output file - output.data #include "studentinfo.h" #include "mediainfo.h" #include #include #include #include using namespace std; /////////////////////////////////////////////////// // Manager // //purpose: Manager class manages the StudentInfo and //MediaInfo arrays and provides functions to manipulate //and print the arrays /////////////////////////////////////////////////// class Manager { public: Manager(); StudentInfo Students[100]; //arrays MediaInfo Media[100]; void PrintStudents(ofstream&); void PrintMedia(ofstream&); void PrintEmail(string&, ofstream&); bool AddStudents(ifstream&); bool AddMedia(ifstream&); bool Delete(string&); void Quit(ofstream&); int Sidx, Midx; //number of spaces used by Students and Media array }; /////////////////////////////////////////////////// // Constructor // //purpose: initializes the values of both arrays and //their objects to x //This has already been done, but I wrote this constructor //before the other ones /////////////////////////////////////////////////// Manager::Manager() { int x; Sidx = 0; Midx = 0; for(x = 0; x < 100; x++) //initializes values to x { Students[x].Name = "x"; Students[x].Number = "x"; Students[x].Address1 = "x"; Students[x].Address2 = "x"; Students[x].Email = "x"; Students[x].Other = "x"; Students[x].State = "x"; Students[x].Zip = "x"; Students[x].City = "x"; Media[x].Business = "x"; Media[x].Price = "x"; Media[x].Name = "x"; Media[x].Type = "x"; Media[x].Author = "x"; Media[x].Owner = "x"; } } void GetCommand(Manager&, ifstream&, ofstream&); void LoadStudents(Manager&, ifstream&); void LoadMedia(Manager&, ifstream&); main() { Manager Organizer; //student organizer ifstream StudentData; //input files ifstream MediaData; ifstream Commands; ofstream OutFile; StudentData.open("students.data"); MediaData.open("items.data"); Commands.open("commands.data"); OutFile.open("output.data"); LoadStudents(Organizer, StudentData); //students.data LoadMedia(Organizer, MediaData); //items.data GetCommand(Organizer, Commands, OutFile); StudentData.close(); //after everything is done, closes file streams MediaData.close(); Commands.close(); OutFile.close(); } /////////////////////////////////////////////////// // LoadStudents // //Input: Organizer, input file StudentData //purpose: Loads all STudent information from students.data /////////////////////////////////////////////////// void LoadStudents(Manager& Organizer, ifstream& StudentData) { string line; //stores each line to determine what kind of information it is int idx = 0; //goes through Organizer array while((StudentData)) { getline(StudentData, line, '\n'); StudentData.ignore(0, '\n'); if(line == "@Name:") //these lines test line for what kind of information is being provided getline(StudentData, Organizer.Students[idx].Name, '\n'); else if(line == "@Phone Number:") getline(StudentData, Organizer.Students[idx].Number, '\n'); else if(line == "@E-mail:") getline(StudentData, Organizer.Students[idx].Email, '\n'); else if(line == "@Address Line 1:") getline(StudentData, Organizer.Students[idx].Address1, '\n'); else if(line == "@Address Line 2:") getline(StudentData, Organizer.Students[idx].Address2, '\n'); else if(line == "@Other:") getline(StudentData, Organizer.Students[idx].Other, '\n'); else if(line == "@State:") getline(StudentData, Organizer.Students[idx].State, '\n'); else if(line == "@Zipcode:") getline(StudentData, Organizer.Students[idx].Zip, '\n'); else if(line == "@City:") getline(StudentData, Organizer.Students[idx].City, '\n'); else if(line == "@@") //at the end, tests if the email was provided...if not, reset all values { if(Organizer.Students[idx].Email == "x") { Organizer.Students[idx].Address1 = "x"; Organizer.Students[idx].Address2 = "x"; Organizer.Students[idx].City = "x"; Organizer.Students[idx].Name = "x"; Organizer.Students[idx].Number = "x"; Organizer.Students[idx].Other = "x"; Organizer.Students[idx].State = "x"; Organizer.Students[idx].Zip = "x"; } else //if email is given, idx is incremented and continues idx++; } StudentData.ignore(0, '\n'); } Organizer.Sidx = idx; } /////////////////////////////////////////////////// // LoadMedia // //Input: Organizer, input file MediaData //purpose: Loads all STudent information from items.data /////////////////////////////////////////////////// void LoadMedia(Manager& Organizer, ifstream& MediaData) { string line; //stores each line to determine what kind of information is given int idx = 0; while((MediaData)) { getline(MediaData, line, '\n'); MediaData.ignore(0, '\n'); if(line == "@by:") //tests what kind of information is being provided getline(MediaData, Organizer.Media[idx].Author, '\n'); else if(line == "@Owner:") getline(MediaData, Organizer.Media[idx].Owner, '\n'); else if(line == "@Availability:") getline(MediaData, Organizer.Media[idx].Business, '\n'); else if(line == "@Item:") getline(MediaData, Organizer.Media[idx].Name, '\n'); else if(line == "@Price:") getline(MediaData, Organizer.Media[idx].Price, '\n'); else if(line == "@Media Type:") getline(MediaData, Organizer.Media[idx].Type, '\n'); else if(line == "@@") //if owner information has not been provided, reset all values { if(Organizer.Media[idx].Owner == "x") { Organizer.Media[idx].Author = "x"; Organizer.Media[idx].Business = "x"; Organizer.Media[idx].Name = "x"; Organizer.Media[idx].Price = "x"; Organizer.Media[idx].Type = "x"; } else //if owner info is provided, increment idx and move on idx++; } MediaData.ignore(0, '\n'); } Organizer.Midx = idx; } /////////////////////////////////////////////////// // GetCommand // //Input: Organizer, input file Commands, output file OutFile //purpose: Loads each command from commands.data file and calls //the correct function provided by Organizer class /////////////////////////////////////////////////// void GetCommand(Manager& Organizer, ifstream& Commands, ofstream& OutFile) { string command; string command2; while((Commands) && (command != "quit")) //stops if command is "quit" { Commands >> command; Commands.ignore(1, '\n'); if(command == "print") { //calls print functions of Organizer (of class Manager) Commands >> command2; Commands.ignore(1, '\n'); if(command2 == "students") { OutFile << "print students" << endl; Organizer.PrintStudents(OutFile); //calls Organizer's print students function } else if(command2 == "items") { OutFile << "print items" << endl; Organizer.PrintMedia(OutFile); //calls Organizer's print media function } else { OutFile << "print " << command2 << endl; Organizer.PrintEmail(command2, OutFile); //calls organizer's print email function } } else if(command == "add") { Commands >> command2; //add student or item? then puts in command2 Commands.ignore(1, '\n'); if(command2 == "student") { OutFile << "add student" << endl; if((Organizer.AddStudents(Commands)) == true) //calls organizer's add studdents function OutFile << "Success" << endl; else OutFile << "Failure" << endl; } else if(command2 == "item") { OutFile << "add item" << endl; if((Organizer.AddMedia(Commands)) == true) //calls organizer's add media function OutFile << "Success" << endl; else OutFile << "Failure" << endl; } } else if(command == "delete") { Commands >> command2; Commands.ignore(1, '\n'); OutFile << "delete " << command2 << endl; if((Organizer.Delete(command2)) == true) //calls organizer's delete function OutFile << "Success" << endl; else OutFile << "Failure" << endl; } else if(command == "quit") { Organizer.Quit(OutFile); //calls organizer's quit function } } } /////////////////////////////////////////////////// // Manager::PrintStudents // //Input: output file OutFile //purpose: prints each student's information from //the StudentData array /////////////////////////////////////////////////// void Manager::PrintStudents(ofstream& OutFile) { int idx; for(idx = 0; idx < Sidx; idx++) //goes thru organizer and prints the student array's information { if(Students[idx].Name != "x") { OutFile << "Name:" << endl; OutFile << " " << Students[idx].Name << endl; } if(Students[idx].Number != "x") { OutFile << "Phone Number:" << endl; OutFile << " " << Students[idx].Number << endl; } if(Students[idx].Email != "x") { OutFile << "Email:" << endl; OutFile << " " << Students[idx].Email << endl; } if(Students[idx].Address1 != "x") { OutFile << "Address:" << endl; OutFile << " " << Students[idx].Address1 << endl; } if(Students[idx].Address2 != "x") { if(Students[idx].Address1 == "x") { OutFile << "Address:" << endl; } OutFile << " " << Students[idx].Address2 << endl; } if(Students[idx].City != "x") { if((Students[idx].Address1 == "x")&&(Students[idx].Address2 == "x")) OutFile << "Address:" << endl; OutFile << " " << Students[idx].City; if((Students[idx].State != "x")||(Students[idx].Zip != "x")) OutFile << ", "; // << Students[idx].State << " "; //else // OutFile << endl; if((Students[idx].State == "x")&&(Students[idx].Zip == "x")) OutFile << endl; } if(Students[idx].State != "x") { if((Students[idx].Address1 == "x")&&(Students[idx].Address2 == "x")&&(Students[idx].City == "x")) OutFile << "Address:" << endl; //if(Students[idx].City == "x") OutFile << " " << Students[idx].State << " "; if(Students[idx].Zip == "x") OutFile << endl; } if(Students[idx].Zip != "x") { if((Students[idx].Address1 == "x")&&(Students[idx].Address2 == "x")&&(Students[idx].City == "x")&&(Students[idx].State == "x")) OutFile << "Address:" << endl; OutFile << " " << Students[idx].Zip << endl; } if(Students[idx].Other != "x") { OutFile << "Other:" << endl; OutFile << Students[idx].Other << endl; } if(Students[idx].Email != "x") OutFile << endl; } } /////////////////////////////////////////////////// // Manager::PrintMedia // //Input: output file OutFile //purpose: prints each item's information from //the MediaInfo array /////////////////////////////////////////////////// void Manager::PrintMedia(ofstream& OutFile) { int idx = 0; for(idx = 0; idx < Midx; idx++) //goes through the organizer's media array and prints each item's information { if(Media[idx].Owner != "x") { if(Media[idx].Name != "x") { OutFile << "Item:" << endl; OutFile << Media[idx].Name << endl; } if(Media[idx].Type != "x") { OutFile << "Media Type:" << endl; OutFile << Media[idx].Type << endl; } if(Media[idx].Author != "x") { OutFile << "by:" << endl; OutFile << Media[idx].Author << endl; } if(Media[idx].Owner != "x") { OutFile << "Owner:" << endl; OutFile << Media[idx].Owner << endl; } if(Media[idx].Price != "x") { OutFile << "Price:" << endl; OutFile << Media[idx].Price << endl; } if(Media[idx].Business != "x") { OutFile << "Availability:" << endl; OutFile << Media[idx].Business << endl; } OutFile << endl; } } } /////////////////////////////////////////////////// // Manager::Delete // //Input: string email //purpose: finds STudent with string email in the //STudentData array and finds items owned by that student //and deletes them /////////////////////////////////////////////////// bool Manager::Delete(string& email) { int idx = 0; bool found = false; for(idx = 0; idx < 100; idx++) { if(Students[idx].Email == email) //finds a matching email address in Organizer's student and resets values to default 'x' { Students[idx].Name = "x"; Students[idx].Number = "x"; Students[idx].Address1 = "x"; Students[idx].Address2 = "x"; Students[idx].Email = "x"; Students[idx].Other = "x"; Students[idx].State = "x"; Students[idx].Zip = "x"; Students[idx].City = "x"; found = true; } } for(idx = 0; idx < 100; idx++) { if(email == "gwlcqzoi@vt.edu") idx = idx; if(Media[idx].Owner == email)//finds a matching email address in Organizer's media and resets values to default 'x' { Media[idx].Business = "x"; Media[idx].Price = "x"; Media[idx].Name = "x"; Media[idx].Type = "x"; Media[idx].Author = "x"; Media[idx].Owner = "x"; found = true; } } if(found == true) return true; else return false; } /////////////////////////////////////////////////// // Manager::AddMedia // //Input: input file Commands //purpose: adds the item specified in the Commands file //into the MediaInfo array /////////////////////////////////////////////////// bool Manager::AddMedia(ifstream& Commands) { string line; bool found = false; int idx = 0, idx2 = 0; //stores indices while((found == false)&&(idx < Midx+1)) //finds first available space in Organizer's medai array { if(Media[idx].Owner == "x") { found = true; } idx++; } idx--; //idx is incremented one extra time if(idx >= 100) return false; found = false; //found needs to be reset back to false for another check while(line != "@@") { getline(Commands, line, '\n'); //gets medai info from Commands.data and stores it in array Commands.ignore(0, '\n'); if(line == "@by:") getline(Commands, Media[idx].Author, '\n'); else if(line == "@Owner:") { getline(Commands, Media[idx].Owner, '\n'); for(idx2 = 0; idx2 < 100; idx2++) { if(Students[idx2].Email == Media[idx].Owner) found = true; } if(found == false) { Media[idx].Author = "x"; Media[idx].Business = "x"; Media[idx].Owner = "x"; Media[idx].Name = "x"; Media[idx].Price = "x"; Media[idx].Type = "x"; return false; } } else if(line == "@Availability:") getline(Commands, Media[idx].Business, '\n'); else if(line == "@Item:") getline(Commands, Media[idx].Name, '\n'); else if(line == "@Price:") getline(Commands, Media[idx].Price, '\n'); else if(line == "@Media Type:") getline(Commands, Media[idx].Type, '\n'); else if(line == "@@") { if(Media[idx].Owner == "x") //if no email address is given, resets values { Media[idx].Author = "x"; Media[idx].Business = "x"; Media[idx].Name = "x"; Media[idx].Price = "x"; Media[idx].Type = "x"; return false; } else { return true; idx++; } } } if(idx > Midx) Midx = idx; Commands.ignore(0, '\n'); return false; } /////////////////////////////////////////////////// // Manager::AddStudents // //Input: input file Commands //purpose: adds the STudent's information specified //in the Commands file to the studentdata array /////////////////////////////////////////////////// bool Manager::AddStudents(ifstream& Commands) { string line; bool found = false; int idx = 0; while((found == false)&&(idx < Sidx+1)) //finds first available space in Students array { if(Students[idx].Email == "x") { found = true; } idx++; } idx--; if(idx >= 100) return false; while(line != "@@") { getline(Commands, line, '\n'); Commands.ignore(0, '\n'); //cout << line << "x" << endl; if(line == "@Name:") //gets student info from Commands.data and stores it in Students array getline(Commands, Students[idx].Name, '\n'); if(line == "@Phone Number:") getline(Commands, Students[idx].Number, '\n'); if(line == "@E-mail:") getline(Commands, Students[idx].Email, '\n'); if(line == "@Address Line 1:") getline(Commands, Students[idx].Address1, '\n'); if(line == "@Address Line 2:") getline(Commands, Students[idx].Address2, '\n'); if(line == "@Other:") getline(Commands, Students[idx].Other, '\n'); if(line == "@State:") getline(Commands, Students[idx].State, '\n'); if(line == "@Zipcode:") getline(Commands, Students[idx].Zip, '\n'); if(line == "@City:") getline(Commands, Students[idx].City, '\n'); if(line == "@@") { if(Students[idx].Email == "x") //if email is not provided, resest values { Students[idx].Address1 = "x"; Students[idx].Address2 = "x"; Students[idx].City = "x"; Students[idx].Name = "x"; Students[idx].Number = "x"; Students[idx].Other = "x"; Students[idx].State = "x"; Students[idx].Zip = "x"; return false; } else { return true; idx++; } } } if(idx > Sidx) Sidx = idx; Commands.ignore(0, '\n'); return false; } /////////////////////////////////////////////////// // Manager::Quit // //Input: output file OutFile //purpose: prints "quit" before all filestreams are //closed in main /////////////////////////////////////////////////// void Manager::Quit(ofstream& OutFile) { OutFile << "quit"; //if you dont understand this, youre stupid } /////////////////////////////////////////////////// // Manager::PrintEmail // //Input: string email, output file OutFile //purpose: Finds student in STudentData array that has //matching email address as string email, then prints //the students data into output file OutFile. then finds //items owned by the students and prints those out /////////////////////////////////////////////////// void Manager::PrintEmail(string& email, ofstream& OutFile) { int idx = 0; bool found = false; for(idx = 0; idx < Sidx; idx++) { if(Students[idx].Email == email) //finds matching email address and prints studnets info out { found = true; if(Students[idx].Name != "x") { OutFile << "Name:" << endl; OutFile << " " << Students[idx].Name << endl; } if(Students[idx].Number != "x") { OutFile << "Phone Number:" << endl; OutFile << " " << Students[idx].Number << endl; } if(Students[idx].Email != "x") { OutFile << "Email:" << endl; OutFile << " " << Students[idx].Email << endl; } if(Students[idx].Address1 != "x") { OutFile << "Address:" << endl; OutFile << " " << Students[idx].Address1 << endl; } if(Students[idx].Address2 != "x") { if(Students[idx].Address1 == "x") { OutFile << "Address:" << endl; } OutFile << " " << Students[idx].Address2 << endl; } if(Students[idx].City != "x") { if((Students[idx].Address1 == "x")&&(Students[idx].Address2 == "x")) OutFile << "Address:" << endl; OutFile << " " << Students[idx].City; if((Students[idx].State != "x")||(Students[idx].Zip != "x")) OutFile << ", "; // << Students[idx].State << " "; //else // OutFile << endl; if((Students[idx].State == "x")&&(Students[idx].Zip == "x")) OutFile << endl; } if(Students[idx].State != "x") { if((Students[idx].Address1 == "x")&&(Students[idx].Address2 == "x")&&(Students[idx].City == "x")) OutFile << "Address:" << endl; //if(Students[idx].City == "x") OutFile << " " << Students[idx].State << " "; if(Students[idx].Zip == "x") OutFile << endl; } if(Students[idx].Zip != "x") { if((Students[idx].Address1 == "x")&&(Students[idx].Address2 == "x")&&(Students[idx].City == "x")&&(Students[idx].State == "x")) OutFile << "Address:" << endl; OutFile << " " << Students[idx].Zip << endl; } if(Students[idx].Other != "x") { OutFile << "Other:" << endl; OutFile << Students[idx].Other << endl; } if(Students[idx].Email != "x") OutFile << endl; } } for(idx = 0; idx < Midx; idx++) { if(Media[idx].Owner == email) //finds matchign email info and prints items' information { found = true; if(Media[idx].Name != "x") { OutFile << "Item:" << endl; OutFile << Media[idx].Name << endl; } if(Media[idx].Type != "x") { OutFile << "Media Type:" << endl; OutFile << Media[idx].Type << endl; } if(Media[idx].Author != "x") { OutFile << "by:" << endl; OutFile << Media[idx].Author << endl; } if(Media[idx].Price != "x") { OutFile << "Price:" << endl; OutFile << Media[idx].Price << endl; } if(Media[idx].Business != "x") { OutFile << "Availability:" << endl; OutFile << Media[idx].Business << endl; } OutFile << endl; } } if(found == false) OutFile << email << " not found." << endl; }