Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // Book Project
- //
- // Created by Isaac Harris on 4/19/17.
- // Copyright © 2017 Isaac Harris. All rights reserved.
- //
- #include <sstream>
- #include <fstream>
- #include <iostream>
- #include <vector>
- using namespace std;
- class Book
- {
- private:
- string title;
- string authorLastName;
- string authorFirstName;
- string authorMiddleInitial;
- string ISBN;
- string price;
- string publisher;
- string publicationDate;
- int pages;
- int inventoryCount;
- public:
- string getTitle();
- string getAuthorLastName();
- string getAuthorFirstName();
- string getISBN();
- string getPrice();
- string getPublisher();
- string getPublicationDate();
- int getPages();
- int getInventoryCount();
- void adjustInventoryCount();
- Book(string);
- };
- Book::Book(string rowData)
- {
- istringstream columns(rowData);
- string column;
- int counter = 0;
- while (getline(columns, column, ',')) {
- switch (counter) {
- case 0:
- title = column;
- break;
- case 1:
- authorLastName = column;
- break;
- case 2:
- authorFirstName = column;
- break;
- case 3:
- authorMiddleInitial = column;
- break;
- case 4:
- ISBN = column;
- break;
- case 5:
- price = column;
- break;
- case 6:
- publisher = column;
- break;
- case 7:
- publicationDate = column;
- break;
- case 8:
- pages = stoi(column);
- break;
- case 9:
- inventoryCount = stoi(column);
- break;
- }
- counter++;
- }
- }
- string Book::getTitle()
- {
- return title;
- }
- string Book::getAuthorLastName()
- {
- return authorLastName;
- }
- string Book::getAuthorFirstName()
- {
- return authorFirstName;
- }
- string Book::getISBN()
- {
- return ISBN;
- }
- int Book::getInventoryCount()
- {
- return inventoryCount;
- }
- void Book::adjustInventoryCount()
- {
- int adjustment;
- cout << "Enter the amount you would like to adjust the inventory count by: "; cin >> adjustment;
- inventoryCount += adjustment;
- }
- string promptISBN()
- {
- string userISBN;
- cout << "Please input ISBN number here: ";
- cin >> userISBN;
- return userISBN;
- }
- void ISBNerror()
- {
- cout << "Sorry, that ISBN number does not exist in our database. You will now be returned to the menu options.\n\n";
- }
- int main()
- {
- ifstream inputFile;
- //inputFile.open("c:\\temp\\books.txt", ios::in);
- inputFile.open("/Users/isaacharris/Desktop/books.txt");
- if (!inputFile.is_open())
- {
- cout << "The file could not be opened or found." << endl;
- system("pause");
- return 1;
- }
- vector<Book> books;
- string rowData;
- // This will return the header row--to be discarded.
- getline(inputFile, rowData);
- while (getline(inputFile, rowData))
- {
- Book newBook = Book(rowData);
- books.push_back(newBook);
- }
- inputFile.close();
- string menuOption;
- while (menuOption != "4")
- {
- cout << "1) Display book details" << endl;
- cout << "2) Adjust inventory counts" << endl;
- cout << "3) Display inventory" << endl;
- cout << "4) Quit" << "\n\n";
- cout << "Type the number of the menu option you would like to execute: "; cin >> menuOption;
- string userISBN;
- if (menuOption == "1")
- {
- userISBN = promptISBN();
- for (long i = 0; i < books.size(); i++)
- {
- if (userISBN == books[i].getISBN())
- {
- cout << endl << "Title: " << books[i].getTitle() << endl;
- cout << "Author: " << books[i].getAuthorLastName() << ", " << books[i].getAuthorFirstName() << "\n\n";
- }
- }
- int counter = 0;
- for (long i = 0; i < books.size(); i++)
- {
- if (userISBN != books[i].getISBN())
- {
- counter++;
- }
- }
- if (counter++ == books.size())
- {
- ISBNerror();
- }
- }
- else if (menuOption == "2")
- {
- userISBN = promptISBN();
- for (long i = 0; i < books.size(); i++)
- {
- if (userISBN == books[i].getISBN())
- {
- books[i].getInventoryCount();
- cout << endl << "Current inventory count: " << books[i].getInventoryCount() << "\n";
- books[i].adjustInventoryCount();
- cout << endl;
- }
- }
- int counter = 0;
- for (long i = 0; i < books.size(); i++)
- {
- if (userISBN != books[i].getISBN())
- {
- counter++;
- }
- }
- if (counter++ == books.size())
- {
- ISBNerror();
- }
- }
- else if (menuOption == "3")
- {
- userISBN = promptISBN();
- for (long i = 0; i < books.size(); i++)
- {
- if (userISBN == books[i].getISBN())
- {
- cout << endl << "Current inventory count: " << books[i].getInventoryCount() << "\n\n";
- }
- }
- int counter = 0;
- for (long i = 0; i < books.size(); i++)
- {
- if (userISBN != books[i].getISBN())
- {
- counter++;
- }
- }
- if (counter++ == books.size())
- {
- ISBNerror();
- }
- }
- else if (menuOption == "4")
- {
- cout << "Exiting program\n\n";
- }
- else
- {
- cout << "Please type in a number that MATCHES one of the menu options.\n\n";
- }
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement