Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Lab 3 Wilson, Drayden T Th
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <algorithm>
- using namespace std;
- enum MenuOption { INVALID = 0, PRINT, SEARCH, ADD, DELETE, EXIT };
- class Address
- {
- public:
- Address();
- void SetAddressFromFile(ifstream& inFile);
- void SetAddressFromUser(); // has user prompts
- void PrintToFile(ofstream& outFile) const;
- void PrintToScreen() const;
- string GetNameLast() const { return nameLast; }
- string GetPhoneNumber() const { return phoneNumber; }
- private:
- string nameFirst;
- string nameLast;
- int buildingNumber;
- string street;
- string city;
- string state;
- int zipcode;
- string phoneNumber;
- void CapitalizeMembers();
- };
- class Record
- {
- public:
- Record() : next(nullptr) {} // data = new Address; is this needed in {}?
- //~Record() { delete next; cout << "killing record in record\n"; /*@debug@*/}
- Address data;
- Record *next;
- };
- class AddressBook
- {
- public:
- AddressBook() : head(nullptr), tail(nullptr) {}
- ~AddressBook();
- void FillBookFromFile();
- void AppendRecord(const Address& newData);
- void RemoveRecordAfter(Record *curRecord);
- void PrintBookToFile() const;
- Record *SearchBook(string searchQuery) const;
- private:
- Record *head;
- Record *tail;
- };
- void Menu(AddressBook recordList);
- int main()
- {
- AddressBook losAngeles;
- losAngeles.FillBookFromFile();
- // Record *dummy = losAngeles.SearchBook("Moe"); // @debug@
- Menu(losAngeles);
- return 0;
- }
- void Menu(AddressBook recordList)
- {
- int userMenuSelection = 0;
- Record *searchRecord;
- Address addressToAppend;
- string searchQuery;
- do
- {
- cout << "\n[1] Print to file\n"
- << "[2] Search\n"
- << "[3] Add Entry\n"
- << "[4] Delete Entry\n"
- << "[5] Exit\n";
- if (userMenuSelection)
- {
- cout << "Select another option: ";
- }
- else
- {
- cout << "Select an option by entering its corresponding number: ";
- }
- cin >> userMenuSelection;
- switch (userMenuSelection)
- {
- case PRINT:
- cout << "Printing address book to output file.\n";
- recordList.PrintBookToFile();
- break;
- case SEARCH:
- cout << "Enter a last name or phone number to search by: ";
- cin >> searchQuery;
- searchRecord = recordList.SearchBook(searchQuery);
- if(searchRecord == nullptr)
- {
- cout << "Record not found.\n";
- }
- else
- {
- cout << "Record found.\n";
- searchRecord->next->data.PrintToScreen();
- }
- break;
- case ADD:
- cout << "Enter information for new record.\n";
- addressToAppend.SetAddressFromUser();
- recordList.AppendRecord(addressToAppend);
- cout << "Record Added.\n";
- break;
- case DELETE:
- cout << "Enter the last name or phone number of the record you want to delete: ";
- cin >> searchQuery;
- searchRecord = recordList.SearchBook(searchQuery);
- if(searchRecord == nullptr)
- {
- cout << "Record not found.\n";
- }
- else
- {
- cout << "Record found. Deleting record.\n";
- recordList.RemoveRecordAfter(searchRecord);
- }
- break;
- case EXIT:
- cout << "Exiting the program.\n\n";
- break;
- default:
- cout << " Invalid menu option selected.";
- userMenuSelection = 1; // 1 == !INVALID
- break;
- }
- } while (userMenuSelection != 5);
- }
- Address::Address()
- {
- nameFirst = "NoFirst";
- nameLast = "NoLast";
- buildingNumber = -1;
- street = "NoStreet";
- city = "NoCity";
- state = "NoState";
- zipcode = -1;
- phoneNumber = -1;
- }
- void Address::SetAddressFromFile(ifstream& inFile)
- {
- inFile >> nameFirst >> nameLast >> buildingNumber >> street >> city >> state >> zipcode >> phoneNumber;
- CapitalizeMembers();
- }
- void Address::SetAddressFromUser()
- {
- cout << "First Name: ";
- cin >> nameFirst;
- cout << "Last Name: ";
- cin >> nameLast;
- cout << "Building Number: ";
- cin >> buildingNumber;
- cout << "Street: ";
- cin >> street;
- cout << "City: ";
- cin >> city;
- cout << "State: ";
- cin >> state;
- cout << "Zipcode: ";
- cin >> zipcode;
- cout << "Phone Number: ";
- cin >> phoneNumber;
- CapitalizeMembers();
- }
- void Address::PrintToFile(ofstream& outFile) const
- {
- outFile << nameFirst << ' ' << nameLast << "\t\t"
- << buildingNumber << ' ' << street << ' ' << city << ' ' << state << ' ' << zipcode << "\t\t"
- << '(' << phoneNumber.substr(0, 3) << ") "
- << phoneNumber.substr(3, 3) << ' ' << phoneNumber.substr(6, 4) << '\n';
- }
- void Address::PrintToScreen() const
- {
- cout << nameFirst << ' ' << nameLast << '\t'
- << buildingNumber << ' ' << street << ' ' << city << ' ' << state << ' ' << zipcode << '\t'
- << '(' << phoneNumber.substr(0, 3) << ") "
- << phoneNumber.substr(3, 3) << ' ' << phoneNumber.substr(6, 4);
- }
- void Address::CapitalizeMembers()
- {
- transform(nameFirst.begin(), nameFirst.end(), nameFirst.begin(), ::toupper);
- transform(nameLast.begin(), nameLast.end(), nameLast.begin(), ::toupper);
- transform(street.begin(), street.end(), street.begin(), ::toupper);
- transform(city.begin(), city.end(), city.begin(), ::toupper);
- transform(state.begin(), state.end(), state.begin(), ::toupper);
- }
- AddressBook::~AddressBook()
- {
- while(head)
- {
- Record *nextRecord = head->next;
- delete head;
- head = nextRecord;
- cout << "killing record in book\n"; /*@debug@*/
- }
- }
- void AddressBook::FillBookFromFile()
- {
- ifstream inFile;
- Address curAddress;
- inFile.open("input.txt");
- if (!inFile)
- {
- cout << "Failed to open input file. Exiting Program.\n";
- exit(1);
- }
- else
- {
- cout << "Input file read successfully.\n";
- }
- if(inFile.peek() == EOF)
- {
- cout << "Input file is empty. Exiting Program.\n";
- exit(1);
- }
- while(inFile.peek() != EOF)
- {
- curAddress.SetAddressFromFile(inFile);
- AppendRecord(curAddress);
- }
- inFile.close();
- }
- void AddressBook::AppendRecord(const Address& newData)
- {
- Record *newRecord = new Record;
- newRecord->data = newData;
- newRecord->next = nullptr;
- if (head == nullptr)
- {
- head = newRecord;
- tail = newRecord;
- }
- else
- {
- tail->next = newRecord;
- tail = newRecord;
- }
- }
- void AddressBook::RemoveRecordAfter(Record *curRecord)
- {
- Record *sucRecord;
- if(curRecord == nullptr && head != nullptr) // removing head
- {
- sucRecord = head->next;
- delete head;
- head = sucRecord;
- if(sucRecord == nullptr) // removed last record/tail
- {
- tail = nullptr;
- }
- }
- else if(curRecord->next != nullptr)
- {
- sucRecord = curRecord->next->next;
- delete curRecord->next;
- curRecord->next = sucRecord;
- if(sucRecord == nullptr) // removed tail
- {
- tail = curRecord;
- }
- }
- }
- void AddressBook::PrintBookToFile() const
- {
- ofstream outFile;
- Record *curRecord = head; // does this work?
- outFile.open("output.txt");
- while(curRecord != nullptr)
- {
- curRecord->data.PrintToFile(outFile);
- curRecord = curRecord->next;
- }
- outFile.close();
- }
- Record *AddressBook::SearchBook(string searchQuery) const
- {
- Record *curRecord = nullptr;
- curRecord = head;
- string curItem;
- while(curRecord != nullptr)
- {
- if (isdigit(searchQuery[0])) // search item is a phone number
- {
- curItem = curRecord->data.GetPhoneNumber();
- }
- else // search item is a last name
- {
- curItem = curRecord->data.GetNameLast();
- transform(searchQuery.begin(), searchQuery.end(), searchQuery.begin(), ::toupper);
- }
- if(curItem == searchQuery)
- {
- return curRecord;
- }
- curRecord = curRecord->next;
- }
- return nullptr; // record not found
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement