#include "Catalog.h" #include #include using std::istringstream; using std::getline; const char* STUDENTS_FILE = "students.data"; const char* ITEMS_FILE = "items.data"; const char* COMMANDS_FILE = "commands.data"; const char* OUTPUT_FILE = "output.data"; Catalog::Catalog() : out(OUTPUT_FILE) { // Make sure the output file opened assert (!out.fail()); // Read the students in ifstream studentsFile(STUDENTS_FILE); assert (!studentsFile.fail()); readStudents(studentsFile); studentsFile.close(); // Read the items in ifstream itemsFile(ITEMS_FILE); assert (!itemsFile.fail()); readItems(itemsFile); itemsFile.close(); // Read and execute commands ifstream commandsFile(COMMANDS_FILE); assert (!commandsFile.fail()); readCommands(commandsFile); commandsFile.close(); } // Reads a student from an input stream void Catalog::readStudents(istream& in) { while (in) { Student s; if (readStudent(in, s)) { addStudent(s, false); } else break; } } // Reads a series of items from an input stream void Catalog::readItems(istream& in) { while (in) { Item i; if (readItem(in, i)) { addItem(i, false); } else break; } } // Reads a command from an input stream and executes it void Catalog::readCommands(istream& in) { while (in) { string line, command, type; in >> std::ws; getline(in, line); if (!in) return; istringstream i(line); i >> std::ws >> command >> std::ws >> type; out << line << std::endl; if (command == "print") { if (type == "students") { // Print all students printStudents(); } else if (type == "items") { // Print all items printItems(); } else { // Print that student printStudent(type); } } else if (command == "add") { if (type == "item") { // Read an item Item item; if (readItem(in, item)) { // Add it addItem(item); } else { out << "Failure" << endl; } } else if (type == "student") { // Read a student Student student; if (readStudent(in, student)) { // Add it addStudent(student); } else { out << "Failure" << endl; } } } else if (command == "delete") { deleteStudent(type); } else if (command == "quit") { return; } } } // Reads in a student's information from an input stream bool Catalog::readStudent(istream& in, Student& student) { while (in) { // Read a field name string field, data; getline(in, field); getline(in, data); if (field == "@Name:") { student.setName(data); } else if (field == "@Phone Number:") { student.setPhone(data); } else if (field == "@E-mail:") { student.setEmail(data); } else if (field == "@Address Line 1:") { student.setStreet(data); } else if (field == "@Address Line 2:") { student.setApt(data); } else if (field == "@City:") { student.setCity(data); } else if (field == "@State:") { student.setState(data); } else if (field == "@Zipcode:") { student.setZip(data); } else if (field == "@Other:") { student.setOther(data); } else { if (student.getEmail() == "") { return false; } else { student.setActive(true); return true; } } } return false; } // Reads an item's information from an input stream bool Catalog::readItem(istream& in, Item& item) { while (in) { // Read a field name string field, data; getline(in, field); getline(in, data); if (field == "@Item:") { item.setName(data); } else if (field == "@Media Type:") { item.setType(data); } else if (field == "@by:") { item.setAuthor(data); } else if (field == "@Owner:") { item.setOwner(data); } else if (field == "@Price:") { item.setPrice(data); } else if (field == "@Availability:") { Availability a = NONE; for (int i = 0; i < 5; i++) { if (data == AVAILABILITIES[i]) { a = Availability(i); break; } } item.setAvailability(a); } else { if (item.getOwner() == "") { return false; } else { item.setActive(true); return true; } } } return false; } // Returns an index where the selected student is located, or // -1 if student does not exist int Catalog::findStudent(string email) { for (int i = 0; i < 100; i++) { if (students[i].isActive() && students[i].getEmail() == email) { return i; } } return -1; } // Returns the first item belonging to the student with the given email address // at or following the given index, or -1 if no more items exist int Catalog::findItem(string email, int start) { if (start < 0 || start >= 100) return -1; for (int i = start; i < 100; i++) { if (items[i].isActive() && items[i].getOwner() == email) { return i; } } return -1; } // Prints all students void Catalog::printStudents() { for (int i = 0; i < 100; i++) { if (students[i].isActive()) { students[i].print(out); } } } // Prints all items void Catalog::printItems() { for (int i = 0; i < 100; i++) { if (items[i].isActive()) { items[i].print(out); } } } // Prints student information and all items belonging to it void Catalog::printStudent(string email) { // Try to find the student int index = findStudent(email); if (index == -1) { out << email << " not found." << endl; return; } students[index].print(out); // Print items belonging to the student index = -1; while ((index = findItem(email, index+1)) != -1) { items[index].print(out, false); } } // Adds an item void Catalog::addItem(const Item& item, bool log) { // Make sure the owner exists if (findStudent(item.getOwner()) == -1) { if (log) out << "Failure" << endl; return; } // Try to find the first unused item space int i; for (i = 0; i < 100; i++) { if (items[i].isActive() == false) break; } if (i == 100) { if (log) out << "Failure" << endl; return; } // Copy the item into the space items[i] = item; if (log) out << "Success" << endl; } // Adds a student void Catalog::addStudent(const Student& student, bool log) { // Make sure the student doesn't already exist if (findStudent(student.getEmail()) != -1) { if (log) out << "Failure" << endl; return; } // Try to find the first unused student space int i; for (i = 0; i < 100; i++) { if (students[i].isActive() == false) break; } if (i == 100) { if (log) out << "Failure" << endl; return; } // Copy the student into the space students[i] = student; if (log) out << "Success" << endl; } // Deletes a student's information and all items associated with it void Catalog::deleteStudent(string email) { // Try to find the student int index = findStudent(email); if (index == -1) { out << "Failure" << endl; return; } // Delete the student students[index].setActive(false); // Try to find any items belonging to this student index = -1; while ((index = findItem(email, index+1)) != -1) { // Delete the item items[index].setActive(false); } out << "Success" << endl; }