Advertisement
robbydooby

Library.cpp

Dec 3rd, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 20.74 KB | None | 0 0
  1. // File: Library.cpp
  2.  
  3. // Programmer: Robert Doobay
  4.  
  5. // Class: COP 2931
  6.  
  7. // Description: This program simulates the checking out and checking in of books
  8. // from a library.  Book data and library card data is read in from books.txt
  9. // and cards.txt and stored in separate arrays of their respective object types.
  10. // When the program user is finished making changes to the database, the
  11. // existing text files are deleted and new ones are created to reflect all
  12. // changes made.
  13.  
  14. // book.h url:http://pastebin.com/DvKxhBgy
  15. // card.h url:http://pastebin.com/yEs6USpp
  16.  
  17.  
  18. // BEGIN PROGRAM
  19.  
  20. #include <iostream>
  21. #include <fstream>
  22. #include <string>
  23. #include <stdio.h>
  24. #include "card.h"
  25. #include "book.h"
  26. using namespace std;
  27.  
  28. // Local Functions
  29. void ShowMenu();
  30. Card createCard(string name, string phone, int holderID);
  31.  
  32. int main()
  33. {
  34.     // int i and j will be variables for loops
  35.     int i = 0;
  36.     int j = 0;
  37.     // int command will be used to store the user's choice for the menu
  38.     int command = 0;
  39.    
  40.     // These variables are used and reused for data entry/retrieval purposes
  41.     string name;
  42.     string phone;
  43.     string holderID;
  44.     string bookID;
  45.     string title;
  46.     string author;
  47.     string checked;
  48.    
  49.     // These variables store important indices for the book and card arrays
  50.     // They will be used when the loop finds the index it's looking for
  51.     int cardnum;
  52.     int booknum;
  53.    
  54.     // These will be used to read in data from the text files
  55.     char lineInput[100];
  56.     char *buffer;
  57.  
  58.    
  59.     // Declare an array of 20 cards
  60.    
  61.     Card cards[20];
  62.    
  63.     // Declare an array of 20 books
  64.    
  65.     Book books[20];
  66.    
  67.     // Declare a file pointer and open the cards file
  68.    
  69.     FILE *cardlist = fopen("cards.txt", "r");
  70.     if (cardlist == NULL) perror ("Error opening \"cards.txt\"");
  71.     else
  72.     {
  73.    
  74.         // Read in lines from the file
  75.        
  76.        
  77.         // As long as it doesn't encounter a NULL character, this loop will
  78.         // read in data, token by token, into the cards array.
  79.  
  80.         for (i = 0; i < 20 && fgets(lineInput, 100, cardlist) != NULL; i++)
  81.         {
  82.             cout << "Loading card...";
  83.            
  84.             buffer = strtok(lineInput, ","); // gets token until the first comma
  85.            
  86.             name = buffer;
  87.             buffer = strtok(NULL, ","); // gets second token until second comma
  88.             buffer++;                   // skips the space
  89.            
  90.             phone = buffer;
  91.             buffer = strtok(NULL, ","); // gets third token until third comma
  92.             buffer++;                   // skips space
  93.            
  94.             holderID = buffer;
  95.             buffer = strtok(NULL, ","); // gets fourth token until fourth comma
  96.             buffer++;                   // skips space, ends at last token
  97.            
  98.             bookID = buffer;            // last assignment
  99.            
  100.             // Store all assigned tokens in the array at index i
  101.            
  102.             cards[i] = Card(name, phone, stoi(holderID));
  103.            
  104.             cout << "DONE" << endl;
  105.             // If card has a checked book, it will assign the book to the card
  106.            
  107.             if (stoi(bookID) != 0)
  108.             {
  109.                 cards[i].changeBook(stoi(bookID));
  110.             }
  111.         }
  112.     }
  113.    
  114.     // Declare a file pointer and open the books file
  115.    
  116.     FILE *booklist = fopen("books.txt", "r");
  117.     if (booklist == NULL) perror ("Error opening \"books.txt\"");
  118.     else
  119.     {
  120.        
  121.         // Read the lines from the file
  122.  
  123.         // As long as it doesn't encounter a NULL character, this loop will
  124.         // read in data, token by token, into the cards array.
  125.        
  126.         for (i = 0; i < 20 && fgets(lineInput, 100, booklist) != NULL; i++)
  127.         {
  128.             cout << "Loading book...";
  129.            
  130.             buffer = strtok(lineInput, ","); // gets token until the first comma
  131.            
  132.             title = buffer;
  133.             buffer = strtok(NULL, ","); // gets second token until second comma
  134.             buffer++;
  135.            
  136.             author = buffer;
  137.             buffer = strtok(NULL, ","); // gets third token until third comma
  138.             buffer++;
  139.            
  140.             bookID = buffer;
  141.             buffer = strtok(NULL, ","); // gets fourth token until fourth comma
  142.             buffer++;
  143.            
  144.             checked = buffer;
  145.             buffer = strtok(NULL, ","); // gets fifth token until fifth comma
  146.             buffer++;                   // skips space, ends at last token
  147.            
  148.             holderID = buffer;         // last assignment
  149.            
  150.             // Store all assigned tokens in the array at index i
  151.            
  152.             books[i] = Book(title, author, stoi(bookID));
  153.             cout << "DONE" << endl;
  154.            
  155.             // If it's checked out, it will assigns the card ID to the book
  156.            
  157.             if (checked != "false")
  158.             {
  159.                 books[i].changeHold(stoi(holderID));
  160.             }
  161.         }
  162.     }
  163.    
  164.     cout << endl;
  165.    
  166.     // Display Main Menu
  167.    
  168.     while(command != 6) // Listens for value 6, which will shut down application
  169.     {
  170.         ShowMenu();     // Show Menu
  171.        
  172.         cin >> command; // Await user input
  173.        
  174.         switch (command)    // Switch statement to process user input
  175.         {
  176.             case 1:     // Display Book List
  177.                
  178.                 // Parse through cards and check for absence of initialization.
  179.                 // The loop will terminate not just if i==20, it will also
  180.                 // terminate if the initialization check fails.
  181.                 // This is to prevent junk output.
  182.                 // If i==20, then the array is full of cards.
  183.                
  184.                 for (i = 0; i < 20 && (cards[i].checkInit() != 0); i++)
  185.                 {
  186.                     // Print separator
  187.                     cout << "\n" << endl;
  188.                    
  189.                     // Print card information
  190.                     cards[i].getInfo();
  191.                    
  192.                     // Check if card has checked book
  193.                     if(cards[i].getStatus() == 0)
  194.                     {
  195.                         cout.width(8); cout << "Books: " << "NONE" << endl;
  196.                     }
  197.                    
  198.                     else
  199.                     {
  200.                         // Parse array of books and find book name
  201.                         // If i does not reach 20, the book ID was found
  202.                         // If i==20, then the book ID was not found
  203.                         for (j = 0; j < 20 && books[j].getID() != cards[i].getBook(); j++);
  204.                        
  205.                         if (j == 20)
  206.                         {
  207.                             cout.width(8); cout << "Error: Unknown Book" << endl;
  208.                         }
  209.                        
  210.                         // Print book name
  211.                         else
  212.                         {
  213.                             cout.width(8); cout << "ON HAND: " <<  books[j].getTitle() << endl;
  214.                         }
  215.                     }
  216.                 }
  217.                
  218.                 // Print separator
  219.                 cout << "\n" << endl;
  220.                
  221.                 // Throw alert if card array is full
  222.                 if (i == 20) cout << "\nCARD LIST FULL\n" << endl;
  223.                
  224.                 break;
  225.                
  226.             case 2:     // Display Card List
  227.                
  228.                 // Parse through books and check for absence of initialization.
  229.                 // The loop will terminate not just if i==20, it will also
  230.                 // terminate if the initialization check fails.
  231.                 // This is to prevent junk output.
  232.                 // If i==20, then the array is full of cards.
  233.                
  234.                 for (i = 0; i < 20 && books[i].checkInit() != 0; i++)
  235.                 {
  236.                     // Print separator
  237.                     cout << "\n" << endl;
  238.                    
  239.                     // Print book information
  240.                     books[i].getInfo();
  241.                 }
  242.                
  243.                 // Print Separator
  244.                 cout << "\n\n" << endl;
  245.                 break;
  246.                
  247.             case 3:     // Check Out A Book
  248.                
  249.                 // Await user input for card ID and book ID
  250.                 cout << "Please enter the ID of the person checking out: ";
  251.                 cin >> holderID;
  252.                 cout << "Please enter the ID of the book to check out: ";
  253.                 cin >> bookID;
  254.                
  255.                 cout << endl;
  256.                
  257.                 // Parse through cards and check for matching IDs.
  258.                 // If i==20, then the card ID was not found.
  259.                 // If loop breaks before i==20, then the card ID was found
  260.                
  261.                 for (i = 0; i < 20 && cards[i].getID() != stoi(holderID); i++);
  262.                
  263.                 if (i == 20)
  264.                 {
  265.                     cout << "Error: Person ID not found.\n" << endl;
  266.                     break;
  267.                 }
  268.                
  269.                 // Store index if person is found
  270.                 cardnum = i;
  271.                
  272.                 // Parse through books and check for matching IDs.
  273.                 // If i==20, then the book ID was not found.
  274.                 // If loop breaks before i==20, then the book ID was found
  275.                
  276.                 for (i = 0; i < 20 && books[i].getID() != stoi(bookID); i++);
  277.                
  278.                 // Throw alert if book not found
  279.                 if (i == 20)
  280.                 {
  281.                     cout << "Error: Book ID not found.\n" << endl;
  282.                     break;
  283.                 }
  284.                
  285.                 // Store index if book is found
  286.                 booknum = i;
  287.                
  288.                 // Check if book is already checked out
  289.                 if (books[booknum].getStatus() != 0)
  290.                 {
  291.                     // Parse through cards to find person who checked out book
  292.                     // by comparing book holder ID with card ID
  293.                     // If i==20, then an unknown user has the book.
  294.                     // If loop breaks before 20, then the card ID was found
  295.                    
  296.                     for (i = 0; i < 20 && cards[i].getID() != books[booknum].whoHas(); i++);
  297.                    
  298.                     if (i == 20)
  299.                     {
  300.                         // Throw error if card ID not found
  301.                         cout << "Error: This book is checked out by UNKNOWN\n" << endl;
  302.                     }
  303.                    
  304.                     // Print name of person who checked out the book
  305.                     else cout << cards[i].getName() << " currently has this book." << endl;
  306.                    
  307.                     cout << "Please try again after this book is checked back in." << endl;
  308.                     cout << "\n";
  309.                     break;
  310.                 }
  311.                
  312.                 // Check if person attempting to check out book has a book
  313.                 // he needs to return by checking his card status
  314.                 if (cards[cardnum].getStatus() != 0)
  315.                 {
  316.                     // Throw error if he has already has checked out book
  317.                     cout << cards[cardnum].getName() << " must return his book before "
  318.                     << "checking out a new one.\n" << endl;
  319.                     break;
  320.                 }
  321.                
  322.                 // If no errors were encountered, proceed with book checkout.
  323.                 // Assign book ID to card, and card ID to book.
  324.                 books[booknum].changeHold(stoi(holderID));
  325.                 cards[cardnum].changeBook(stoi(bookID));
  326.                
  327.                 // Print success message with name
  328.                 cout << "Book checkout to " << cards[cardnum].getName() << " successful.\n" << endl;
  329.                
  330.                 break;
  331.                
  332.             case 4:     // Return A Book
  333.                
  334.                 // Await user input
  335.                 cout << "Please enter the ID of the person returning: ";
  336.                 cin >> holderID;
  337.                
  338.                 cout << "Please enter the ID of the book to return: ";
  339.                 cin >> bookID;
  340.                
  341.                 cout << endl;
  342.  
  343.                 // Parse through cards and check for matching IDs.
  344.                 // If i==20, then the card ID was not found.
  345.                 // If loop breaks before i==20, then the card ID was found
  346.                
  347.                 for (i = 0; i < 20 && cards[i].getID() != stoi(holderID); i++);
  348.                
  349.                 // Throw error if ID is not found
  350.                 if (i == 20)
  351.                 {
  352.                     cout << "Error: Person ID not found\n" << endl;
  353.                     break;
  354.                 }
  355.                
  356.                 // Store index if card is found
  357.                 cardnum = i;
  358.                
  359.                 // Parse through books and check for matching IDs.
  360.                 // If i==20, then the book ID was not found.
  361.                 // If loop breaks before i==20, then the book ID was found
  362.                 for (i = 0; i < 20 && books[i].getID() != stoi(bookID); i++);
  363.                
  364.                 // Throw error if book is not found
  365.                 if (i == 20)
  366.                 {
  367.                     cout << "Error: Book ID not found.\n" << endl;
  368.                     break;
  369.                 }
  370.                
  371.                 // Store index if book is found
  372.                 booknum = i;
  373.                
  374.                 // Check if book is already checked in
  375.                 if (books[booknum].getStatus() != 1)
  376.                 {
  377.                     cout << "That book is already in our inventory.\n" << endl;
  378.                 }
  379.                
  380.                 // Check if user has any books checked out
  381.                 if (cards[cardnum].getStatus() != 1)
  382.                 {
  383.                     cout << "You do not have any checked books to return.\n" << endl;
  384.                 }
  385.                
  386.                 // If either error was thrown, then break operation
  387.                 if (books[booknum].getStatus() != 1 || cards[cardnum].getStatus() != 1) break;
  388.                
  389.                 // If no errors were encountered, then proceed with book return
  390.                 // Assign both card ID and book ID to 0
  391.                 cards[cardnum].resetHold();
  392.                 books[booknum].resetHold();
  393.                
  394.                 // Print success message with name
  395.                 cout << "Book return from " <<  cards[cardnum].getName() << " successful.\n" << endl;
  396.                
  397.                 break;
  398.                
  399.             case 5:     // Create A Card
  400.                
  401.                 // Await user input
  402.                 cout << "Please enter the name for the card: ";
  403.                
  404.                 // Flush stream
  405.                 cin.ignore();
  406.                 getline(cin, name);
  407.                 cout << "Please enter the phone number for the card: ";
  408.                 cin >> phone;
  409.                 cout << "Please enter the user ID desired: ";
  410.                 cin >> holderID;
  411.                
  412.                 cout << endl;
  413.                
  414.                 // Parse through cards and check for matching IDs.
  415.                 // If i==20, then no matching IDs were found.
  416.                 // If loop breaks before i==20, then the card ID was found
  417.                 // If a card ID is found, then it will throw an error, since
  418.                 // there cannot be two matching IDs.
  419.                
  420.                 for (i = 0; i < 20 && cards[i].getID() != stoi(holderID); i++);
  421.                
  422.                 // Throw error if ID is found
  423.                 if (i != 20)
  424.                 {
  425.                     cout << "Error: That ID is already taken.\n" << endl;
  426.                     break;
  427.                 }
  428.                
  429.                 // Parse through cards and stop when uninitialized space
  430.                 // is encounered.
  431.                 // If i==20, then the array is full.
  432.                 // If loop breaks before i==20, then a space was found to
  433.                 // store the new card.
  434.                
  435.                 for(i = 0; i < 20 && cards[i].checkInit() != 0; i++);
  436.                
  437.                 // Throw alert if array is full
  438.                 if (i == 20)
  439.                 {
  440.                     cout << "Error: no more cards can be added." << endl;
  441.                     cout << "Please try again later.\n" << endl;
  442.                     break;
  443.                 }
  444.                
  445.                 // If no errors were encountered, then append new card to array
  446.                 cards[i] = createCard(name, phone, stoi(holderID));
  447.                
  448.                 // Print success message with name
  449.                 cout << "Card successfuly created." << endl;
  450.                 cout << cards[i].getName() << " may now check out books using their ID.\n" << endl;
  451.                
  452.                 break;
  453.                
  454.             case 6:     // Quit Application
  455.                 cout << "Library Application Shutting Down...\n\n" << endl;
  456.                 break;
  457.                
  458.             default:    // Default Case
  459.                 cout << "Error: Bad Request. Select a number from the list:\n"
  460.                 << endl;
  461.                 break;
  462.         }
  463.     }
  464.    
  465.     // Close file pointers
  466.    
  467.     fclose(cardlist);
  468.     fclose(booklist);
  469.    
  470.     // Create string to store concatenated lines for output
  471.    
  472.     string lineOutput;
  473.    
  474.     // Remove existing cards file and write new cards file
  475.    
  476.     if (remove("cards.txt") != 0) // Throw error message if cannot remove file
  477.     {
  478.         cout << "Error overwriting \"cards.txt\". Lost all changes.\n" << endl;
  479.     }
  480.    
  481.     else
  482.     {
  483.        
  484.         // Parse array of cards and write contents to file cardlist
  485.         // Loop will stop if improperly initialized card is encountered or if
  486.         // end of the array is encountered.
  487.        
  488.         cout << "Writing \"books.txt\"..."; // Alert message
  489.         ofstream cardout("cards.txt", ios_base::app); // Create output stream
  490.        
  491.         for (i = 0; i < 20 && cards[i].checkInit() != 0; i++)
  492.         {
  493.            
  494.             // Read card data to string with delimiters
  495.             lineOutput = cards[i].getName() + ", "
  496.             + cards[i].getPhone() + ", "
  497.             + to_string(cards[i].getID()) + ", "
  498.             + to_string(cards[i].getBook()) + "\n";
  499.            
  500.             // Write to file
  501.             cardout << lineOutput;
  502.         }
  503.        
  504.         // Close card file
  505.         cardout.close();
  506.        
  507.         cout << "DONE" << endl;
  508.     }
  509.    
  510.     // Remove existing books file and write new books file
  511.    
  512.     if (remove("books.txt") != 0) // Throw error message if cannot remove file
  513.     {
  514.         cout << "Error overwriting \"books.txt\". Lost all changes.\n" << endl;
  515.     }
  516.    
  517.     else
  518.     {
  519.        
  520.         // Parse array of books and write contents to file booklist
  521.         // Loop will stop if improperly initialized book is encountered or if
  522.         // end of the array is encountered.
  523.        
  524.         cout << "Writing \"books.txt\"..."; // Alert message
  525.         ofstream bookout("books.txt", ios_base::app); // Create output stream
  526.        
  527.         for (i = 0; i < 20 && books[i].checkInit() != 0; i++)
  528.         {
  529.            
  530.             // Read card data to string with delimiters
  531.             lineOutput = books[i].getTitle() + ", "
  532.             + books[i].getAuthor() + ", "
  533.             + to_string(books[i].getID()) + ", "
  534.             + ((books[i].getStatus() == 0)? "false" : "true") + ", "
  535.             + to_string(books[i].whoHas()) + "\n";
  536.            
  537.             // Write to file
  538.             bookout << lineOutput;
  539.         }
  540.        
  541.         // Close book file
  542.         bookout.close();
  543.        
  544.         cout << "DONE" << endl;
  545.     }
  546.    
  547.     // End of main function
  548.    
  549.     return 0;
  550.  
  551. }
  552.  
  553. void ShowMenu()
  554. {
  555.     // Display Library Application menu
  556.    
  557.     cout << "    MENU\n"
  558.     << endl;
  559.    
  560.     // List choices
  561.     cout << "1 - Show All Library Cards" << endl;
  562.     cout << "2 - Show All Books" << endl;
  563.     cout << "3 - Check Out A Book" << endl;
  564.     cout << "4 - Return A Book" << endl;
  565.     cout << "5 - Create A New Library Card" << endl;
  566.     cout << "6 - Exit The System" << endl;
  567.     cout << "\n    Option: ";
  568.    
  569. }
  570.  
  571. Card createCard(string name, string phone, int holderID)
  572. {
  573.     // Create new card object using arguments and return object
  574.     Card newcard = Card(name, phone, holderID);
  575.     return newcard;
  576. }
  577.  
  578. // END PROGRAM
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement