Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "UserInteractor.h"
- #include <limits>
- #include <vector>
- #ifndef _MSC_VER
- const UI UserInteractor::ADDBOOK;
- const UI UserInteractor::REMOVEBOOK;
- const UI UserInteractor::ADDCUSTOMER;
- const UI UserInteractor::REMOVECUSTOMER;
- const UI UserInteractor::ASSIGNBOOKTOCUSTOMER;
- const UI UserInteractor::REMOVEASSIGNMENT;
- const UI UserInteractor::FINDBOOK;
- const UI UserInteractor::FINDCUSTOMER;
- const UI UserInteractor::LISTOFALLBOOKS;
- const UI UserInteractor::LISTOFALLCUSTOMERS;
- const UI UserInteractor::LOADBOOKSFROMFILE;
- const UI UserInteractor::LOADCUSTOMERSFROMFILE;
- const UI UserInteractor::LOADASSIGNMENTSFROMFILE;
- const UI UserInteractor::SAVEBOOKSTOFILE;
- const UI UserInteractor::SAVECUSTOMERSTOFILE;
- const UI UserInteractor::SAVEASSIGNMENTSTOFILE;
- const UI UserInteractor::EXIT;
- #endif
- UserInteractor::UserInteractor()
- {
- }
- void UserInteractor::aboutCustomer(const Customer customer, const std::vector<Book> books)
- {
- std::cout << "\nID: " << customer.getID() << "\n";
- std::cout << "First name: " << customer.getFirstName() << "\n";
- std::cout << "Last name: " << customer.getLastName() << "\n";
- std::cout << "Year of birth: " << customer.getBirthYear() << "\n";
- std::cout << "Place of residence: " << customer.getPlaceOfResidence() << "\n";
- std::cout << "Borrowed books: " << books.size() << "\n";
- for(unsigned i = 0; i < books.size(); ++i)
- std::cout << "Book " << (i+1) << ": " << books[i].getTitle() << "\n";
- }
- void UserInteractor::aboutBook(const Book book, const Customer customer)
- {
- std::cout << "\nID: " << book.getID() << "\n";
- std::cout << "Title: " << book.getTitle() << "\n";
- std::cout << "Author: " << book.getAuthor() << "\n";
- std::cout << "Release date: " << book.getReleaseDate() << "\n";
- std::cout << "ISBN: " << book.getISBN() << "\n";
- if(book.getBorrowed())
- std::cout << "Borrowed by: " << customer.getFirstName() << " " << customer.getLastName() << "\n";
- }
- void UserInteractor::printMenu()
- {
- std::cout << "\n MENU\n";
- std::cout << "1. Add book\n";
- std::cout << "2. Remove book\n";
- std::cout << "3. Add customer\n";
- std::cout << "4. Remove customer\n";
- std::cout << "5. Assign book to customer\n";
- std::cout << "6. Remove assignment\n";
- std::cout << "7. Find book\n";
- std::cout << "8. Find customer\n";
- std::cout << "9. List of all books\n";
- std::cout << "10. List of all customer\n";
- std::cout << "11. Load books from file\n";
- std::cout << "12. Load customers from file\n";
- std::cout << "13. Load assignments from file\n";
- std::cout << "14. Save books to file\n";
- std::cout << "15. Save customers to file\n";
- std::cout << "16. Save assignments to file\n";
- std::cout << "17. Exit\n";
- }
- int UserInteractor::chooseMenuAction()
- {
- std::string choice;
- int choice_int;
- std::cout << "\nYour choice:\n> ";
- std::cin >> choice;
- choice_int = std::stoi(choice); // throws exceptions
- return choice_int;
- }
- void UserInteractor::invalidValue()
- {
- std::cout << "Invalid value!\n\n";
- }
- Customer UserInteractor::addCustomerDialogue()
- {
- int ID = getCustomerID();
- std::string firstName, lastName, birthYear, placeOfResidence;
- int birthYear_int;
- std::cout << "\nCustomer's first name:\n> ";
- std::cin >> firstName;
- std::cout << "\nCustomer's last name:\n> ";
- std::cin >> lastName;
- std::cout << "\nCustomer's birth year:\n> ";
- std::cin >> birthYear;
- birthYear_int = std::stoi(birthYear); // throws exceptions
- std::cout << "\nCustomer's place of residence:\n> ";
- std::cin >> placeOfResidence;
- return Customer(ID, firstName, lastName, birthYear_int, placeOfResidence);
- }
- Book UserInteractor::addBookDialogue()
- {
- int ID = getBookID();
- std::string title, author, ISBN, releaseDate;
- unsigned long long ISBN_ull;
- int releaseDate_int;
- // clear stdin stream (becouse of using both std::cin and std::getline in code)
- std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- std::cin.clear();
- fflush(stdin);
- std::cout << "\nBook title:\n> ";
- std::getline(std::cin, title);
- std::cout << "\nAuthor:\n> ";
- std::getline(std::cin, author);
- std::cout << "\nISBN:\n> ";
- std::cin >> ISBN;
- std::cout << "\nRelease date:\n> ";
- std::cin >> releaseDate;
- ISBN_ull = std::stoull(ISBN); // throws exceptions
- releaseDate_int = std::stoi(releaseDate); // throws exceptions
- return Book(ID, title, author, ISBN_ull, releaseDate_int);
- }
- int UserInteractor::getBookID()
- {
- std::string ID;
- int ID_int;
- std::cout << "\nBook ID:\n> ";
- std::cin >> ID;
- ID_int = std::stoi(ID); // throws exceptions
- return ID_int;
- }
- int UserInteractor::getCustomerID()
- {
- std::string ID;
- int ID_int;
- std::cout << "\nCustomer ID:\n> ";
- std::cin >> ID;
- ID_int = std::stoi(ID); // throws exceptions
- return ID_int;
- }
- std::string UserInteractor::getFilename()
- {
- std::string filename;
- std::cout << "\nFilename:\n> ";
- std::cin >> filename;
- return filename;
- }
- void UserInteractor::openFile(const std::string filename)
- {
- this->file.open(filename, std::ios::in | std::ios::out);
- }
- void UserInteractor::closeFile()
- {
- this->file.close();
- }
- void UserInteractor::truncateFile(const std::string filename)
- {
- this->file.close();
- this->file.open(filename, std::ios::out | std::ios::trunc);
- this->file.close();
- }
- Book UserInteractor::parseBook()
- {
- std::string title, author, ID, releaseYear, ISBN;
- std::getline(file, ID);
- std::getline(file, title);
- std::getline(file, author);
- std::getline(file, ISBN);
- std::getline(file, releaseYear);
- return Book(std::stoi(ID), title, author, std::stoull(ISBN), std::stoi(releaseYear));
- }
- Customer UserInteractor::parseCustomer()
- {
- std::string ID, firstName, lastName, birthYear, placeOfResidence;
- std::getline(file, ID);
- std::getline(file, firstName);
- std::getline(file, lastName);
- std::getline(file, birthYear);
- std::getline(file, placeOfResidence);
- return Customer(std::stoi(ID), firstName, lastName, std::stoi(birthYear), placeOfResidence);
- }
- void UserInteractor::waitForEnter()
- {
- // clear stdin stream
- std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- std::cin.clear();
- fflush(stdin);
- std::cin.sync();
- std::cin.get();
- }
- void UserInteractor::saveBook(const Book book)
- {
- file << book.getID() << "\n";
- file << book.getTitle() << "\n";
- file << book.getAuthor() << "\n";
- file << book.getISBN() << "\n";
- file << book.getReleaseDate() << "\n";
- }
- void UserInteractor::saveCustomer(const Customer customer)
- {
- file << customer.getID() << "\n";
- file << customer.getFirstName() << "\n";
- file << customer.getLastName() << "\n";
- file << customer.getBirthYear() << "\n";
- file << customer.getPlaceOfResidence() << "\n";
- }
- std::pair<int, int> UserInteractor::parseAssignment()
- {
- std::pair<int, int> CB;
- file >> CB.first >> CB.second;
- return CB;
- }
- void UserInteractor::saveAssignment(std::pair<Customer*, Book*> CB)
- {
- file << CB.first->getID() << " " << CB.second->getID() << "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement