Advertisement
phamt

Library [Final version]

Dec 6th, 2019
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.04 KB | None | 0 0
  1. /* TonyPLab8.cpp
  2. Created by: Tony Pham
  3. Date: 12.01.2019
  4.  
  5. */
  6. #include <iostream>
  7. #include <iomanip>
  8. #include <fstream>
  9. #include <string>
  10.  
  11. using namespace std;
  12.  
  13. // structure
  14. struct Book {
  15.     string title;
  16.     string author;
  17. };
  18.  
  19. const int ARRAY_SIZE = 1000;
  20. Book books[ARRAY_SIZE];
  21. string pathName;
  22. bool userInput;
  23.  
  24. // global variables
  25. int loadData();
  26. void showAll(int count);
  27. void showBooksByAuthor(int count, string author);
  28. void showBooksByTitle(int count, string title);
  29. void sortByTitle(int count, string title);
  30. void sortByAuthor(int count, string author);
  31.  
  32. int main()
  33. {
  34.     // initialised variables
  35.     int count = 0;
  36.     char selector = 'q';
  37.     string name;
  38.     string title;
  39.  
  40.     // asks user for file pathname
  41.     cout << "Welcome to Tony's Library Database.\n";
  42.     cout << "Please enter the name of the file: ";
  43.     cin >> pathName;
  44.     count = loadData();
  45.     // catches if count value is less than 0
  46.     while (count < 0) {
  47.         cout << " Do you want to try again(1) or quit(0)? \n";
  48.         cin >> userInput;
  49.         if (userInput == 1) {
  50.             cout << "Please enter the name of the file: ";
  51.             cin >> pathName;
  52.             count = loadData();
  53.         }
  54.         if (userInput == 0)
  55.         {
  56.             cout << " Program will now close! \n";
  57.             return 0;
  58.         }
  59.     }
  60.     cout << count << " Records loaded successfully.\n";
  61.     // Switch case menu
  62.     do {
  63.         cout << endl << "Please enter a keyword that corresponds to the list of options: \n";
  64.         cout << " Search by: (A)uthor, (T)itle, (S)how All, (Q)uit Program: ";
  65.         cin >> selector;
  66.         selector = toupper(selector);
  67.         switch (selector)
  68.         {
  69.         case 'S':
  70.             sortByTitle(count, title);
  71.             if (count <= 0) {
  72.                 cout << " No counts found! \n";
  73.             }
  74.             else {
  75.                 showAll(count);
  76.             }
  77.             break;
  78.         case 'A':
  79.             sortByAuthor(count, name);
  80.             cout << " Book Author: ";
  81.             cin.ignore();
  82.             getline(cin, name);
  83.             if (count <= 0) {
  84.                 cout << " No records found! \n";
  85.             }
  86.             else {
  87.                 showBooksByAuthor(count, name);
  88.                 break;
  89.             }
  90.         case 'T':
  91.             sortByTitle(count, title);
  92.             cout << " Book Title: ";
  93.             cin.ignore();
  94.             getline(cin, title);
  95.             if (count <= 0) {
  96.                 cout << " No records found! \n";
  97.             }
  98.             else {
  99.                 showBooksByTitle(count, title);
  100.                 break;
  101.             }
  102.         }
  103.     }
  104.     while (selector != 'q' && selector != 'Q');
  105.     return 0;
  106. }
  107.  
  108. int loadData()
  109. {
  110.     int count = 0;
  111.     ifstream fileLocation;
  112.     fileLocation.open(pathName);
  113.     if (!fileLocation.is_open())
  114.     {
  115.         cout << " Unable to open file path! \n";
  116.         return -1;
  117.     }
  118.     while (!fileLocation.eof()) // Echo starts here
  119.     {
  120.         getline(fileLocation, books[count].title);
  121.         getline(fileLocation, books[count].author);
  122.         count++;
  123.     }
  124.     return count;
  125. }
  126. // displays all book titles beside the author names
  127. void showAll(int count)
  128. {
  129.     for (int i = 0; i < count; i++)
  130.     {
  131.         cout << books[i].title << " " << "(" << books[i].author << ")\n";
  132.     }
  133. }
  134. // displays books based on author name matching user input.
  135. void showBooksByAuthor(int count, string name)
  136. {
  137.     int j = 0;
  138.     for (int i = 0; i < count; i++)
  139.     {
  140.         if (books[i].author.find(name) < 100)
  141.         {
  142.             cout << books[i].title << " " << "(" << books[i].author << ")\n";
  143.             j++;
  144.         }
  145.     }
  146.     cout << j << " records found! \n";
  147. }
  148. // Displays books based on title matching user input.
  149. void showBooksByTitle(int count, string title)
  150. {
  151.     int j = 0;
  152.     for (int i = 0; i < count; i++)
  153.     {
  154.         if (books[i].title.find(title) < 100)
  155.         {
  156.             cout << books[i].title << " " << "(" << books[i].author << ")\n";
  157.             j++;
  158.         }
  159.     }
  160.     cout << j << " records found! \n";
  161. }
  162. // Sorts by book title.
  163. void sortByTitle(int count, string title) {
  164.     Book temp;
  165.     for (int i = 1; i < count; i++) {
  166.         for (int j = 0; j < count - i; j++) {
  167.             if (books[j].title > books[j + 1].title) {
  168.                 temp = books[j];
  169.                 books[j] = books[j + 1];
  170.                 books[j + 1] = temp;
  171.             }
  172.         }
  173.     }
  174. }
  175. // Sorts by book author.
  176. void sortByAuthor(int count, string name) {
  177.     Book temp;
  178.     for (int i = 1; i < count; i++) {
  179.         for (int j = 0; j < count - i; j++) {
  180.             if (books[j].author > books[j + 1].author) {
  181.                 temp = books[j];
  182.                 books[j] = books[j + 1];
  183.                 books[j + 1] = temp;
  184.             }
  185.         }
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement