Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.34 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  Book Project
  4. //
  5. //  Created by Isaac  Harris on 4/19/17.
  6. //  Copyright © 2017 Isaac  Harris. All rights reserved.
  7. //
  8.  
  9.  
  10. #include <sstream>
  11. #include <fstream>
  12. #include <iostream>
  13. #include <vector>
  14. using namespace std;
  15.  
  16. class Book
  17. {
  18. private:
  19.     string title;
  20.     string authorLastName;
  21.     string authorFirstName;
  22.     string authorMiddleInitial;
  23.     string ISBN;
  24.     string price;
  25.     string publisher;
  26.     string publicationDate;
  27.     int pages;
  28.     int inventoryCount;
  29.    
  30. public:
  31.     string getTitle();
  32.     string getAuthorLastName();
  33.     string getAuthorFirstName();
  34.     string getISBN();
  35.     string getPrice();
  36.     string getPublisher();
  37.     string getPublicationDate();
  38.     int getPages();
  39.     int getInventoryCount();
  40.     void adjustInventoryCount();
  41.     Book(string);
  42. };
  43.  
  44. Book::Book(string rowData)
  45. {
  46.     istringstream columns(rowData);
  47.     string column;
  48.    
  49.     int counter = 0;
  50.     while (getline(columns, column, ',')) {
  51.         switch (counter) {
  52.             case 0:
  53.                 title = column;
  54.                 break;
  55.             case 1:
  56.                 authorLastName = column;
  57.                 break;
  58.             case 2:
  59.                 authorFirstName = column;
  60.                 break;
  61.             case 3:
  62.                 authorMiddleInitial = column;
  63.                 break;
  64.             case 4:
  65.                 ISBN = column;
  66.                 break;
  67.             case 5:
  68.                 price = column;
  69.                 break;
  70.             case 6:
  71.                 publisher = column;
  72.                 break;
  73.             case 7:
  74.                 publicationDate = column;
  75.                 break;
  76.             case 8:
  77.                 pages = stoi(column);
  78.                 break;
  79.             case 9:
  80.                 inventoryCount = stoi(column);
  81.                 break;
  82.         }
  83.         counter++;
  84.     }
  85. }
  86.  
  87. string Book::getTitle()
  88. {
  89.     return title;
  90. }
  91. string Book::getAuthorLastName()
  92. {
  93.     return authorLastName;
  94. }
  95. string Book::getAuthorFirstName()
  96. {
  97.     return authorFirstName;
  98. }
  99. string Book::getISBN()
  100. {
  101.     return ISBN;
  102. }
  103. int Book::getInventoryCount()
  104. {
  105.     return inventoryCount;
  106. }
  107. void Book::adjustInventoryCount()
  108. {
  109.     int adjustment;
  110.    
  111.     cout << "Enter the amount you would like to adjust the inventory count by: "; cin >> adjustment;
  112.     inventoryCount += adjustment;
  113. }
  114. string promptISBN()
  115. {
  116.     string userISBN;
  117.     cout << "Please input ISBN number here: ";
  118.     cin >> userISBN;
  119.    
  120.     return userISBN;
  121. }
  122.  
  123. void ISBNerror()
  124. {
  125.     cout << "Sorry, that ISBN number does not exist in our database. You will now be returned to the menu options.\n\n";
  126. }
  127.  
  128. int main()
  129. {
  130.     ifstream inputFile;
  131.     //inputFile.open("c:\\temp\\books.txt", ios::in);
  132.     inputFile.open("/Users/isaacharris/Desktop/books.txt");
  133.     if (!inputFile.is_open())
  134.     {
  135.         cout << "The file could not be opened or found." << endl;
  136.         system("pause");
  137.         return 1;
  138.     }
  139.    
  140.     vector<Book> books;
  141.    
  142.     string rowData;
  143.     // This will return the header row--to be discarded.
  144.     getline(inputFile, rowData);
  145.    
  146.     while (getline(inputFile, rowData))
  147.     {
  148.         Book newBook = Book(rowData);
  149.         books.push_back(newBook);
  150.     }
  151.  
  152.     inputFile.close();
  153.    
  154.     string menuOption;
  155.    
  156.     while (menuOption != "4")
  157.     {
  158.         cout << "1) Display book details" << endl;
  159.         cout << "2) Adjust inventory counts" << endl;
  160.         cout << "3) Display inventory" << endl;
  161.         cout << "4) Quit" << "\n\n";
  162.         cout << "Type the number of the menu option you would like to execute: "; cin >> menuOption;
  163.        
  164.         string userISBN;
  165.        
  166.         if (menuOption == "1")
  167.         {
  168.             userISBN = promptISBN();
  169.             for (long i = 0; i < books.size(); i++)
  170.             {
  171.                 if (userISBN == books[i].getISBN())
  172.                 {
  173.                     cout << endl << "Title: " << books[i].getTitle() << endl;
  174.                     cout << "Author: " << books[i].getAuthorLastName() << ", " << books[i].getAuthorFirstName() << "\n\n";
  175.                 }
  176.             }
  177.            
  178.             int counter = 0;
  179.             for (long i = 0; i < books.size(); i++)
  180.             {
  181.                 if (userISBN != books[i].getISBN())
  182.                 {
  183.                     counter++;
  184.                 }
  185.             }
  186.             if (counter++ == books.size())
  187.             {
  188.                 ISBNerror();
  189.             }
  190.         }
  191.        
  192.         else if (menuOption == "2")
  193.         {
  194.             userISBN = promptISBN();
  195.             for (long i = 0; i < books.size(); i++)
  196.             {
  197.                 if (userISBN == books[i].getISBN())
  198.                 {
  199.                     books[i].getInventoryCount();
  200.                     cout << endl << "Current inventory count: " << books[i].getInventoryCount() << "\n";
  201.                     books[i].adjustInventoryCount();
  202.                     cout << endl;
  203.                 }
  204.             }
  205.            
  206.             int counter = 0;
  207.             for (long i = 0; i < books.size(); i++)
  208.             {
  209.                 if (userISBN != books[i].getISBN())
  210.                 {
  211.                     counter++;
  212.                 }
  213.             }
  214.             if (counter++ == books.size())
  215.             {
  216.                 ISBNerror();
  217.             }
  218.         }
  219.    
  220.         else if (menuOption == "3")
  221.         {
  222.             userISBN = promptISBN();
  223.             for (long i = 0; i < books.size(); i++)
  224.             {
  225.                 if (userISBN == books[i].getISBN())
  226.                 {
  227.                     cout << endl << "Current inventory count: " << books[i].getInventoryCount() << "\n\n";
  228.                 }
  229.             }
  230.            
  231.             int counter = 0;
  232.             for (long i = 0; i < books.size(); i++)
  233.             {
  234.                 if (userISBN != books[i].getISBN())
  235.                 {
  236.                     counter++;
  237.                 }
  238.             }
  239.             if (counter++ == books.size())
  240.             {
  241.                 ISBNerror();
  242.             }
  243.  
  244.         }
  245.         else if (menuOption == "4")
  246.         {
  247.             cout << "Exiting program\n\n";
  248.         }
  249.         else
  250.         {
  251.             cout << "Please type in a number that MATCHES one of the menu options.\n\n";
  252.         }
  253.     }
  254.    
  255.     system("pause");
  256.     return 0;
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement