Advertisement
Glenpl

Untitled

Jul 8th, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.30 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include "UserInteractor.h"
  4. #include <limits>
  5. #include <vector>
  6.  
  7. #ifndef _MSC_VER
  8. const UI UserInteractor::ADDBOOK;
  9. const UI UserInteractor::REMOVEBOOK;
  10. const UI UserInteractor::ADDCUSTOMER;
  11. const UI UserInteractor::REMOVECUSTOMER;
  12. const UI UserInteractor::ASSIGNBOOKTOCUSTOMER;
  13. const UI UserInteractor::REMOVEASSIGNMENT;
  14. const UI UserInteractor::FINDBOOK;
  15. const UI UserInteractor::FINDCUSTOMER;
  16. const UI UserInteractor::LISTOFALLBOOKS;
  17. const UI UserInteractor::LISTOFALLCUSTOMERS;
  18. const UI UserInteractor::LOADBOOKSFROMFILE;
  19. const UI UserInteractor::LOADCUSTOMERSFROMFILE;
  20. const UI UserInteractor::LOADASSIGNMENTSFROMFILE;
  21. const UI UserInteractor::SAVEBOOKSTOFILE;
  22. const UI UserInteractor::SAVECUSTOMERSTOFILE;
  23. const UI UserInteractor::SAVEASSIGNMENTSTOFILE;
  24. const UI UserInteractor::EXIT;
  25. #endif
  26.  
  27. UserInteractor::UserInteractor()
  28. {
  29.  
  30. }
  31.  
  32. void UserInteractor::aboutCustomer(const Customer customer, const std::vector<Book> books)
  33. {
  34.     std::cout << "\nID: " << customer.getID() << "\n";
  35.     std::cout << "First name: " << customer.getFirstName() << "\n";
  36.     std::cout << "Last name: " << customer.getLastName() << "\n";
  37.     std::cout << "Year of birth: " << customer.getBirthYear() << "\n";
  38.     std::cout << "Place of residence: " << customer.getPlaceOfResidence() << "\n";
  39.     std::cout << "Borrowed books: " << books.size() << "\n";
  40.     for(unsigned i = 0; i < books.size(); ++i)
  41.         std::cout << "Book " << (i+1) << ": " << books[i].getTitle() << "\n";
  42. }
  43.  
  44. void UserInteractor::aboutBook(const Book book, const Customer customer)
  45. {
  46.     std::cout << "\nID: " << book.getID() << "\n";
  47.     std::cout << "Title: " << book.getTitle() << "\n";
  48.     std::cout << "Author: " << book.getAuthor() << "\n";
  49.     std::cout << "Release date: " << book.getReleaseDate() << "\n";
  50.     std::cout << "ISBN: " << book.getISBN() << "\n";
  51.     if(book.getBorrowed())
  52.         std::cout << "Borrowed by: " << customer.getFirstName() << " " << customer.getLastName() << "\n";
  53. }
  54.  
  55. void UserInteractor::printMenu()
  56. {
  57.     std::cout << "\n     MENU\n";
  58.     std::cout << "1. Add book\n";
  59.     std::cout << "2. Remove book\n";
  60.     std::cout << "3. Add customer\n";
  61.     std::cout << "4. Remove customer\n";
  62.     std::cout << "5. Assign book to customer\n";
  63.     std::cout << "6. Remove assignment\n";
  64.     std::cout << "7. Find book\n";
  65.     std::cout << "8. Find customer\n";
  66.     std::cout << "9. List of all books\n";
  67.     std::cout << "10. List of all customer\n";
  68.     std::cout << "11. Load books from file\n";
  69.     std::cout << "12. Load customers from file\n";
  70.     std::cout << "13. Load assignments from file\n";
  71.     std::cout << "14. Save books to file\n";
  72.     std::cout << "15. Save customers to file\n";
  73.     std::cout << "16. Save assignments to file\n";
  74.     std::cout << "17. Exit\n";
  75. }
  76.  
  77. int UserInteractor::chooseMenuAction()
  78. {
  79.     std::string choice;
  80.     int choice_int;
  81.     std::cout << "\nYour choice:\n> ";
  82.     std::cin >> choice;
  83.     choice_int = std::stoi(choice); // throws exceptions
  84.     return choice_int;
  85. }
  86.  
  87. void UserInteractor::invalidValue()
  88. {
  89.     std::cout << "Invalid value!\n\n";
  90. }
  91.  
  92. Customer UserInteractor::addCustomerDialogue()
  93. {
  94.     int ID = getCustomerID();
  95.     std::string firstName, lastName, birthYear, placeOfResidence;
  96.     int birthYear_int;
  97.  
  98.     std::cout << "\nCustomer's first name:\n> ";
  99.     std::cin >> firstName;
  100.     std::cout << "\nCustomer's last name:\n> ";
  101.     std::cin >> lastName;
  102.     std::cout << "\nCustomer's birth year:\n> ";
  103.     std::cin >> birthYear;
  104.     birthYear_int = std::stoi(birthYear); // throws exceptions
  105.     std::cout << "\nCustomer's place of residence:\n> ";
  106.     std::cin >> placeOfResidence;
  107.  
  108.     return Customer(ID, firstName, lastName, birthYear_int, placeOfResidence);
  109. }
  110.  
  111. Book UserInteractor::addBookDialogue()
  112. {
  113.     int ID = getBookID();
  114.     std::string title, author, ISBN, releaseDate;
  115.     unsigned long long ISBN_ull;
  116.     int releaseDate_int;
  117.  
  118.     // clear stdin stream (becouse of using both std::cin and std::getline in code)
  119.     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  120.     std::cin.clear();
  121.     fflush(stdin);
  122.  
  123.     std::cout << "\nBook title:\n> ";
  124.     std::getline(std::cin, title);
  125.     std::cout << "\nAuthor:\n> ";
  126.     std::getline(std::cin, author);
  127.     std::cout << "\nISBN:\n> ";
  128.     std::cin >> ISBN;
  129.     std::cout << "\nRelease date:\n> ";
  130.     std::cin >> releaseDate;
  131.     ISBN_ull = std::stoull(ISBN); // throws exceptions
  132.     releaseDate_int = std::stoi(releaseDate); // throws exceptions
  133.  
  134.     return Book(ID, title, author, ISBN_ull, releaseDate_int);
  135. }
  136.  
  137. int UserInteractor::getBookID()
  138. {
  139.     std::string ID;
  140.     int ID_int;
  141.     std::cout << "\nBook ID:\n> ";
  142.     std::cin >> ID;
  143.     ID_int = std::stoi(ID); // throws exceptions
  144.     return ID_int;
  145. }
  146.  
  147. int UserInteractor::getCustomerID()
  148. {
  149.     std::string ID;
  150.     int ID_int;
  151.     std::cout << "\nCustomer ID:\n> ";
  152.     std::cin >> ID;
  153.     ID_int = std::stoi(ID); // throws exceptions
  154.     return ID_int;
  155. }
  156.  
  157. std::string UserInteractor::getFilename()
  158. {
  159.     std::string filename;
  160.     std::cout << "\nFilename:\n> ";
  161.     std::cin >> filename;
  162.     return filename;
  163. }
  164.  
  165. void UserInteractor::openFile(const std::string filename)
  166. {
  167.     this->file.open(filename, std::ios::in | std::ios::out);
  168. }
  169.  
  170. void UserInteractor::closeFile()
  171. {
  172.     this->file.close();
  173. }
  174.  
  175. void UserInteractor::truncateFile(const std::string filename)
  176. {
  177.     this->file.close();
  178.     this->file.open(filename, std::ios::out | std::ios::trunc);
  179.     this->file.close();
  180. }
  181.  
  182. Book UserInteractor::parseBook()
  183. {
  184.     std::string title, author, ID, releaseYear, ISBN;
  185.     std::getline(file, ID);
  186.     std::getline(file, title);
  187.     std::getline(file, author);
  188.     std::getline(file, ISBN);
  189.     std::getline(file, releaseYear);
  190.  
  191.     return Book(std::stoi(ID), title, author, std::stoull(ISBN), std::stoi(releaseYear));
  192. }
  193.  
  194. Customer UserInteractor::parseCustomer()
  195. {
  196.     std::string ID, firstName, lastName, birthYear, placeOfResidence;
  197.     std::getline(file, ID);
  198.     std::getline(file, firstName);
  199.     std::getline(file, lastName);
  200.     std::getline(file, birthYear);
  201.     std::getline(file, placeOfResidence);
  202.  
  203.     return Customer(std::stoi(ID), firstName, lastName, std::stoi(birthYear), placeOfResidence);
  204. }
  205.  
  206. void UserInteractor::waitForEnter()
  207. {
  208.     // clear stdin stream
  209.     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  210.     std::cin.clear();
  211.     fflush(stdin);
  212.  
  213.     std::cin.sync();
  214.     std::cin.get();
  215. }
  216.  
  217. void UserInteractor::saveBook(const Book book)
  218. {
  219.     file << book.getID() << "\n";
  220.     file << book.getTitle() << "\n";
  221.     file << book.getAuthor() << "\n";
  222.     file << book.getISBN() << "\n";
  223.     file << book.getReleaseDate() << "\n";
  224. }
  225.  
  226. void UserInteractor::saveCustomer(const Customer customer)
  227. {
  228.     file << customer.getID() << "\n";
  229.     file << customer.getFirstName() << "\n";
  230.     file << customer.getLastName() << "\n";
  231.     file << customer.getBirthYear() << "\n";
  232.     file << customer.getPlaceOfResidence() << "\n";
  233. }
  234.  
  235. std::pair<int, int> UserInteractor::parseAssignment()
  236. {
  237.     std::pair<int, int> CB;
  238.     file >> CB.first >> CB.second;
  239.     return CB;
  240. }
  241.  
  242. void UserInteractor::saveAssignment(std::pair<Customer*, Book*> CB)
  243. {
  244.     file << CB.first->getID() << " " << CB.second->getID() << "\n";
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement