Advertisement
Guest User

bbltk

a guest
Aug 20th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cstdlib>
  3. #include <fstream>
  4. #include <iostream>
  5. #include <string>
  6. #define SIZE 10
  7.  
  8. using namespace std;
  9.  
  10. struct Date{
  11.     int day, month, year;
  12. };
  13.  
  14. struct Book{
  15.     int bookID;
  16.     string bookName;
  17.     int rentID;
  18. };
  19.  
  20. struct Rent{
  21.     int rentID;
  22.     Date startDate;
  23.     Date endDate;
  24.     int customerID;
  25.     int bookID;
  26. };
  27.  
  28. struct Customer{
  29.     int customerID;
  30.     string name;
  31.     string surname;
  32. };
  33.  
  34.  
  35. /*
  36. struct LogFile{
  37.     int accountID;
  38.     int typeOfOperation;
  39.     float amountOfMoney;
  40.     Date date;
  41. };
  42.  
  43. */
  44.  
  45. bool searchForCustomer(int id);
  46. void createCustomer();
  47. bool searchForRent(int id);
  48. void createRent();
  49. bool searchForBook(int id);
  50. void showCustomerRents();
  51. void returnBook();
  52. void menu();
  53.  
  54. int main()
  55. {
  56.  
  57.     menu();
  58. }
  59.  
  60. void menu()
  61. {
  62.  
  63.  
  64.     int choice;
  65.  
  66.     while(1)
  67.     {
  68.         cout << "Enter your choice" << endl;
  69.         cout << "To make a new customer, press 1" << endl;
  70.         cout << "To make a new rent, press 2" << endl;
  71.         cout << "To return a book, press 3" << endl;
  72.         cout << "To show rents by customer, press 4" << endl;
  73.         //cout << "To print log file, press 6" << endl;
  74.         cout << "To exit a program, press -1" << endl << endl;
  75.  
  76.         cin >> choice;
  77.  
  78.         if(choice == -1)
  79.            {
  80.                 break;
  81.            }
  82.         switch(choice)
  83.         {
  84.             case 1: createCustomer(); break;
  85.             case 2: createRent(); break;
  86.             case 3: returnBook();break;
  87.             case 4: showCustomerRents();break;
  88.            // case 5: showBookRents();break;
  89.            // case 6: printLogFile();break;
  90.  
  91.             default: cout << "You entered a wrong value, try again";
  92.             cin >> choice;
  93.         }
  94.     }
  95.  
  96.  
  97. }
  98.  
  99. void createCustomer()
  100. {
  101.     Customer cust;
  102.     int id;
  103.  
  104.     cout << "Please enter a new ID of your customer: ";cin >> id;
  105.  
  106.     while(1)
  107.     {
  108.         if(searchForCustomer(id) == false)
  109.         {
  110.             ofstream customersout;
  111.             customersout.open("customers.txt", ofstream::app);
  112.             if(customersout.fail())
  113.             {
  114.                 cout << "Customers out file error.";
  115.                 exit(1);
  116.             }
  117.             cust.customerID = id;
  118.             cout << "Enter a new name: ";
  119.             cin >> cust.name;
  120.             cout << "Enter a new surname: ";
  121.             cin >> cust.surname;
  122.  
  123.             customersout << cust.customerID << " " << cust.name << " " << cust.surname << endl;
  124.             customersout.close();
  125.             break;
  126.         }
  127.  
  128.         else
  129.         {
  130.             cout << "ID exists. Try again: "; cin >> id;
  131.         }
  132.     }
  133.  
  134. }
  135.  
  136. bool searchForCustomer(int id)
  137. {
  138.     Customer cust;
  139.  
  140.     ifstream customersin;
  141.     customersin.open("customers.txt");
  142.     if(customersin.fail())
  143.     {
  144.         cout << "Customers in file error.";
  145.         exit(1);
  146.     }
  147.  
  148.  
  149.     while(!customersin.eof())
  150.     {
  151.         customersin >> cust.customerID;
  152.         customersin >> cust.name;
  153.         customersin >> cust.surname;
  154.  
  155.         if(id == cust.customerID)
  156.         {
  157.             customersin.close();
  158.             return true;
  159.         }
  160.     }
  161.  
  162.     customersin.close();
  163.     return false;
  164.  
  165. }
  166.  
  167. void createRent()
  168. {
  169.     Rent rnt;
  170.  
  171.     int custID, rentID, bookID;
  172.     cout << "Please enter customer ID you want to make rent for: "; cin >> custID;
  173.  
  174.     while(1)
  175.     {
  176.         if(searchForCustomer(custID) == true)
  177.         {
  178.             cout << "Enter your rent ID: "; cin >> rentID;
  179.             while(1)
  180.             {
  181.                 if(searchForRent(rentID) == false)
  182.                 {
  183.                     rnt.customerID = custID;
  184.                     rnt.rentID = rentID;
  185.                    
  186.                     cout << "Enter starting day, month and year of rent: ";
  187.                     cin >> rnt.startDate.day >> rnt.startDate.month >> rnt.startDate.year;
  188.                    
  189.                     cout << "Enter ending day, month and year of rent: ";
  190.                     cin >> rnt.endDate.day >> rnt.endDate.month >> rnt.endDate.year;
  191.                    
  192.                     cout << "Enter a book ID you want to rent: "; cin>>bookID;
  193.                     while(1)
  194.                     {
  195.                         if(searchForBook(bookID)==true)
  196.                         {
  197.                             if(rentStatus(bookID)==true) {
  198.                                 rnt.bookID=bookID;
  199.                             }
  200.                             else
  201.                             {
  202.                                 cout<<"This book is already rented. Try another: "; cin>> bookID;
  203.                             }
  204.                             break;     
  205.                         }
  206.                         else {
  207.                             cout << "This book ID doesn't exist. Try another: "; cin>>bookID;
  208.                         }
  209.                     }
  210.                    
  211.  
  212.                     ofstream rentsout;
  213.                     rentsout.open("rents.txt", ofstream::app);
  214.                     string s, e;
  215.                     s=rnt.startDate.day+"/"+rnt.startDate.month+"/"+rnt.startDate.year;
  216.                     e=rnt.endDate.day+"/"+rnt.endDate.month+"/"+rnt.endDate.year;
  217.                     rentsout << rnt.rentID << " " << s<<" " <<e << " " << rnt.customerID <<" "<<rnt.bookID<< endl;
  218.                     rentsout.close();
  219.  
  220.                     break;
  221.                 }
  222.  
  223.                 else
  224.                 {
  225.                     cout << "Rent ID already exists. Try again: "; cin >> accID;
  226.                 }
  227.  
  228.             }
  229.  
  230.             break;
  231.         }
  232.  
  233.         else
  234.         {
  235.             cout << "Customer ID doesn't exist. Try again: "; cin >> custID;
  236.         }
  237.     }
  238.  
  239. }
  240.  
  241. bool searchForRent(int id)
  242. {
  243.     Rent rnt;
  244.     string startD;
  245.     string endD;
  246.  
  247.     ifstream rentin;
  248.     rentin.open("rents.txt");
  249.     if(rentin.fail())
  250.     {
  251.         cout << "Rents in file error.";
  252.         exit(1);
  253.     }
  254.  
  255.  
  256.     while(!rentin.eof())
  257.     {
  258.         rentin >> rnt.rentID;
  259.        
  260.         rentin >> startD;
  261.         rnt.startDate.day=startD.substr(0,2);
  262.         rnt.startDate.month=startD.substr(3,2);
  263.         rnt.startDate.year=startD.substr(6,4);
  264.        
  265.         rentin >> endD;
  266.         rnt.endDate.day=endD.substr(0,2);
  267.         rnt.endDate.month=endD.substr(3,2);
  268.         rnt.endDate.year=endD.substr(6,4);
  269.        
  270.         rentin >> rnt.customerID;
  271.         rentin >> rnt.bookID;
  272.  
  273.         if(id == rnt.rentID)
  274.         {
  275.             rentin.close();
  276.             return true;
  277.         }
  278.     }
  279.  
  280.     rentin.close();
  281.     return false;
  282.  
  283. }
  284.  
  285. bool searchForBook(int id) {
  286.     Book book;
  287.  
  288.  
  289.     ifstream bookin;
  290.     bookin.open("books.txt");
  291.     if(bookin.fail())
  292.     {
  293.         cout << "Books in file error.";
  294.         exit(1);
  295.     }
  296.  
  297.  
  298.     while(!bookin.eof())
  299.     {
  300.         bookin >> book.bookID;
  301.         bookin >> book.bookName;
  302.         bookin >> book.rentID;
  303.  
  304.         if(id == book.bookID)
  305.         {
  306.             bookin.close();
  307.             return false;
  308.         }
  309.     }
  310.  
  311.     bookin.close();
  312.     return true;
  313. }
  314.  
  315. bool rentStatus(int id) {
  316.     Book book;
  317.  
  318.  
  319.     ifstream bookin;
  320.     bookin.open("books.txt");
  321.     if(bookin.fail())
  322.     {
  323.         cout << "Books in file error.";
  324.         exit(1);
  325.     }
  326.  
  327.  
  328.     while(!bookin.eof())
  329.     {
  330.         bookin >> book.bookID;
  331.         bookin >> book.bookName;
  332.         bookin >> book.rentID;
  333.  
  334.         if(id == book.bookID)
  335.         {
  336.             if(book.rentID==-1) {
  337.                 bookin.close();
  338.                 return true;
  339.             }
  340.             else {
  341.                 bookin.close();
  342.                 return false;
  343.             }
  344.            
  345.         }
  346.     }
  347.     //never gets here cuz program logic
  348.     bookin.close();
  349.     return true;
  350. }
  351.  
  352. void returnBook(){
  353.     int customerID;
  354.     int bookID;
  355.     int rentID;
  356.     Rent rnt;
  357.     Book book;
  358.    
  359.     cout<< "Enter customer ID that is returning book: "; cin >> customerID;
  360.     while(1) {
  361.         if(searchForCustomer(customerID)==true) {
  362.             cout<<"Enter book ID customer wants to return: "; cin>>bookID;
  363.            
  364.             ifstream rentin("rents.txt");
  365.             if(rentin.fail())
  366.             {
  367.                 cout << "Rents in file error.";
  368.                 exit(1);
  369.             }
  370.            
  371.             ofstream tempRentsOut("temprents.txt");
  372.  
  373.             if(tempRentsOut.fail())
  374.             {
  375.                 cout << "Temp rents file error.";
  376.                 exit(1);
  377.             }
  378.             while(1)
  379.             {
  380.  
  381.  
  382.                 rentin >> rnt.rentID;
  383.        
  384.                 rentin >> startD;
  385.                 rnt.startDate.day=startD.substr(0,2);
  386.                 rnt.startDate.month=startD.substr(3,2);
  387.                 rnt.startDate.year=startD.substr(6,4);
  388.                
  389.                 rentin >> endD;
  390.                 rnt.endDate.day=endD.substr(0,2);
  391.                 rnt.endDate.month=endD.substr(3,2);
  392.                 rnt.endDate.year=endD.substr(6,4);
  393.                
  394.                 rentin >> rnt.customerID;
  395.                 rentin >> rnt.bookID;
  396.  
  397.                 if( rentin.eof() )
  398.                     break;
  399.  
  400.  
  401.                 if(customerID == rnt.customerID)
  402.                 {
  403.                     rentID=rnt.rentID;
  404.                     /*ofstream logFileOut("logfile.txt", ofstream::app);
  405.                     if(logFileOut.fail())
  406.                     {
  407.                         cout << "Log file error.";
  408.                         exit(1);
  409.                     }
  410.                     log.accountID = accID;
  411.                     log.amountOfMoney = deposit;
  412.                     log.typeOfOperation = 1; // 1 means deposit, 2 means withdrawal
  413.  
  414.                     cout << "Enter day, month and year of transaction: ";
  415.                     cin >> log.date.day >> log.date.month >> log.date.year;
  416.  
  417.                     logFileOut << log.accountID << " " << log.typeOfOperation << " " << log.amountOfMoney << " "
  418.                     << log.date.day << " " << log.date.month << " " << log.date.year << endl;
  419.  
  420.                     logFileOut.close(); */
  421.                 }
  422.  
  423.                 else
  424.                 {
  425.                     string s, e;
  426.                     s=rnt.startDate.day+"/"+rnt.startDate.month+"/"+rnt.startDate.year;
  427.                     e=rnt.endDate.day+"/"+rnt.endDate.month+"/"+rnt.endDate.year;
  428.                     tempRentsOut << rnt.rentID << " " << s<<" " <<e << " " << rnt.customerID <<" "<<rnt.bookID<< endl;
  429.                 }
  430.             }
  431.  
  432.             tempRentsOut.close();
  433.             rentin.close();
  434.            
  435.             ifstream bookin("books.txt");
  436.             if(bookin.fail())
  437.             {
  438.                 cout << "Books in file error.";
  439.                 exit(1);
  440.             }
  441.            
  442.             ofstream tempBooksOut("tempbooks.txt");
  443.  
  444.             if(tempBooksOut.fail())
  445.             {
  446.                 cout << "Temp books file error.";
  447.                 exit(1);
  448.             }
  449.             while(1)
  450.             {
  451.  
  452.  
  453.                 bookin >> book.bookID;
  454.                 bookin >> book.bookName;
  455.                 bookin >> book.rentID;
  456.  
  457.                 if( bookin.eof() )
  458.                     break;
  459.  
  460.  
  461.                 if(rentID == book.rentID)
  462.                 {
  463.                     book.rentID=-1;
  464.                     tempBooksOut<<book.bookID<<" "<<book.bookName<<" "<<book.rentID<<endl;
  465.                     /*ofstream logFileOut("logfile.txt", ofstream::app);
  466.                     if(logFileOut.fail())
  467.                     {
  468.                         cout << "Log file error.";
  469.                         exit(1);
  470.                     }
  471.                     log.accountID = accID;
  472.                     log.amountOfMoney = deposit;
  473.                     log.typeOfOperation = 1; // 1 means deposit, 2 means withdrawal
  474.  
  475.                     cout << "Enter day, month and year of transaction: ";
  476.                     cin >> log.date.day >> log.date.month >> log.date.year;
  477.  
  478.                     logFileOut << log.accountID << " " << log.typeOfOperation << " " << log.amountOfMoney << " "
  479.                     << log.date.day << " " << log.date.month << " " << log.date.year << endl;
  480.  
  481.                     logFileOut.close(); */
  482.                 }
  483.  
  484.                 else
  485.                 {
  486.                     tempBooksOut<<book.bookID<<" "<<book.bookName<<" "<<book.rentID<<endl;
  487.                 }
  488.             }
  489.  
  490.             tempBooksOut.close();
  491.             bookin.close();
  492.            
  493.             break;
  494.         }
  495.         else {
  496.             cout<<"Customer ID doesn't exist, try again: "; cin >>customerID;
  497.         }
  498.     }
  499. }
  500.  
  501. void showCustomerRents(){
  502.     int custID;
  503.     Rent rnt;
  504.     string startD;
  505.     string endD;
  506.    
  507.     cout<<"Enter customer ID whose rents you want to show: "; cin>>custID;
  508.    
  509.     ifstream rentin;
  510.     rentin.open("rents.txt");
  511.     if(rentin.fail())
  512.     {
  513.         cout << "Rents in file error.";
  514.         exit(1);
  515.     }
  516.  
  517.  
  518.     while(!rentin.eof())
  519.     {
  520.         rentin >> rnt.rentID;
  521.        
  522.         rentin >> startD;
  523.         rnt.startDate.day=startD.substr(0,2);
  524.         rnt.startDate.month=startD.substr(3,2);
  525.         rnt.startDate.year=startD.substr(6,4);
  526.        
  527.         rentin >> endD;
  528.         rnt.endDate.day=endD.substr(0,2);
  529.         rnt.endDate.month=endD.substr(3,2);
  530.         rnt.endDate.year=endD.substr(6,4);
  531.        
  532.         rentin >> rnt.customerID;
  533.         rentin >> rnt.bookID;
  534.  
  535.         if(custID == rnt.customerID)
  536.         {
  537.             cout<< rnt.rentID<<" "<<startD<<" "<<endD<<" "<< rnt.bookID;
  538.             rentin.close();
  539.             return true;
  540.         }
  541.     }
  542.  
  543.     rentin.close();
  544.     return false;
  545. }
  546.  
  547. /*
  548.  
  549.  
  550.  
  551. void printLogFile()
  552. {
  553.     LogFile log;
  554.     ifstream logFileIn("logfile.txt");
  555.     if(logFileIn.fail())
  556.     {
  557.         cout << "Log file error.";
  558.         exit(1);
  559.     }
  560.  
  561.     cout << endl << endl;
  562.  
  563.     while(1)
  564.     {
  565.         logFileIn >> log.accountID;
  566.         logFileIn >> log.typeOfOperation;
  567.         logFileIn >> log.amountOfMoney;
  568.         logFileIn >> log.date.day;
  569.         logFileIn >> log.date.month;
  570.         logFileIn >> log.date.year;
  571.  
  572.         if(logFileIn.eof())
  573.             break;
  574.         cout << "Account ID:\t\t" << log.accountID << endl;
  575.         cout << "Type of operation:\t";
  576.         if(log.typeOfOperation == 1)
  577.             cout << "Deposit" << endl;
  578.         else
  579.             cout << "Withdrawal" << endl;
  580.         cout << "Amount:\t\t\t" << log.amountOfMoney << endl;
  581.         cout << "Date:\t\t\t" << log.date.day << "-" << log.date.month << "-" << log.date.year << endl;
  582.         cout << "-------------------" << endl;
  583.     }
  584.  
  585.     cout << endl << endl << endl;
  586.     logFileIn.close();
  587. } */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement