// Project 2 for CS 1704 Spring 2004 // // Programmer: Chase Alix // OS: Windows XP Profession // System: AMD Athlon 2400+ - 2.0 Ghz, 512 MB DDRAM // Compiler: Visual Studio .net // Last modified: Feb. 20, 2004 // // Purpose // This program reads in a list of student information and stores it in an array // of a class type that contains all possible information for that student. Only // information that a student must have is an e-mail. The program then reads in // a list of media information and stores it in an array of a class type that // contains all possible information for that media. Both arrays are contained // in another class. // // The program then reads in a list of commands that can print one student, print // all the students, or print all the items. The commands can also add students and // add items, as well as removed students and all items associated with the students // and quit the program. Whenever a single student record is printed, all items // that the student owns are printed. Anytime an item is added, the student // must have a valid e-mail in the student information. // #include #include #include #include #include using namespace std; class Student { private: string Name; //stores name of student int AreaCode; //stores area code of phone number int Phone1; //stores first part of phone number int Phone2; //stores second part of phone number string E_Mail; //stores e-mail of student string Add1; //stores first part of address string Add2; //stores second part of address string City; //stores City string State; //stores state int ZipCode; //stores zip code string Other; //stores all other information public: Student(); //default constructor //adds a new student to the index with all the information inputted bool AddStudent(string StudentName, int Area, int PhoneNum1, int PhoneNum2, string EMail, string Addy1, string Addy2, string AddyCity, string AddyState, int Zip, string OtherInfo); string GetName(); //returns student name int GetArea(); //returns area code int GetPhone1(); //returns first part of phone number int GetPhone2(); //returns second part of phone number string GetE_Mail(); //returns the e-mail string GetAdd1(); //returns first part of address string GetAdd2(); //returns second part of address string GetCity(); //returns City string GetState(); //returns state int GetZip(); //returns zipcode string GetOther(); //returns other information }; //////////////////////////////////////////////////////////////// Student() // default constructor for student class // // Parameters: // // Pre: None // // Post: None // // Returns: None // // Called by: client code // Calls: None // Student::Student() { Name = "None"; AreaCode = -111; Phone1 = -111; Phone2 = -1111; E_Mail = "None"; Add1 = "None"; Add2 = "None"; City = "None"; State = "None"; ZipCode = -11111; Other = "None"; } //////////////////////////////////////////////////////////////// AddStudent() // Adds a student by assigneing the student info the inputted values from // the input file // // Parameters: // StudentName stores the name of the student // Area stores the area code of the student // PhoneNum1 stores the first part of the phone number // PhoneNum2 stores the second part of the phone number // EMail stores the e-mail of the student // Addy1 stores the first line of the address // Addy2 stores the second line of the address // AddyCity stores the city of the address // AddyState stores the state of the address // Zip stores the zipcode of the address // OtherInfo stores all the other info for the student // // Pre: All the inputs files must have been opened correctly, all input // must have been read in correctly // // Post: The student info will have been added; // // Returns: true if added correctly, false if not added correctly // // Called by: MediaIndex::AddStudent() // Calls: None // bool Student::AddStudent(string StudentName, int Area, int PhoneNum1, int PhoneNum2, string EMail, string Addy1, string Addy2, string AddyCity, string AddyState, int Zip, string OtherInfo) { Name = StudentName; AreaCode = Area; Phone1 = PhoneNum1; Phone2 = PhoneNum2; E_Mail = EMail; Add1 = Addy1; Add2 = Addy2; City = AddyCity; State = AddyState; ZipCode = Zip; Other = OtherInfo; return true; } //////////////////////////////////////////////////////////////// GetName() // returns the string variable of Name // // Parameters: // None // // Pre: There must be a name for the student // // Post: the students name will be returned // // Returns: string with the students name // // Called by: MediaIndex::Print() // Calls: None // string Student::GetName() { return Name; } //////////////////////////////////////////////////////////////// GetArea() // returns the int variable of AreaCode // // Parameters: // None // // Pre: There must be an area code for the student // // Post: the students area code will be returned // // Returns: int with the students area code // // Called by: MediaIndex::Print() // Calls: None // int Student::GetArea() { return AreaCode; } //////////////////////////////////////////////////////////////// GetPhone1() // returns the int variable of the first part of the phone number // // Parameters: // None // // Pre: There must be a phone number for the student // // Post: the students first part of the phone number will be returned // // Returns: int with the students first three numbers of the phone number // // Called by: MediaIndex::Print() // Calls: None // int Student::GetPhone1() { return Phone1; } //////////////////////////////////////////////////////////////// GetPhone2() // returns the int variable of the second part of the phone number // // Parameters: // None // // Pre: There must be a phone number for the student // // Post: the students second part of the phone number will be returned // // Returns: int with the students last fout numbers of the phone number // // Called by: MediaIndex::Print() // Calls: None // int Student::GetPhone2() { return Phone2; } //////////////////////////////////////////////////////////////// GetE_Mail() // returns the string variable of E_Mail // // Parameters: // None // // Pre: There must be an e-mail for the student // // Post: the e-mail address will be returned // // Returns: string with the students e-mail // // Called by: MediaIndex::GetStudentE_Mail() // Calls: None // string Student::GetE_Mail() { return E_Mail; } //////////////////////////////////////////////////////////////// GetAdd1() // returns the string variable of the first part of the address // // Parameters: // None // // Pre: There must be an address for the student // // Post: the students first part of the address will be returned // // Returns: string with the students first address // // Called by: MediaIndex::Print() // Calls: None // string Student::GetAdd1() { return Add1; } //////////////////////////////////////////////////////////////// GetAdd2() // returns the string variable of the second part of the address // // Parameters: // None // // Pre: There must be an address for the student // // Post: the students second part of the address will be returned // // Returns: string with the students second address // // Called by: MediaIndex::Print() // Calls: None // string Student::GetAdd2() { return Add2; } //////////////////////////////////////////////////////////////// GetCity() // returns the string variable of the city // // Parameters: // None // // Pre: There must be a city for the student // // Post: the students city will be returned // // Returns: string with the city // // Called by: MediaIndex::Print() // Calls: None // string Student::GetCity() { return City; } //////////////////////////////////////////////////////////////// GetState() // returns the string variable of the state // // Parameters: // None // // Pre: There must be a state for the student // // Post: the students state will be returned // // Returns: string with the state // // Called by: MediaIndex::Print() // Calls: None // string Student::GetState() { return State; } //////////////////////////////////////////////////////////////// Getzip() // returns the int variable of the zipcode // // Parameters: // None // // Pre: There must be a zipcode for the student // // Post: the students zipcode will be returned // // Returns: int with the zipcode // // Called by: MediaIndex::Print() // Calls: None // int Student::GetZip() { return ZipCode; } //////////////////////////////////////////////////////////////// GetOther() // returns the string variable of all other info // // Parameters: // None // // Pre: There must be other info for the student // // Post: the students other info will be returned // // Returns: string with the other info // // Called by: MediaIndex::Print() // Calls: None // string Student::GetOther() { return Other; } class ItemInfo { private: string Item; //stores the item name string MediaType; //stores the type of media string Author; //stores who the media is by string Owner; //stores the e-mail address of the owner int Dollars; //stores the dollar amount of the price int Cents; //stores the cents amount of the price string Avail; //stores the availability of the item public: ItemInfo(); //default constructor //adds a new item with the information inputted bool AddItem(string ItemName, string Type, string By, string EMail, int Dollar, int Cent, string Available); string GetItem(); //returns the item string GetType(); //returns the media type string GetAuthor(); //returns the author string GetOwner(); //returns the owner's e-mail int GetDollars(); //returns the dollars int GetCents(); //returns the cents string GetAvail(); //returns the availability }; //////////////////////////////////////////////////////////////// Item() // default constructor for item class // // Parameters: // // Pre: None // // Post: None // // Returns: None // // Called by: client code // Calls: None // ItemInfo::ItemInfo() { Item = "None"; MediaType = "None"; Author = "None"; Owner = "None"; Dollars = -1; Cents = -1; Avail = "None"; } //////////////////////////////////////////////////////////////// AddItem() // Adds an item by assigning the item info the inputted values from // the input file // // Parameters: // ItemName The name of the item // Type The item type // By who the item is by // EMail E-mail of the owner of the item // Dollar Dollar amount of price // Cent Cents amount of price // Available Availability of the item // // Pre: All the inputs files must have been opened correctly, all input // must have been read in correctly // // Post: The item info will have been added; // // Returns: true if added correctly, false if not added correctly // // Called by: MediaIndex::AddStudent() // Calls: None // bool ItemInfo::AddItem(string ItemName, string Type, string By, string EMail, int Dollar, int Cent, string Available) { Item = ItemName; MediaType = Type; Author = By; Owner = EMail; Dollars = Dollar; Cents = Cent; Avail = Available; return true; } //////////////////////////////////////////////////////////////// GetItem() // returns the string variable of the item // // Parameters: // None // // Pre: There must be an name for the item // // Post: the item name will be returned // // Returns: string with the item name // // Called by: MediaIndex::Print() // Calls: None // string ItemInfo::GetItem() { return Item; } //////////////////////////////////////////////////////////////// GetType() // returns the string variable of the item type // // Parameters: // None // // Pre: There must be a media type for the item // // Post: the item's media type will be returned // // Returns: string with the item's media type // // Called by: MediaIndex::Print() // Calls: None // string ItemInfo::GetType() { return MediaType; } //////////////////////////////////////////////////////////////// GetAuthor() // returns the string variable of author of the item // // Parameters: // None // // Pre: There must be an author for the item // // Post: the item's author will be returned // // Returns: string with the item's author // // Called by: MediaIndex::Print() // Calls: None // string ItemInfo::GetAuthor() { return Author; } //////////////////////////////////////////////////////////////// GetOwner() // returns the string variable e-mail of the owner of the item // // Parameters: // None // // Pre: There must be an e-mail for the owner of the item // // Post: the item's owner's e-mail will be returned // // Returns: string with the item's owner's e-mail // // Called by: MediaIndex::Print() // Calls: None // string ItemInfo::GetOwner() { return Owner; } //////////////////////////////////////////////////////////////// GetDollars() // returns the int variable of item's dollars // // Parameters: // None // // Pre: There must be a price for the item // // Post: the item's dollar amount will be returned // // Returns: int with the item's dollars // // Called by: MediaIndex::GetStudentE_Mail() // Calls: None // int ItemInfo::GetDollars() { return Dollars; } //////////////////////////////////////////////////////////////// GetCents() // returns the int variable of the item's cents // // Parameters: // None // // Pre: There must be a price for the item // // Post: the item's cents amount will be returned // // Returns: int with the item's cents amount // // Called by: MediaIndex::Print() // Calls: None // int ItemInfo::GetCents() { return Cents; } //////////////////////////////////////////////////////////////// GetAvail() // returns the string variable of the availability of the item // // Parameters: // None // // Pre: There must be an availability for the item // // Post: the item's availability will be returned // // Returns: string with the items availability // // Called by: MediaIndex::Print() // Calls: None // string ItemInfo::GetAvail() { return Avail; } class MediaIndex { private: Student Students[100]; //array to store student information ItemInfo Items[100]; //array to store Item information int StudentPos; //keeps track of position in Students array int ItemPos; //keeps track of position in Items array ifstream InStudents; //stream for inputting student info ifstream InItems; //stream for inputting item info ofstream IndexOut; //stream for outputting to file bool InputStudents; //true if the student data file has been read in bool InputItems; //true if the items data file had been read in public: MediaIndex(); //default constructor //adds a Student with the inputted information void AddStudent(string StudentName, int Area, int PhoneNum1, int PhoneNum2, string EMail, string Addy1, string Addy2, string AddyCity, string AddyState, int Zip, string OtherInfo); //adds an item with the inputted information void AddItem(string ItemName, string Type, string By, string EMail, int Dollar, int Cent, string Available); //removes a student and the items that the studnet owns void RemoveStudent(string EMail); //returns Student e-mail // string GetStudentEMail(); //returns Item owner // string GetItemOwner(); //Prints all student information void PrintStudents(string EMail, bool SingleStudent); //Prints all item information void PrintItems(string EMail, bool SingleStudent); //quits the program void Quit(); }; //////////////////////////////////////////////////////////////// MediaIndex() // default constructor for MediaIndex class // // Parameters: // // Pre: None // // Post: None // // Returns: None // // Called by: client code // Calls: None // MediaIndex::MediaIndex() { StudentPos = 0; ItemPos = 0; InStudents.open("students.data"); InItems.open("items.data"); IndexOut.open("output.data"); InputStudents = false; InputItems = false; } //////////////////////////////////////////////////////////////// AddStudent() // adds all the information for one student from the information passed // into the function, unless it is the first time running the function // then the function reads in the student information from the data file // // Parameters: // StudentName stores the name of the student // Area stores the area code of the student // PhoneNum1 stores the first part of the phone number // PhoneNum2 stores the second part of the phone number // EMail stores the e-mail of the student // Addy1 stores the first line of the address // Addy2 stores the second line of the address // AddyCity stores the city of the address // AddyState stores the state of the address // Zip stores the zipcode of the address // OtherInfo stores all the other info for the student // // Pre: The student must have an e-mail and all other student information // must be read in correctly // // Post: The student information will be added to the index // // Returns: None // // Called by: Main // Calls: Student::AddStudent() // void MediaIndex::AddStudent(string StudentName, int Area, int PhoneNum1, int PhoneNum2, string EMail, string Addy1, string Addy2, string AddyCity, string AddyState, int Zip, string OtherInfo) { string InfoType; //type of information being inputted string Name = "None"; //student name int AreaCode = -111; //student's area code int Phone1 = -111; //first part of students phone number int Phone2 = -1111; //second part of students phone number string E_Mail = "None"; //E-mail of student string Add1 = "None"; //first part of address string Add2 = "None"; //second part of address string City = "None"; //city of student string State = "None"; //state of student int ZipCode = -11111; //zip code of student string Other = "None"; //Other information provided by student bool Repeat = false; //bool to check to see if a student already exists //if the student data file has not been read in, then read it in and //store the information if (InputStudents == false) { InStudents.ignore(INT_MAX, '@'); getline(InStudents, InfoType, '\n'); while (InStudents) { //if the inputted type is the name of the student //then input it and store the name if (InfoType == "Name:") { getline(InStudents, Name, '\n'); } //if the inputted type is the phone number then input //it and ignore any data that is not a number and store //the inputted numbers accordingly else if (InfoType == "Phone Number:") { InStudents.ignore(INT_MAX, '('); InStudents>>AreaCode; InStudents.ignore(INT_MAX, ')'); InStudents>>Phone1; InStudents.ignore(INT_MAX, '-'); InStudents>>Phone2; InStudents.ignore(INT_MAX, '\n'); } //if the inputted type is the e-mail then input it //and store it as the e-mail else if (InfoType == "E-mail:") { getline(InStudents, E_Mail, '\n'); } //if the inputted type is the first part of the address //then input it else if (InfoType == "Address Line 1:") { getline(InStudents, Add1, '\n'); } //if the inputted type is the second part of the address //then input it else if (InfoType == "Address Line 2:") { getline(InStudents, Add2, '\n'); } //if the inputted type is the city then input it else if (InfoType == "City:") { getline(InStudents, City, '\n'); } //if the inputted type is the state then input it else if (InfoType == "State:") { getline(InStudents, State, '\n'); } //if the inputted type is the Zipcode then input it else if (InfoType == "Zipcode:") { InStudents>>ZipCode; InStudents.ignore(INT_MAX, '\n'); } //if the inputted type is other then input it else if (InfoType == "Other:") { getline(InStudents, Other, '\n'); } //if the inputted type is the @ then make sure an e-mail was inputted //and add all the info to the index else if (InfoType == "@") { if (E_Mail != "None") { //makes sure there is not already an e-mail address for that student //in the index for (int j = 0; j < 100; j++) { if (Students[j].GetE_Mail() == E_Mail) Repeat = true; } if (Repeat == false) { //double checks to make sure that no repeat has happen assert(Repeat == false); Students[StudentPos].AddStudent(Name, AreaCode, Phone1, Phone2, E_Mail, Add1, Add2, City, State, ZipCode, Other); //updates the Student postition to signify a student has been added //and sets StudentPos to the first empty place in the array for (int i = 0; i < 100; i++) { if (Students[i].GetE_Mail() == "None") { StudentPos = i; i = 101; } } } //resets all values back to default values Name = "None"; AreaCode = -111; Phone1 = -111; Phone2 = -1111; E_Mail = "None"; Add1 = "None"; Add2 = "None"; City = "None"; State = "None"; ZipCode = -11111; Other = "None"; } } //if the inputted type is not recognized then just ignore the line else { InStudents.ignore(INT_MAX, '\n'); } InStudents.ignore(INT_MAX, '@'); getline(InStudents, InfoType, '\n'); } InputStudents = true; //sets to true to mean that the student data has been //inputted } //if inputstudents is true then add the student information that was //passed to the function else { IndexOut<<"add student"<>Dollars; InItems.ignore(INT_MAX, '.'); InItems>>Cents; InItems.ignore(INT_MAX, '\n'); } //if the inputted type is the the availability of the item //then input it else if (InfoType == "Availability:") { getline(InItems, Avail, '\n'); } //if the inputted type is the @ then make sure an owner was inputted //and that the owner is in the student info then add all //the info to the index else if (InfoType == "@") { if (Owner != "None") { for (int j = 0; j < 100; j++) { if (Students[j].GetE_Mail() == Owner) { Items[ItemPos].AddItem(Name, MediaType, Author, Owner, Dollars, Cents, Avail); //updates the Item postition to signify an item has //been added and sets ItemPos to the first empty place //in the array for (int i = 0; i < 100; i++) { if (Items[i].GetOwner() == "None") { ItemPos = i; i = 101; } } } } } //resets all values back to default values Name = "None"; MediaType = "None"; Author = "None"; Owner = "None"; Dollars = -1; Cents = -1; Avail = "None"; } //if the inputted type is not recognized then just ignore the line else { InItems.ignore(INT_MAX, '\n'); } InItems.ignore(INT_MAX, '@'); getline(InItems, InfoType, '\n'); } InputItems = true; //sets to true to mean that the student data has been //inputted } //if inputstudents is true then add the student information that was //passed to the function else { IndexOut<<"add item"<= 0) && (i < 100)); //checks to make sure item has an owner before outputting if printing all //items, if printing single students items, then check for when their item //is in the index if ((Items[i].GetOwner() != "None" && SingleStudent == false) || (Items[i].GetOwner() == EMail && SingleStudent == true)) { //checks to make sure Item has a name, if so output it if (Items[i].GetItem() != "None") { IndexOut<<"Item:"<>Commands; //inputs while the commands file is still open and there is data left to //read in while (InCommands) { //checks if the command is print, if so then it inputs the type of print //to happen if (Commands == "print") { //inputs the type of print to happen InCommands>>PrintType; //if print type is all students, then call the print function //to print all students if (PrintType == "students") Multimedia.PrintStudents("None", false); //if print type is all items, then call the print function //to print all items else if (PrintType == "items") Multimedia.PrintItems("None", false); //for all others call print function for finding one student //and their items else { Multimedia.PrintStudents(PrintType, true); Multimedia.PrintItems(PrintType, true); } //ignores the blank line after the command InCommands.ignore(INT_MAX, '\n'); } //checks if command is to add something to the index, either //a student of an item else if (Commands == "add") { //gets the type of adding to happen, a student or item InCommands>>AddType; //if add type is to add an item, then input all item //information and add it to the index if (AddType == "item") { //sets all info back to default values ItemName = "None"; MediaType = "None"; Author = "None"; Owner = "None"; Dollars = -1; Cents = -1; Avail = "None"; //ignore up to the @ symbol InCommands.ignore(INT_MAX, '@'); getline(InCommands, AddType, '\n'); //runs through the inputting of the item information while (AddType != "@") { //if the addtype is Item, then input the item name if (AddType == "Item:") { getline(InCommands, ItemName, '\n'); } //if the addtype is Media Type, then input the item media type if (AddType == "Media Type:") { getline(InCommands, MediaType, '\n'); } //if the addtype is by, then input item author if (AddType == "by:") { getline(InCommands, Author, '\n'); } //if the addtype is the owner, then input the owner of the item if (AddType == "Owner:") { getline(InCommands, Owner, '\n'); } //if the add type is the price, then input the dollars and cents if (AddType == "Price:") { InCommands.ignore(INT_MAX, '$'); InCommands>>Dollars; InCommands.ignore(INT_MAX, '.'); InCommands>>Cents; InCommands.ignore(INT_MAX, '\n'); } //if the add type is the availability, then input it if (AddType == "Availability:") { getline(InCommands, Avail, '\n'); } //ignore up to the @ symbol InCommands.ignore(INT_MAX, '@'); getline(InCommands, AddType, '\n'); } //adds the item to the index Multimedia.AddItem(ItemName, MediaType, Author, Owner, Dollars, Cents, Avail); } else if (AddType == "student") { //sets all information back to it's default value Name = "None"; AreaCode = -111; Phone1 = -111; Phone2 = -1111; E_Mail = "None"; Add1 = "None"; Add2 = "None"; City = "None"; State = "None"; ZipCode = -11111; Other = "None"; //ignore up to the @ symbol InCommands.ignore(INT_MAX, '@'); getline(InCommands, AddType, '\n'); while (AddType != "@") { //if the inputted type is the name of the student //then input it and store the name if (AddType == "Name:") { getline(InCommands, Name, '\n'); } //if the inputted type is the phone number then input //it and ignore any data that is not a number and store //the inputted numbers accordingly else if (AddType == "Phone Number:") { InCommands.ignore(INT_MAX, '('); InCommands>>AreaCode; InCommands.ignore(INT_MAX, ')'); InCommands>>Phone1; InCommands.ignore(INT_MAX, '-'); InCommands>>Phone2; InCommands.ignore(INT_MAX, '\n'); } //if the inputted type is the e-mail then input it //and store it as the e-mail else if (AddType == "E-mail:") { getline(InCommands, E_Mail, '\n'); } //if the inputted type is the first part of the address //then input it else if (AddType == "Address Line 1:") { getline(InCommands, Add1, '\n'); } //if the inputted type is the second part of the address //then input it else if (AddType == "Address Line 2:") { getline(InCommands, Add2, '\n'); } //if the inputted type is the city then input it else if (AddType == "City:") { getline(InCommands, City, '\n'); } //if the inputted type is the state then input it else if (AddType == "State:") { getline(InCommands, State, '\n'); } //if the inputted type is the Zipcode then input it else if (AddType == "Zipcode:") { InCommands>>ZipCode; InCommands.ignore(INT_MAX, '\n'); } //if the inputted type is other then input it else if (AddType == "Other:") { getline(InCommands, Other, '\n'); } //ignore up to the @ symbol InCommands.ignore(INT_MAX, '@'); getline(InCommands, AddType, '\n'); } Multimedia.AddStudent(Name, AreaCode, Phone1, Phone2, E_Mail, Add1, Add2, City, State, ZipCode, Other); } //ignores the blank line InCommands.ignore(INT_MAX, '\n'); } else if (Commands == "delete") { //gets the e-mail address of the student to be removed InCommands>>E_Mail; //getline(InCommands, E_Mail, '\n'); //calls the function to remove the student Multimedia.RemoveStudent(E_Mail); //ignores the blank line InCommands.ignore(INT_MAX, '\n'); } else if (Commands == "quit") { //calls the quit function Multimedia.Quit(); //close the commands file to quit the program InCommands.close(); } //inputs the first command InCommands>>Commands; } return 0; }