Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <cstdlib>
- #include <fstream>
- #include <iostream>
- #include <string>
- #define SIZE 10
- using namespace std;
- struct Date{
- int day, month, year;
- };
- struct Book{
- int bookID;
- string bookName;
- int rentID;
- };
- struct Rent{
- int rentID;
- Date startDate;
- Date endDate;
- int customerID;
- int bookID;
- };
- struct Customer{
- int customerID;
- string name;
- string surname;
- };
- /*
- struct LogFile{
- int accountID;
- int typeOfOperation;
- float amountOfMoney;
- Date date;
- };
- */
- bool searchForCustomer(int id);
- void createCustomer();
- bool searchForRent(int id);
- void createRent();
- bool searchForBook(int id);
- void showCustomerRents();
- void returnBook();
- void menu();
- int main()
- {
- menu();
- }
- void menu()
- {
- int choice;
- while(1)
- {
- cout << "Enter your choice" << endl;
- cout << "To make a new customer, press 1" << endl;
- cout << "To make a new rent, press 2" << endl;
- cout << "To return a book, press 3" << endl;
- cout << "To show rents by customer, press 4" << endl;
- //cout << "To print log file, press 6" << endl;
- cout << "To exit a program, press -1" << endl << endl;
- cin >> choice;
- if(choice == -1)
- {
- break;
- }
- switch(choice)
- {
- case 1: createCustomer(); break;
- case 2: createRent(); break;
- case 3: returnBook();break;
- case 4: showCustomerRents();break;
- // case 5: showBookRents();break;
- // case 6: printLogFile();break;
- default: cout << "You entered a wrong value, try again";
- cin >> choice;
- }
- }
- }
- void createCustomer()
- {
- Customer cust;
- int id;
- cout << "Please enter a new ID of your customer: ";cin >> id;
- while(1)
- {
- if(searchForCustomer(id) == false)
- {
- ofstream customersout;
- customersout.open("customers.txt", ofstream::app);
- if(customersout.fail())
- {
- cout << "Customers out file error.";
- exit(1);
- }
- cust.customerID = id;
- cout << "Enter a new name: ";
- cin >> cust.name;
- cout << "Enter a new surname: ";
- cin >> cust.surname;
- customersout << cust.customerID << " " << cust.name << " " << cust.surname << endl;
- customersout.close();
- break;
- }
- else
- {
- cout << "ID exists. Try again: "; cin >> id;
- }
- }
- }
- bool searchForCustomer(int id)
- {
- Customer cust;
- ifstream customersin;
- customersin.open("customers.txt");
- if(customersin.fail())
- {
- cout << "Customers in file error.";
- exit(1);
- }
- while(!customersin.eof())
- {
- customersin >> cust.customerID;
- customersin >> cust.name;
- customersin >> cust.surname;
- if(id == cust.customerID)
- {
- customersin.close();
- return true;
- }
- }
- customersin.close();
- return false;
- }
- void createRent()
- {
- Rent rnt;
- int custID, rentID, bookID;
- cout << "Please enter customer ID you want to make rent for: "; cin >> custID;
- while(1)
- {
- if(searchForCustomer(custID) == true)
- {
- cout << "Enter your rent ID: "; cin >> rentID;
- while(1)
- {
- if(searchForRent(rentID) == false)
- {
- rnt.customerID = custID;
- rnt.rentID = rentID;
- cout << "Enter starting day, month and year of rent: ";
- cin >> rnt.startDate.day >> rnt.startDate.month >> rnt.startDate.year;
- cout << "Enter ending day, month and year of rent: ";
- cin >> rnt.endDate.day >> rnt.endDate.month >> rnt.endDate.year;
- cout << "Enter a book ID you want to rent: "; cin>>bookID;
- while(1)
- {
- if(searchForBook(bookID)==true)
- {
- if(rentStatus(bookID)==true) {
- rnt.bookID=bookID;
- }
- else
- {
- cout<<"This book is already rented. Try another: "; cin>> bookID;
- }
- break;
- }
- else {
- cout << "This book ID doesn't exist. Try another: "; cin>>bookID;
- }
- }
- ofstream rentsout;
- rentsout.open("rents.txt", ofstream::app);
- string s, e;
- s=rnt.startDate.day+"/"+rnt.startDate.month+"/"+rnt.startDate.year;
- e=rnt.endDate.day+"/"+rnt.endDate.month+"/"+rnt.endDate.year;
- rentsout << rnt.rentID << " " << s<<" " <<e << " " << rnt.customerID <<" "<<rnt.bookID<< endl;
- rentsout.close();
- break;
- }
- else
- {
- cout << "Rent ID already exists. Try again: "; cin >> accID;
- }
- }
- break;
- }
- else
- {
- cout << "Customer ID doesn't exist. Try again: "; cin >> custID;
- }
- }
- }
- bool searchForRent(int id)
- {
- Rent rnt;
- string startD;
- string endD;
- ifstream rentin;
- rentin.open("rents.txt");
- if(rentin.fail())
- {
- cout << "Rents in file error.";
- exit(1);
- }
- while(!rentin.eof())
- {
- rentin >> rnt.rentID;
- rentin >> startD;
- rnt.startDate.day=startD.substr(0,2);
- rnt.startDate.month=startD.substr(3,2);
- rnt.startDate.year=startD.substr(6,4);
- rentin >> endD;
- rnt.endDate.day=endD.substr(0,2);
- rnt.endDate.month=endD.substr(3,2);
- rnt.endDate.year=endD.substr(6,4);
- rentin >> rnt.customerID;
- rentin >> rnt.bookID;
- if(id == rnt.rentID)
- {
- rentin.close();
- return true;
- }
- }
- rentin.close();
- return false;
- }
- bool searchForBook(int id) {
- Book book;
- ifstream bookin;
- bookin.open("books.txt");
- if(bookin.fail())
- {
- cout << "Books in file error.";
- exit(1);
- }
- while(!bookin.eof())
- {
- bookin >> book.bookID;
- bookin >> book.bookName;
- bookin >> book.rentID;
- if(id == book.bookID)
- {
- bookin.close();
- return false;
- }
- }
- bookin.close();
- return true;
- }
- bool rentStatus(int id) {
- Book book;
- ifstream bookin;
- bookin.open("books.txt");
- if(bookin.fail())
- {
- cout << "Books in file error.";
- exit(1);
- }
- while(!bookin.eof())
- {
- bookin >> book.bookID;
- bookin >> book.bookName;
- bookin >> book.rentID;
- if(id == book.bookID)
- {
- if(book.rentID==-1) {
- bookin.close();
- return true;
- }
- else {
- bookin.close();
- return false;
- }
- }
- }
- //never gets here cuz program logic
- bookin.close();
- return true;
- }
- void returnBook(){
- int customerID;
- int bookID;
- int rentID;
- Rent rnt;
- Book book;
- cout<< "Enter customer ID that is returning book: "; cin >> customerID;
- while(1) {
- if(searchForCustomer(customerID)==true) {
- cout<<"Enter book ID customer wants to return: "; cin>>bookID;
- ifstream rentin("rents.txt");
- if(rentin.fail())
- {
- cout << "Rents in file error.";
- exit(1);
- }
- ofstream tempRentsOut("temprents.txt");
- if(tempRentsOut.fail())
- {
- cout << "Temp rents file error.";
- exit(1);
- }
- while(1)
- {
- rentin >> rnt.rentID;
- rentin >> startD;
- rnt.startDate.day=startD.substr(0,2);
- rnt.startDate.month=startD.substr(3,2);
- rnt.startDate.year=startD.substr(6,4);
- rentin >> endD;
- rnt.endDate.day=endD.substr(0,2);
- rnt.endDate.month=endD.substr(3,2);
- rnt.endDate.year=endD.substr(6,4);
- rentin >> rnt.customerID;
- rentin >> rnt.bookID;
- if( rentin.eof() )
- break;
- if(customerID == rnt.customerID)
- {
- rentID=rnt.rentID;
- /*ofstream logFileOut("logfile.txt", ofstream::app);
- if(logFileOut.fail())
- {
- cout << "Log file error.";
- exit(1);
- }
- log.accountID = accID;
- log.amountOfMoney = deposit;
- log.typeOfOperation = 1; // 1 means deposit, 2 means withdrawal
- cout << "Enter day, month and year of transaction: ";
- cin >> log.date.day >> log.date.month >> log.date.year;
- logFileOut << log.accountID << " " << log.typeOfOperation << " " << log.amountOfMoney << " "
- << log.date.day << " " << log.date.month << " " << log.date.year << endl;
- logFileOut.close(); */
- }
- else
- {
- string s, e;
- s=rnt.startDate.day+"/"+rnt.startDate.month+"/"+rnt.startDate.year;
- e=rnt.endDate.day+"/"+rnt.endDate.month+"/"+rnt.endDate.year;
- tempRentsOut << rnt.rentID << " " << s<<" " <<e << " " << rnt.customerID <<" "<<rnt.bookID<< endl;
- }
- }
- tempRentsOut.close();
- rentin.close();
- ifstream bookin("books.txt");
- if(bookin.fail())
- {
- cout << "Books in file error.";
- exit(1);
- }
- ofstream tempBooksOut("tempbooks.txt");
- if(tempBooksOut.fail())
- {
- cout << "Temp books file error.";
- exit(1);
- }
- while(1)
- {
- bookin >> book.bookID;
- bookin >> book.bookName;
- bookin >> book.rentID;
- if( bookin.eof() )
- break;
- if(rentID == book.rentID)
- {
- book.rentID=-1;
- tempBooksOut<<book.bookID<<" "<<book.bookName<<" "<<book.rentID<<endl;
- /*ofstream logFileOut("logfile.txt", ofstream::app);
- if(logFileOut.fail())
- {
- cout << "Log file error.";
- exit(1);
- }
- log.accountID = accID;
- log.amountOfMoney = deposit;
- log.typeOfOperation = 1; // 1 means deposit, 2 means withdrawal
- cout << "Enter day, month and year of transaction: ";
- cin >> log.date.day >> log.date.month >> log.date.year;
- logFileOut << log.accountID << " " << log.typeOfOperation << " " << log.amountOfMoney << " "
- << log.date.day << " " << log.date.month << " " << log.date.year << endl;
- logFileOut.close(); */
- }
- else
- {
- tempBooksOut<<book.bookID<<" "<<book.bookName<<" "<<book.rentID<<endl;
- }
- }
- tempBooksOut.close();
- bookin.close();
- break;
- }
- else {
- cout<<"Customer ID doesn't exist, try again: "; cin >>customerID;
- }
- }
- }
- void showCustomerRents(){
- int custID;
- Rent rnt;
- string startD;
- string endD;
- cout<<"Enter customer ID whose rents you want to show: "; cin>>custID;
- ifstream rentin;
- rentin.open("rents.txt");
- if(rentin.fail())
- {
- cout << "Rents in file error.";
- exit(1);
- }
- while(!rentin.eof())
- {
- rentin >> rnt.rentID;
- rentin >> startD;
- rnt.startDate.day=startD.substr(0,2);
- rnt.startDate.month=startD.substr(3,2);
- rnt.startDate.year=startD.substr(6,4);
- rentin >> endD;
- rnt.endDate.day=endD.substr(0,2);
- rnt.endDate.month=endD.substr(3,2);
- rnt.endDate.year=endD.substr(6,4);
- rentin >> rnt.customerID;
- rentin >> rnt.bookID;
- if(custID == rnt.customerID)
- {
- cout<< rnt.rentID<<" "<<startD<<" "<<endD<<" "<< rnt.bookID;
- rentin.close();
- return true;
- }
- }
- rentin.close();
- return false;
- }
- /*
- void printLogFile()
- {
- LogFile log;
- ifstream logFileIn("logfile.txt");
- if(logFileIn.fail())
- {
- cout << "Log file error.";
- exit(1);
- }
- cout << endl << endl;
- while(1)
- {
- logFileIn >> log.accountID;
- logFileIn >> log.typeOfOperation;
- logFileIn >> log.amountOfMoney;
- logFileIn >> log.date.day;
- logFileIn >> log.date.month;
- logFileIn >> log.date.year;
- if(logFileIn.eof())
- break;
- cout << "Account ID:\t\t" << log.accountID << endl;
- cout << "Type of operation:\t";
- if(log.typeOfOperation == 1)
- cout << "Deposit" << endl;
- else
- cout << "Withdrawal" << endl;
- cout << "Amount:\t\t\t" << log.amountOfMoney << endl;
- cout << "Date:\t\t\t" << log.date.day << "-" << log.date.month << "-" << log.date.year << endl;
- cout << "-------------------" << endl;
- }
- cout << endl << endl << endl;
- logFileIn.close();
- } */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement