Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // File: Library.cpp
- // Programmer: Robert Doobay
- // Class: COP 2931
- // Description: This program simulates the checking out and checking in of books
- // from a library. Book data and library card data is read in from books.txt
- // and cards.txt and stored in separate arrays of their respective object types.
- // When the program user is finished making changes to the database, the
- // existing text files are deleted and new ones are created to reflect all
- // changes made.
- // book.h url:http://pastebin.com/DvKxhBgy
- // card.h url:http://pastebin.com/yEs6USpp
- // BEGIN PROGRAM
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <stdio.h>
- #include "card.h"
- #include "book.h"
- using namespace std;
- // Local Functions
- void ShowMenu();
- Card createCard(string name, string phone, int holderID);
- int main()
- {
- // int i and j will be variables for loops
- int i = 0;
- int j = 0;
- // int command will be used to store the user's choice for the menu
- int command = 0;
- // These variables are used and reused for data entry/retrieval purposes
- string name;
- string phone;
- string holderID;
- string bookID;
- string title;
- string author;
- string checked;
- // These variables store important indices for the book and card arrays
- // They will be used when the loop finds the index it's looking for
- int cardnum;
- int booknum;
- // These will be used to read in data from the text files
- char lineInput[100];
- char *buffer;
- // Declare an array of 20 cards
- Card cards[20];
- // Declare an array of 20 books
- Book books[20];
- // Declare a file pointer and open the cards file
- FILE *cardlist = fopen("cards.txt", "r");
- if (cardlist == NULL) perror ("Error opening \"cards.txt\"");
- else
- {
- // Read in lines from the file
- // As long as it doesn't encounter a NULL character, this loop will
- // read in data, token by token, into the cards array.
- for (i = 0; i < 20 && fgets(lineInput, 100, cardlist) != NULL; i++)
- {
- cout << "Loading card...";
- buffer = strtok(lineInput, ","); // gets token until the first comma
- name = buffer;
- buffer = strtok(NULL, ","); // gets second token until second comma
- buffer++; // skips the space
- phone = buffer;
- buffer = strtok(NULL, ","); // gets third token until third comma
- buffer++; // skips space
- holderID = buffer;
- buffer = strtok(NULL, ","); // gets fourth token until fourth comma
- buffer++; // skips space, ends at last token
- bookID = buffer; // last assignment
- // Store all assigned tokens in the array at index i
- cards[i] = Card(name, phone, stoi(holderID));
- cout << "DONE" << endl;
- // If card has a checked book, it will assign the book to the card
- if (stoi(bookID) != 0)
- {
- cards[i].changeBook(stoi(bookID));
- }
- }
- }
- // Declare a file pointer and open the books file
- FILE *booklist = fopen("books.txt", "r");
- if (booklist == NULL) perror ("Error opening \"books.txt\"");
- else
- {
- // Read the lines from the file
- // As long as it doesn't encounter a NULL character, this loop will
- // read in data, token by token, into the cards array.
- for (i = 0; i < 20 && fgets(lineInput, 100, booklist) != NULL; i++)
- {
- cout << "Loading book...";
- buffer = strtok(lineInput, ","); // gets token until the first comma
- title = buffer;
- buffer = strtok(NULL, ","); // gets second token until second comma
- buffer++;
- author = buffer;
- buffer = strtok(NULL, ","); // gets third token until third comma
- buffer++;
- bookID = buffer;
- buffer = strtok(NULL, ","); // gets fourth token until fourth comma
- buffer++;
- checked = buffer;
- buffer = strtok(NULL, ","); // gets fifth token until fifth comma
- buffer++; // skips space, ends at last token
- holderID = buffer; // last assignment
- // Store all assigned tokens in the array at index i
- books[i] = Book(title, author, stoi(bookID));
- cout << "DONE" << endl;
- // If it's checked out, it will assigns the card ID to the book
- if (checked != "false")
- {
- books[i].changeHold(stoi(holderID));
- }
- }
- }
- cout << endl;
- // Display Main Menu
- while(command != 6) // Listens for value 6, which will shut down application
- {
- ShowMenu(); // Show Menu
- cin >> command; // Await user input
- switch (command) // Switch statement to process user input
- {
- case 1: // Display Book List
- // Parse through cards and check for absence of initialization.
- // The loop will terminate not just if i==20, it will also
- // terminate if the initialization check fails.
- // This is to prevent junk output.
- // If i==20, then the array is full of cards.
- for (i = 0; i < 20 && (cards[i].checkInit() != 0); i++)
- {
- // Print separator
- cout << "\n" << endl;
- // Print card information
- cards[i].getInfo();
- // Check if card has checked book
- if(cards[i].getStatus() == 0)
- {
- cout.width(8); cout << "Books: " << "NONE" << endl;
- }
- else
- {
- // Parse array of books and find book name
- // If i does not reach 20, the book ID was found
- // If i==20, then the book ID was not found
- for (j = 0; j < 20 && books[j].getID() != cards[i].getBook(); j++);
- if (j == 20)
- {
- cout.width(8); cout << "Error: Unknown Book" << endl;
- }
- // Print book name
- else
- {
- cout.width(8); cout << "ON HAND: " << books[j].getTitle() << endl;
- }
- }
- }
- // Print separator
- cout << "\n" << endl;
- // Throw alert if card array is full
- if (i == 20) cout << "\nCARD LIST FULL\n" << endl;
- break;
- case 2: // Display Card List
- // Parse through books and check for absence of initialization.
- // The loop will terminate not just if i==20, it will also
- // terminate if the initialization check fails.
- // This is to prevent junk output.
- // If i==20, then the array is full of cards.
- for (i = 0; i < 20 && books[i].checkInit() != 0; i++)
- {
- // Print separator
- cout << "\n" << endl;
- // Print book information
- books[i].getInfo();
- }
- // Print Separator
- cout << "\n\n" << endl;
- break;
- case 3: // Check Out A Book
- // Await user input for card ID and book ID
- cout << "Please enter the ID of the person checking out: ";
- cin >> holderID;
- cout << "Please enter the ID of the book to check out: ";
- cin >> bookID;
- cout << endl;
- // Parse through cards and check for matching IDs.
- // If i==20, then the card ID was not found.
- // If loop breaks before i==20, then the card ID was found
- for (i = 0; i < 20 && cards[i].getID() != stoi(holderID); i++);
- if (i == 20)
- {
- cout << "Error: Person ID not found.\n" << endl;
- break;
- }
- // Store index if person is found
- cardnum = i;
- // Parse through books and check for matching IDs.
- // If i==20, then the book ID was not found.
- // If loop breaks before i==20, then the book ID was found
- for (i = 0; i < 20 && books[i].getID() != stoi(bookID); i++);
- // Throw alert if book not found
- if (i == 20)
- {
- cout << "Error: Book ID not found.\n" << endl;
- break;
- }
- // Store index if book is found
- booknum = i;
- // Check if book is already checked out
- if (books[booknum].getStatus() != 0)
- {
- // Parse through cards to find person who checked out book
- // by comparing book holder ID with card ID
- // If i==20, then an unknown user has the book.
- // If loop breaks before 20, then the card ID was found
- for (i = 0; i < 20 && cards[i].getID() != books[booknum].whoHas(); i++);
- if (i == 20)
- {
- // Throw error if card ID not found
- cout << "Error: This book is checked out by UNKNOWN\n" << endl;
- }
- // Print name of person who checked out the book
- else cout << cards[i].getName() << " currently has this book." << endl;
- cout << "Please try again after this book is checked back in." << endl;
- cout << "\n";
- break;
- }
- // Check if person attempting to check out book has a book
- // he needs to return by checking his card status
- if (cards[cardnum].getStatus() != 0)
- {
- // Throw error if he has already has checked out book
- cout << cards[cardnum].getName() << " must return his book before "
- << "checking out a new one.\n" << endl;
- break;
- }
- // If no errors were encountered, proceed with book checkout.
- // Assign book ID to card, and card ID to book.
- books[booknum].changeHold(stoi(holderID));
- cards[cardnum].changeBook(stoi(bookID));
- // Print success message with name
- cout << "Book checkout to " << cards[cardnum].getName() << " successful.\n" << endl;
- break;
- case 4: // Return A Book
- // Await user input
- cout << "Please enter the ID of the person returning: ";
- cin >> holderID;
- cout << "Please enter the ID of the book to return: ";
- cin >> bookID;
- cout << endl;
- // Parse through cards and check for matching IDs.
- // If i==20, then the card ID was not found.
- // If loop breaks before i==20, then the card ID was found
- for (i = 0; i < 20 && cards[i].getID() != stoi(holderID); i++);
- // Throw error if ID is not found
- if (i == 20)
- {
- cout << "Error: Person ID not found\n" << endl;
- break;
- }
- // Store index if card is found
- cardnum = i;
- // Parse through books and check for matching IDs.
- // If i==20, then the book ID was not found.
- // If loop breaks before i==20, then the book ID was found
- for (i = 0; i < 20 && books[i].getID() != stoi(bookID); i++);
- // Throw error if book is not found
- if (i == 20)
- {
- cout << "Error: Book ID not found.\n" << endl;
- break;
- }
- // Store index if book is found
- booknum = i;
- // Check if book is already checked in
- if (books[booknum].getStatus() != 1)
- {
- cout << "That book is already in our inventory.\n" << endl;
- }
- // Check if user has any books checked out
- if (cards[cardnum].getStatus() != 1)
- {
- cout << "You do not have any checked books to return.\n" << endl;
- }
- // If either error was thrown, then break operation
- if (books[booknum].getStatus() != 1 || cards[cardnum].getStatus() != 1) break;
- // If no errors were encountered, then proceed with book return
- // Assign both card ID and book ID to 0
- cards[cardnum].resetHold();
- books[booknum].resetHold();
- // Print success message with name
- cout << "Book return from " << cards[cardnum].getName() << " successful.\n" << endl;
- break;
- case 5: // Create A Card
- // Await user input
- cout << "Please enter the name for the card: ";
- // Flush stream
- cin.ignore();
- getline(cin, name);
- cout << "Please enter the phone number for the card: ";
- cin >> phone;
- cout << "Please enter the user ID desired: ";
- cin >> holderID;
- cout << endl;
- // Parse through cards and check for matching IDs.
- // If i==20, then no matching IDs were found.
- // If loop breaks before i==20, then the card ID was found
- // If a card ID is found, then it will throw an error, since
- // there cannot be two matching IDs.
- for (i = 0; i < 20 && cards[i].getID() != stoi(holderID); i++);
- // Throw error if ID is found
- if (i != 20)
- {
- cout << "Error: That ID is already taken.\n" << endl;
- break;
- }
- // Parse through cards and stop when uninitialized space
- // is encounered.
- // If i==20, then the array is full.
- // If loop breaks before i==20, then a space was found to
- // store the new card.
- for(i = 0; i < 20 && cards[i].checkInit() != 0; i++);
- // Throw alert if array is full
- if (i == 20)
- {
- cout << "Error: no more cards can be added." << endl;
- cout << "Please try again later.\n" << endl;
- break;
- }
- // If no errors were encountered, then append new card to array
- cards[i] = createCard(name, phone, stoi(holderID));
- // Print success message with name
- cout << "Card successfuly created." << endl;
- cout << cards[i].getName() << " may now check out books using their ID.\n" << endl;
- break;
- case 6: // Quit Application
- cout << "Library Application Shutting Down...\n\n" << endl;
- break;
- default: // Default Case
- cout << "Error: Bad Request. Select a number from the list:\n"
- << endl;
- break;
- }
- }
- // Close file pointers
- fclose(cardlist);
- fclose(booklist);
- // Create string to store concatenated lines for output
- string lineOutput;
- // Remove existing cards file and write new cards file
- if (remove("cards.txt") != 0) // Throw error message if cannot remove file
- {
- cout << "Error overwriting \"cards.txt\". Lost all changes.\n" << endl;
- }
- else
- {
- // Parse array of cards and write contents to file cardlist
- // Loop will stop if improperly initialized card is encountered or if
- // end of the array is encountered.
- cout << "Writing \"books.txt\"..."; // Alert message
- ofstream cardout("cards.txt", ios_base::app); // Create output stream
- for (i = 0; i < 20 && cards[i].checkInit() != 0; i++)
- {
- // Read card data to string with delimiters
- lineOutput = cards[i].getName() + ", "
- + cards[i].getPhone() + ", "
- + to_string(cards[i].getID()) + ", "
- + to_string(cards[i].getBook()) + "\n";
- // Write to file
- cardout << lineOutput;
- }
- // Close card file
- cardout.close();
- cout << "DONE" << endl;
- }
- // Remove existing books file and write new books file
- if (remove("books.txt") != 0) // Throw error message if cannot remove file
- {
- cout << "Error overwriting \"books.txt\". Lost all changes.\n" << endl;
- }
- else
- {
- // Parse array of books and write contents to file booklist
- // Loop will stop if improperly initialized book is encountered or if
- // end of the array is encountered.
- cout << "Writing \"books.txt\"..."; // Alert message
- ofstream bookout("books.txt", ios_base::app); // Create output stream
- for (i = 0; i < 20 && books[i].checkInit() != 0; i++)
- {
- // Read card data to string with delimiters
- lineOutput = books[i].getTitle() + ", "
- + books[i].getAuthor() + ", "
- + to_string(books[i].getID()) + ", "
- + ((books[i].getStatus() == 0)? "false" : "true") + ", "
- + to_string(books[i].whoHas()) + "\n";
- // Write to file
- bookout << lineOutput;
- }
- // Close book file
- bookout.close();
- cout << "DONE" << endl;
- }
- // End of main function
- return 0;
- }
- void ShowMenu()
- {
- // Display Library Application menu
- cout << " MENU\n"
- << endl;
- // List choices
- cout << "1 - Show All Library Cards" << endl;
- cout << "2 - Show All Books" << endl;
- cout << "3 - Check Out A Book" << endl;
- cout << "4 - Return A Book" << endl;
- cout << "5 - Create A New Library Card" << endl;
- cout << "6 - Exit The System" << endl;
- cout << "\n Option: ";
- }
- Card createCard(string name, string phone, int holderID)
- {
- // Create new card object using arguments and return object
- Card newcard = Card(name, phone, holderID);
- return newcard;
- }
- // END PROGRAM
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement