// The implemement of the database class #include"database.h" #include #include #include using namespace std; ///////////////////////database() // the constructor // // Parameters: // // Pre: // // Post: database object is created // // Returns: // // Called by: // Calls: database::database() { // Open the filestreams ifstream sin("students.data"); // student input stream ifstream min("items.data"); // media input stream // Initialize the counter variables counter1 = 0, counter2 = 0; // Blank class objects student blank; media empty; // Create blank objects for(int i = 0; i < 100; i++) { people[i] = blank; library[i] = empty; } // Declare and initialize variables string input, // what to read in name, // name of the student or media or author of the media email, // email of the student street, // the street address apartment, // address line 2 city, // the city state, // the state option, // other info and field type; // media type int result, // results from turning strings into ints area, // the area code first, // first three digits and the dollars second, // last four digits and the cents zipcode; // the zipcode char junk, // junk next; // the next character in the input file // Read in and set the information for the students next = sin.peek(); // peek at the student input stream while(sin) { while(next == '\n') { sin.ignore(100, '\n'); next = sin.peek(); } if(next == '@') { getline(sin, input, '\n'); // read in the item to be added } result = switch_strings(input); switch (result) { // Set the student's name case 0: getline(sin, name, '\n'); people[counter1].set_name(name); break; // Set the student's email case 1: getline(sin, email, '\n'); people[counter1].set_email(email); break; // Set the street address case 2: getline(sin, street, '\n'); people[counter1].set_street_name(street); break; // Set address line 2 case 3: getline(sin, apartment, '\n'); people[counter1].set_apt(apartment); break; // Set the city case 4: getline(sin, city, '\n'); people[counter1].set_city(city); break; // Set the state case 5: sin >> ws >> state >> ws; people[counter1].set_state(state); break; // Set the zipcode case 6: sin >> ws >> zipcode >> ws; people[counter1].set_zip(zipcode); break; // Set Phone number case 7: sin >> ws >> junk >> ws >> area >> ws >> junk >> ws >> first >> ws >> junk >> ws >> second >> ws; people[counter1].set_phone(area, first, second); break; // Set the other information case 8: getline(sin, option, '\n'); people[counter1].set_info(option); break; case 9: //cout << people[counter1] << endl; counter1++; break; // Default case if input field wasn't found default: cout << "Invalid field. Try again" << endl; break; }; // Seconadry read next = sin.peek(); } cout << counter1 << endl; // Read in the information for the media items and set them in the array next = min.peek(); while(min) { while(next == '\n') { min.ignore(100, '\n'); next = min.peek(); } if( next == '@') { getline(min, input, '\n'); // read in the item to be added } result = media_strings(input); // switch the strings to ints for the switch statement switch (result) { // Set item's name case 0: getline(min, name, '\n'); library[counter2].set_item(name); break; // Set the media type case 1: getline(min, type, '\n'); library[counter2].set_type(type); break; // Set the author of the media case 2: getline(min, name, '\n'); library[counter2].set_author(name); break; // Set the owner of the media(student's email) case 3: getline(min, email, '\n'); library[counter2].set_owner(email); break; // Set the field case 4: getline(min, option, '\n'); library[counter2].set_field(option); break; // Set the dollars and cents case 5: min >> ws >> junk >> ws >> first >> ws >> junk >> ws >> second >> ws; library[counter2].set_price(first, second); break; case 6: //library[counter2].print(cout, "Owber"); counter2++; break; // Default case if input field is invalid default: cout << "Invalid field. TRY again." << endl; break; }; // Secondary read next = min.peek(); } sin.close(); min.close(); } ///////////////////////get_media() // gets the private data at the specified array index // // Parameters: position - the array index // // Pre: database object is created // // Post: // // Returns: library[position] // // Called by: // Calls: media database::get_media(int position) { return library[position]; } ///////////////////////get_student() // gets the private member at the specified array indes // // Parameters: position - the array index // // Pre: database object is created // // Post: // // Returns: people[position] // // Called by: // Calls: student database::get_student(int position) { return people[position]; } ///////////////////////switch_streams() // turns strings into ints // // Parameters: input - the string to change into an int // // Pre: input is created and initailized // // Post: // // Returns: an int // // Called by: database(), add_student() // Calls: int database::switch_strings(string input) { if(input == "@Name:") { return 0; } else if(input == "@E-mail:") { return 1; } else if(input == "@Address Line 1:") { return 2; } else if(input == "@Address Line 2:") { return 3; } else if(input == "@City:") { return 4; } else if(input == "@State:") { return 5; } else if(input == "@Zipcode:") { return 6; } else if(input == "@Phone Number:") { return 7; } else if(input == "@Other:") { return 8; } else if(input == "@@") { return 9; } else { return 10; } } ///////////////////////media_streams() // turns strings into ints // // Parameters: input - the string to change into an int // // Pre: input is created and initailized // // Post: // // Returns: an int // // Called by: database(), add_media() // Calls: int database::media_strings(string input) { if(input == "@Item:") { return 0; } else if(input == "@Media Type:") { return 1; } else if(input == "@by:") { return 2; } else if(input == "@Owner:") { return 3; } else if(input == "@Availability:") { return 4; } else if(input == "@Price:") { return 5; } else if(input == "@@") { return 6; } else { return 7; } } ///////////////////////add_student() // adds a student to the people array // // Parameters: in - the input stream // // Pre: input stream is created and opened // // Post: a stdent is added to the array // // Returns: true if added/ false if otherwise // // Called by: amain() // Calls: media_streams(), get_owner() bool database::add_student(ifstream& in) { // Declare and initialize local variables string email, // the email of the student name, // the name of the student street, // the street address apartment, // address line 2 city, // the city state, // the state option, // other info and field input, // what is to be input next; // the next char in the input stream int result, // results from turning strings into ints area, // the area code first, // first three digits and the dollars second, // last four digits and the cents location; // where to put the student char junk; // junk student empty; // empty student class object // Find an empty spot in the array for(int i = 0; i < 100; i++) { if(people[i].get_email() == "") { location = i; break; } } // Priming read getline(in, next, '\n'); while(next != "@@") { result = switch_strings(next); switch (result) { // Set the student's name case 0: getline(in, name, '\n'); people[location].set_name(name); break; // Set the student's email case 1: getline(in, email, '\n'); people[location].set_email(email); break; // Set the street address case 2: getline(in, street, '\n'); people[location].set_street_name(street); break; // Set address line 2 case 3: getline(in, apartment, '\n'); people[location].set_apt(apartment); break; // Set the city case 4: getline(in, city, '\n'); people[location].set_city(city); break; // Set the state case 5: in >> ws >> state; people[location].set_state(state); break; // Set the zipcode case 6: in >> ws >> area >> ws; people[location].set_zip(area); break; // Set Phone number case 7: in >> ws >> junk >> ws >> area >> ws >> junk >> ws >> first >> ws >> junk >> ws >> second >> ws; people[location].set_phone(area, first, second); break; // Set the other information case 8: getline(in, option, '\n'); people[location].set_info(option); break; }; // Secondary read getline(in, next, '\n'); } // If it wasn't a valid entry if(people[location].get_email() != "") { counter1++; return true; } else { people[location] = empty; return false; } assert(counter1 < 100); } ///////////////////////add_media() // adds a media item to the library array // // Parameters: in - the input stream // // Pre: input stream is created and opened // // Post: a media item is added to the array // // Returns: true if added/ false if otherwise // // Called by: main() // Calls: switch_streams(), get_email() bool database::add_media(ifstream& in) { // Declare and initailize variables int location, // where to add the item result, // result of testing the input first, // dollars of the price second; // cents of the price char junk; string name, // the item's name or the author type, // the type of item email, // the email of the Owner option, // the availability of the item next; // the next thing to be input media empty; // empty media item // Find an empty spot to place the item in for(int i = 0; i < 100; i++) { if(library[i].get_owner() == "") { location = i; break; } } // Priming read getline(in, next, '\n'); while(next != "@@") { result = media_strings(next); switch (result) { // Set item's name case 0: getline(in, name, '\n'); library[location].set_item(name); break; // Set the media type case 1: getline(in, type, '\n'); library[location].set_type(type); break; // Set the author of the media case 2: getline(in, name, '\n'); library[location].set_author(name); break; // Set the owner of the media(student's email) case 3: getline(in, email, '\n'); library[location].set_owner(email); break; // Set the field case 4: getline(in, option, '\n'); library[location].set_field(option); break; // Set the dollars and cents case 5: in >> ws >> junk >> ws >> first >> ws >> junk >> ws >> second; library[location].set_price(first, second); break; }; // Secondary read getline(in, next, '\n'); } bool valid = false; // determinds if it is a valid entry for(int i = 0; i < 100; i++) { if(people[i].get_email() == library[location].get_owner()) { valid = true; break; } } // If it was an invalid entry if((library[location].get_owner() != "") && (valid == true)) { counter2++; return true; } else { library[location] = empty; return false; } } ///////////////////////print() // prints out the arrays // // Parameters: what - determines the what to print // out - the output stream // // Pre: output stream is created and opened // // Post: prints information to output file // // Returns: // // Called by: main() // Calls: get_email(), print(), get_owner() void database::print(string what, ofstream& out) { //How to print // Print out all students if(what == "students") { // Print out the students in order for(int i = 0; i < 100; i++) { // Make sure there are no blank students if(people[i].get_email() != "") { out << people[i] << endl; } } } // Print out all items else if(what == "items") { // Print out the items in order for(int i = 0; i < 100; i++) { // Make sure there are no blank items if(library[i].get_owner() != "") { string how = "Owner"; library[i].print(out, how); } } } else { int location = 0; // used to determine if the student is in array // Print a specific student and their items for(int j = 0; j < 100; j++) { if(people[j].get_email() == what) { location= j; break; } } if(location == 0) { out << what << " not found." << endl; } else { // Print the student's info out << people[location] << endl; for(int k = 0; k < 100; k++) { // Find any matching media items if(library[k].get_owner() == people[location].get_email()) { // Print out the items library[k].print(out, "noOwn"); } } } } } ///////////////////////delete_entery() // deletes a student and all media associated with them // // Parameters: Student - the email of who to delete // // Pre: arrays are created and student is in them // // Post: deletes a student from the people array and items from the library array // // Returns: true if successful/ false otherwise // // Called by: main() // Calls: get_email(), get_owner() bool database::delete_entry(string Student) { // Initailize variables int location, // location the student array deleted = 0; // number of items deleted student remove; // the student to remove bool found = false; // test to make sure student is in the array // Find the location in the array of the student to be deleted for(int i = 0; i < 100; i++) { if(people[i].get_email() == Student) { found = true; location = i; break; } } if(found == true) { // Delete the student counter1--; people[location] = remove; // Find all items that have the deleted student's email for(int i = 0; i < 100; i++) { if(library[i].get_owner() == Student) { media Remove; // make a default blank media object library[i] = Remove; // remove the item from the database deleted++; } } counter2 = counter2 - deleted; // correct the number of items return true; } else { return false; } }