// Project 2 for CS 1704 Spring 2004 // // Programmer: Thomas Nguyen // OS: Windows XP Pro // System: Pentium III 800, 512 MB Memory // Compiler: Visual C++ 6.0, Service Pack 4 // Last modified: Feb 22, 2004 // // Purpose // This program acts as an electronic organizer. It can store information on students and their items. // Student information can be name, address, phone number, email address, and a comment. Item information // can be name, author, price, owner, type, and availability. Functions of this organizer are // add student - adds a student to the organizer and his/her information // add item - adds an item of a student and information on that item // delete student - deletes a student and items belonging to him/her // print student - prints information on a student and the items that belong to him/her // print items - prints every item in organizer // print students - prints information on all students in organizer // // On my honor: // // - I have not discussed the C++ language code in my program with // anyone other than my instructor or the teaching assistants // assigned to this course. // // - I have not used C++ language code obtained from another student, // or any other unauthorized source, either modified or unmodified. // // - If any C++ language code or documentation used in my program // was obtained from another source, such as a text book or course // notes, that has been clearly noted with a proper citation in // the comments of my program. // // - I have not designed this program in such a way as to defeat or // interfere with the normal operation of the Curator System. // // Thomas Nguyen #define NDEBUG #include #include #include #include #include #include #include using namespace std; ofstream outFile("output.data"); //output stream struct Address { string line1; //line 1 of address of student string line2; //line 2 of address of student string zipcode; //zipcode of student string city; //city of student string state; //state of student }; class Student { private: string name; //name of student string email; //email address of student Address address; //address of student string other; //comment relating to student string phone; //phone number of student public: Student(); //default constructor void setStudent(string setName, string setEmail, Address setAddress, string setOther, string setPhone); //function to set private members string getName() const { return name; }; //returns name string getEmail() const { return email; }; //returns email address Address getAddress() const { return address; }; //returns address string getOther() const { return other; }; //returns comment string getPhone() const { return phone; }; //returns phone number }; class Item { private: string name; //name of item string type; //type of item string author; //author of item string owner; //owner of item string price; //price of item string status; //availability of item public: Item(); //default constructor void setItem(string setName, string setType, string setAuthor, string setOwner, string setPrice, string setStatus); //function to set private members string getName() const { return name; }; //returns name string getType() const { return type; }; //returns type string getAuthor() const { return author; }; //returns author string getOwner() const { return owner; }; //returns owner string getPrice() const { return price; }; //returns price string getStatus() const { return status; }; //returns status }; class Organizer { private: Student students[100]; //array of students Item items[100]; //array of items int studentCount; //number of students int itemCount; //number of items public: Organizer(); //default constructor int checkStudent(string email); //returns index of a student if student exist else returns dummy value of 999 bool addStudent(string setName, string setEmail, Address setAddress, string setOther, string setPhone, int display); //function that adds a student bool addItem(string setName, string setType, string setAuthor, string setOwner, string setPrice, string setStatus, int display); //function that adds an item void printStudent(string email); //function that prints info on a specific student and items belonging to student void printAllStudents(); //function that prints all students in organizer void printItems(); //function that prints all items in organizer void delStudent(string email); //function that deletes a specific student and items belonging to student }; Organizer::Organizer() { studentCount = 0; itemCount = 0; } Student::Student() { //sets private members to dummy values name = "empty"; email = "empty"; other = "empty"; phone = "empty"; address.line1 = "empty"; address.line2 = "empty"; address.city = "empty"; address.state = "empty"; address.zipcode = "empty"; } Item::Item() { //sets private members to dummy values name = "empty"; type = "empty"; author = "empty"; owner = "empty"; price = "empty"; status = "empty"; } //////////////////////////////////////////////////////////////// Organizer::checkStudent // searches for a student in students array and returns index if student is found else // dummy value of 999 is returned if student is not found // // Parameters: // email: email address of student to be searched in students array // // Pre: NONE // // Post: index of student is returned or dummy value is returned // // Returns: index of student is returned or dummy value is returned // // Called by: main // Calls: NONE // int Organizer::checkStudent(string email) { //loop searches for student and returns index if student is found for(int i=0; i<100; i++) { if(students[i].getEmail() == email) return i; } return 999; //returns dummy value if student not found } //////////////////////////////////////////////////////////////// Organizer::addStudent // adds a student to students array if student doesn't already exist or array is full // // Parameters: // setName: name of student // setEmail: email of student // setAddress: address of student // setOther: comment about student // setPhone: phone number of student // display: variable that tells whether to output success/failure message // // Pre: NONE // // Post: NONE // // Returns: true or false depending if add is successful or unsuccessful // // Called by: readScript // Calls: Organizer::checkStudent // bool Organizer::addStudent(string setName, string setEmail, Address setAddress, string setOther, string setPhone, int display) { int openSpace; //index of first open space in students array if(checkStudent(setEmail) == 999 && studentCount != 100) //checks if student already exist { //loop finds the first open space in students array for(int i=0; i<100; i++) { if(students[i].getEmail() == "empty") { openSpace = i; break; } } students[openSpace].setStudent(setName, setEmail, setAddress, setOther, setPhone); //adds student if(display == 1) outFile<<"Success"<5); return 0; } //////////////////////////////////////////////////////////////// initialize // takes input from students.data and items.data and adds the students and items in the file to the organizer // // Parameters: // organizer: organizer object that students and items are being added to // // Pre: NONE // // Post: organizer is filled with students // // Returns: NONE // // Called by: main // Calls: Organizer::addStudent // void initialize(Organizer &organizer) { string name = "empty"; //variable to hold input student name or input item name string email = "empty"; //variable to hold input student email string other = "empty"; //variable to hold input student comment string phone = "empty"; //variable to hold input student phone number string owner = "empty"; //variable to hold input item owner string author = "empty"; //variable to hold input item author string status = "empty"; //variable to hold input item availability string type = "empty"; //variable to hold input item type string price = "empty"; //variable to hold input item price Address address; //variable to hold input student address //setting address members to dummy values address.city = "empty"; address.line1 = "empty"; address.line2 = "empty"; address.state = "empty"; address.zipcode = "empty"; string line; //string to hold input from stream bool valid = true; //variable that tells whether format is correct or incorrect ifstream studentsInput("students.data"); //stream for students.data ifstream itemsInput("items.data"); //stream for items.data while(!studentsInput.eof()) { while(line != "@@") { getline(studentsInput, line, '\n'); if(line == "@Name:") getline(studentsInput, name, '\n'); else if(line == "@E-mail:") getline(studentsInput, email, '\n'); else if(line == "@Address Line 1:") getline(studentsInput, address.line1, '\n'); else if(line == "@Address Line 2:") getline(studentsInput, address.line2, '\n'); else if(line == "@City:") getline(studentsInput, address.city, '\n'); else if(line == "@State:") getline(studentsInput, address.state, '\n'); else if(line == "@Zipcode:") getline(studentsInput, address.zipcode, '\n'); else if(line == "@Other:") getline(studentsInput, other, '\n'); else if(line == "@Phone Number:") getline(studentsInput, phone, '\n'); else break; } if(email != "empty") { if(address.zipcode != "empty") //checks if zipcode format is correct { assert(address.zipcode.size() == 5 && isdigit(address.zipcode[0]) && isdigit(address.zipcode[1]) && isdigit(address.zipcode[2]) && isdigit(address.zipcode[3]) && isdigit(address.zipcode[4])); if(address.zipcode.size() != 5 || !isdigit(address.zipcode[0]) || !isdigit(address.zipcode[1]) || !isdigit(address.zipcode[2]) || !isdigit(address.zipcode[3]) || !isdigit(address.zipcode[4])) { valid = false; address.zipcode = "empty"; } } if(phone != "empty") //checks if phone number format is correct { assert(phone.size() == 14 && phone[0] == '(' && phone[4] == ')' && phone[5] == ' ' && phone[9] == '-' && isdigit(phone[1]) && isdigit(phone[2]) && isdigit(phone[3]) && isdigit(phone[6]) && isdigit(phone[7]) && isdigit(phone[8]) && isdigit(phone[10]) && isdigit(phone[11]) && isdigit(phone[12]) && isdigit(phone[13])); if(phone.size() != 14 || phone[0] != '(' || phone[4] != ')' || phone[5] != ' ' || phone[9] != '-' || !isdigit(phone[1]) || !isdigit(phone[2]) || !isdigit(phone[3]) || !isdigit(phone[6]) || !isdigit(phone[7]) || !isdigit(phone[8]) || !isdigit(phone[10]) || !isdigit(phone[11]) || !isdigit(phone[12]) || !isdigit(phone[13])) { valid = false; phone = "empty"; } } if(valid != false) //adds student if formats are correct { organizer.addStudent(name, email, address, other, phone, 0); } } //resets variable values valid = true; name = "empty"; email = "empty"; phone = "empty"; address.line1 = "empty"; address.line2 = "empty"; address.city = "empty"; address.state = "empty"; address.zipcode = "empty"; other = "empty"; getline(studentsInput, line, '\n'); } line = ""; while(!itemsInput.eof()) { while(line != "@@") { getline(itemsInput, line, '\n'); if(line == "@Owner:") getline(itemsInput, email, '\n'); else if(line == "@Item:") getline(itemsInput, name, '\n'); else if(line == "@Media Type:") getline(itemsInput, type, '\n'); else if(line == "@by:") getline(itemsInput, author, '\n'); else if(line == "@Availability:") getline(itemsInput, status, '\n'); else if(line == "@Price:") getline(itemsInput, price, '\n'); else break; } //checks price format if(price != "empty") { assert(price[0] == '$'); if(price[0] != '$') { valid = false; outFile<<"Failure"<1) valid = false; } } } //adds item if formats are valid if(valid == true && email != "empty") organizer.addItem(name, type, author, email, price, status, 0); //resets variable values email = "empty"; author = "empty"; price = "empty"; status = "empty"; type = "empty"; name = "empty"; getline(itemsInput, line, '\n'); } } //////////////////////////////////////////////////////////////// readScript // carries out commands in commands.data // // Parameters: // organizer: organizer object that students and items are being added to // // Pre: NONE // // Post: commands in commands.data are carried out // // Returns: NONE // // Called by: main // Calls: Organizer::addStudent, Organizer::addItem, Organizer::printItems, Organizer::printStudent // Organizer::printAllStudents, Organizer::printItems, Organizer::deleteStudent // void readScript(Organizer &organizer) { string name = "empty"; //variable to hold input student name or input item name string email = "empty"; //variable to hold input student email string other = "empty"; //variable to hold input student comment string phone = "empty"; //variable to hold input student phone number string owner = "empty"; //variable to hold input item owner string author = "empty"; //variable to hold input item author string status = "empty"; //variable to hold input item availability string type = "empty"; //variable to hold input item type string price = "empty"; //variable to hold input item price Address address; //variable to hold input student address //setting address members to dummy values address.city = "empty"; address.line1 = "empty"; address.line2 = "empty"; address.state = "empty"; address.zipcode = "empty"; string command, command2, line; //variables to hold commands and lines from commands.data stream bool valid = true; //variable that tells whether format is correct or incorrect ifstream input("commands.data"); //stream for commands.data while(command != "quit") { getline(input, line, '\n'); //gets a line from the stream istringstream sin(line); sin>>command; //deletes a student if(command == "delete") { sin>>email; outFile<<"delete "<>command2; if(command2 == "student") { while(command != "@@") { getline(input, command, '\n'); if(command == "@Name:") getline(input, name, '\n'); if(command == "@E-mail:") getline(input, email, '\n'); if(command == "@Address Line 1:") getline(input, address.line1, '\n'); if(command == "@Address Line 2:") getline(input, address.line2, '\n'); if(command == "@City:") getline(input, address.city, '\n'); if(command == "@State:") getline(input, address.state, '\n'); if(command == "@Zipcode:") getline(input, address.zipcode, '\n'); if(command == "@Other:") getline(input, other, '\n'); if(command == "@Phone Number:") getline(input, phone, '\n'); } outFile<<"add student"<1) valid = false; } } } if(valid == true && email != "empty") //adds item if formats are correct and student exist organizer.addItem(name, type, author, email, price, status, 1); else outFile<<"Failure"<>command2; outFile<<"print "<