Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <vector>
- #include <stdlib.h>
- #include "Date.h"
- using namespace std;
- enum Genre
- {
- FICTION = 1,
- HORROR,
- CRIME,
- DRAMA,
- SCIFI,
- THRILLER,
- BIO
- };
- class Book
- {
- private:
- string name;
- string ISBN;
- string author;
- Genre genre;
- string defaultISBN = "000-000-000-F";
- string defaultName = "Default";
- bool checkedOut;
- public:
- Book(string name,string ISBN,string author,Genre genre)
- :name(name),ISBN(ISBN),author(author),checkedOut(false),genre(genre)
- {
- if(!validISBN(ISBN))
- {
- ISBN = defaultISBN;
- cout << "not a valid ISBN, defaulting the ISBN to : " << defaultISBN << endl;
- }
- if(!validAuthor(author))
- {
- author = defaultName;
- cout << "not a valid name,defaulting the author to :" << defaultName << endl;
- }
- }
- Book() {}
- Book(const Book& other)
- {
- name = other.name;
- ISBN = other.ISBN;
- author = other.author;
- genre = other.genre;
- checkedOut = false;
- if(!validISBN(ISBN))
- {
- ISBN = defaultISBN;
- cout << "not a valid ISBN, defaulting the ISBN to : " << defaultISBN << endl;
- }
- if(!validAuthor(author))
- {
- author = defaultName;
- cout << "not a valid name,defaulting the author to :" << defaultName << endl;
- }
- }
- string getName() const
- {
- return name;
- }
- string getISBN() const
- {
- return ISBN;
- }
- string getAuthor()const
- {
- return author;
- }
- bool isCheckedOut() const
- {
- return checkedOut;
- }
- bool checkOutBook()
- {
- if(checkedOut)
- {
- cout << "sorry book already checked out" << endl;
- return false;
- }
- else
- {
- checkedOut = true;
- return true;
- }
- }
- void returnBook()
- {
- checkedOut = true;
- }
- bool operator==(const Book& other)
- {
- return ISBN == other.ISBN;
- }
- bool operator!=(const Book& other)
- {
- return !(ISBN == other.ISBN);
- }
- friend ostream& operator<<(ostream& os,const Book& other);
- bool validISBN(string i)
- {
- int second;
- int third;
- int fouth;
- int fifth;
- for(int j = 0; j < i.size(); j++)
- {
- if(i.at(j) == '-')
- {
- second = j;
- break;
- }
- if(!isdigit(i.at(j)))
- {
- return false;
- }
- }
- for(int j = second+1; j < i.size(); j++)
- {
- if(i.at(j) == '-')
- {
- third = j;
- break;
- }
- if(!isdigit(i.at(j)))
- {
- return false;
- }
- }
- for(int j = third+1; j < i.size(); j++)
- {
- if(i.at(j) == '-')
- {
- fifth = j;
- break;
- }
- if(!isdigit(i.at(j)))
- {
- return false;
- }
- }
- for(int j = fifth+1; j < i.size(); j++)
- {
- if(i.at(j) == '-')
- {
- return false;
- }
- if(isdigit(i.at(j)))
- {
- return false;
- }
- }
- return true;
- }
- bool validAuthor(string n)
- {
- for(int i = 0; i < n.size(); i++)
- {
- if(isdigit(n.at(i)))
- {
- return false;
- }
- return true;
- }
- }
- };
- ostream& operator<<(ostream& os,const Book& other)
- {
- os << "Name: " << other.getName() << " Author: " << other.getAuthor() << " ISBN: " <<other.getISBN() << endl;
- return os;
- }
- class Patron
- {
- public:
- string name;
- string cardNumber;
- int fees;
- bool owesFees;
- Patron(string name,string cardNumber):
- name(name),cardNumber(cardNumber),fees(0),owesFees(false)
- {}
- Patron() {}
- Patron(const Patron& other)
- {
- name = other.name;
- cardNumber = other.cardNumber;
- fees = other.fees;
- owesFees = other.owesFees;
- }
- string getName() const
- {
- return name;
- }
- string getCardNumber() const
- {
- return cardNumber;
- }
- int getFees() const
- {
- return fees;
- }
- bool getOwesFees() const
- {
- return owesFees;
- }
- };
- bool checkFees(const Patron& p)
- {
- return p.getOwesFees();
- }
- class Library
- {
- public:
- vector<Patron> patrons;
- vector<Book> books;
- vector<Dates::Date> dates;
- Dates::Date today;
- struct Transaction
- {
- Patron patron;
- Book book;
- Dates::Date dateTaken;
- Dates::Date dateDue;
- };
- vector<Transaction> transactions;
- void setDate(Dates::Date d)
- {
- today = d;
- }
- void printDate()
- {
- cout << "Today's date :" <<
- today.getDay() << "/" << today.getMonth() << "/" << today.getYear().y << endl;
- }
- bool addPatron(ofstream& storePatrons)
- {
- string name;
- string cardNumber;
- char choice;
- cout << "Enter the name of the Patron" << endl;
- getline(cin,name);
- cout << "Enter card Number of Patron" << endl;
- getline(cin,cardNumber);
- for(int i = 0; i < patrons.size(); i++)
- {
- if(name == patrons[i].getName())
- {
- cout << "the library already has a patron by that name"
- << "is it another person? type y for yes n for no" << endl;
- cin >> choice;
- cin.get();
- if(choice == 'n')
- {
- cout << "what the heck" << endl;
- return false;
- }
- }
- if(cardNumber == patrons[i].getCardNumber())
- {
- cout << "false : cardNumber = cardnumber" << endl;
- return false;
- }
- }
- storePatrons << name << endl;
- storePatrons << cardNumber << endl;
- patrons.push_back(Patron(name,cardNumber));
- return true;
- }
- bool addBook(ofstream &storeBooks)
- {
- string name;
- string author;
- string ISBN;
- Genre genre;
- char choice;
- cout << "Enter the name of the Book" << endl;
- getline(cin,name);
- cout << "Enter name of the Author" << endl;
- getline(cin,author);
- cout << "Enter ISBN" << endl;
- getline(cin,ISBN);
- // implement genre ask user to choose genre
- genre = BIO;
- for(int i = 0; i < books.size(); i++)
- {
- if(name == books.at(i).getName() && author == books.at(i).getAuthor())
- {
- cout << "the library already has a book by that Author"
- << "is it another book? type y for yes n for no" << endl;
- cin >> choice;
- cin.get();
- if(choice == 'n')
- {
- cout << "first false" << endl;
- return false;
- }
- }
- if(ISBN == books.at(i).getISBN())
- {
- cout << "sec false" << endl;
- return false;
- }
- }
- cout << "adding book" << endl;
- books.push_back(Book(name,ISBN,author,BIO));
- storeBooks << name << endl;
- storeBooks << ISBN << endl;
- storeBooks << author << endl;
- return true;
- }
- void readFile(ifstream& in,ifstream& inPatron,ifstream& inTrans)
- {
- // read in Books
- string name;
- string ISBN;
- string author;
- Genre genre;
- while(in)
- {
- getline(in,name);
- if(!in)
- {
- break;
- }
- getline(in,ISBN);
- getline(in,author);
- genre = HORROR;
- Book temp(name,ISBN,author,genre);
- books.push_back(temp);
- }
- // read in Patrons
- string patronName;
- string cardNumber;
- while(inPatron)
- {
- getline(inPatron,patronName);
- if(!inPatron)
- {
- break;
- }
- getline(inPatron,cardNumber);
- Patron temp(patronName,cardNumber);
- patrons.push_back(temp);
- }
- // read in Transactions
- while(inTrans)
- {
- int day;
- Dates::Month month;
- int year;
- string dayStr;
- string monthStr;
- string yearStr;
- stringstream ss;
- getline(inTrans,dayStr,',');
- if(!inTrans){
- break;
- }
- ss << dayStr;
- ss >> day;
- ss.str( std::string() );
- ss.clear();
- getline(inTrans,monthStr,',');
- int monthInt;
- ss << monthStr;
- ss >> monthInt;
- month = Dates::Month(monthInt);
- ss.str( std::string() );
- ss.clear();
- getline(inTrans,yearStr,',');
- ss << yearStr;
- ss >> year;
- ss.str( std::string() );
- ss.clear();
- Year yr(year);
- Dates::Date dateTaken(day,month,yr);
- getline(inTrans,dayStr,',');
- ss << dayStr;
- ss >> day;
- ss.str( std::string() );
- ss.clear();
- getline(inTrans,monthStr,',');
- ss << monthStr;
- ss >> monthInt;
- month = Dates::Month(monthInt);
- ss.str( std::string() );
- ss.clear();
- getline(inTrans,yearStr,',');
- ss << yearStr;
- ss >> year;
- ss.str( std::string() );
- ss.clear();
- yr = Year(year);
- Dates::Date dateDue(day,month,yr);
- string cardNumber;
- string ISBN;
- getline(inTrans,cardNumber,',');
- Patron p;
- for(int i = 0; i < patrons.size(); i++)
- {
- if(cardNumber == patrons.at(i).getCardNumber())
- {
- p = patrons.at(i);
- }
- }
- getline(inTrans,ISBN);
- Book b;
- for(int i = 0; i < books.size(); i++)
- {
- if(ISBN == books.at(i).getISBN())
- {
- b = books.at(i);
- }
- }
- Transaction t;
- t.book = b;
- t.patron = p;
- t.dateTaken = dateTaken;
- t.dateDue = dateDue;
- transactions.push_back(t);
- }
- // check if book is in the open transactions if it is check it out
- bool checkedOut = false;
- for(int i = 0; i < transactions.size(); i++){
- for(int j = 0; j < books.size(); j++){
- if(transactions.at(i).book.getISBN() == books.at(i).getISBN() && !checkedOut){
- books.at(i).checkOutBook();
- checkedOut = true;
- }
- }
- }
- }
- void printBooks()
- {
- for(int i = 0; i < books.size(); i++)
- {
- cout << books.at(i);
- if(books.at(i).isCheckedOut())
- {
- cout << " : Above book checked out,currently not available" << endl;
- }
- }
- }
- void printPatrons()
- {
- for(int i = 0; i < patrons.size(); i++)
- {
- cout << patrons.at(i).getName() << " card Number :" << patrons.at(i).getCardNumber() << endl;
- }
- }
- bool searchByAuthor(string a)
- {
- bool print = true;
- bool found = false;
- for(int i = 0; i < books.size(); i++)
- {
- if(a == books.at(i).getAuthor())
- {
- found = true;
- if(print)
- {
- cout << "results found for that author " << endl;
- print = false;
- }
- cout << "Author : " << books.at(i).getAuthor() <<
- " Book title : " << books.at(i).getName() << endl;
- }
- }
- if(found)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- bool searchByName(string n)
- {
- bool print = true;
- bool found = false;
- for(int i = 0; i < books.size(); i++)
- {
- if(n == books.at(i).getName())
- {
- found = true;
- if(print)
- {
- cout << "results found for that name " << endl;
- print = false;
- }
- cout << "Author : " << books.at(i).getAuthor() <<
- " Book title : " << books.at(i).getName() << endl;
- }
- if(found)
- {
- return true;
- }
- else
- {
- cout << "could not find any results by that name" << endl;
- return false;
- }
- }
- }
- bool searchByISBN(string is)
- {
- bool print = true;
- bool found = false;
- for(int i = 0; i < books.size(); i++)
- {
- if(is == books.at(i).getISBN())
- {
- found = true;
- if(print)
- {
- cout << "results found for that author " << endl;
- print = false;
- }
- cout << "Author : " << books.at(i).getAuthor() <<
- " Book title : " << books.at(i).getName() << endl;
- }
- }
- if(found)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- bool addTransaction(ofstream &trans,string ISBN,string cn)
- {
- Transaction t;
- bool conditionOne = false;
- bool conditionTwo = false;
- for(int i = 0; i < books.size(); i++)
- {
- if(ISBN == books.at(i).getISBN())
- {
- conditionOne = true;
- t.book = books.at(i);
- }
- for(i = 0; i < patrons.size(); i++)
- {
- if(cn == patrons.at(i).getCardNumber())
- {
- conditionTwo = true;
- t.patron = patrons.at(i);
- }
- }
- t.dateTaken = today;
- int todaysDay = today.getDay();
- t.dateDue = Dates::Date(todaysDay+14,Dates::APR,Year(2018));
- if(conditionOne && conditionTwo)
- {
- transactions.push_back(t);
- trans << t.dateTaken.getDay();
- trans << ",";
- trans << t.dateTaken.getMonth();
- trans << ",";
- trans << t.dateTaken.getYear().y;
- trans << ",";
- trans << t.dateDue.getDay();
- trans << ",";
- trans << t.dateDue.getMonth();
- trans << ",";
- trans << t.dateDue.getYear().y;
- trans << ",";
- trans << cn;
- trans << ",";
- trans << ISBN;
- trans << endl;
- return true;
- }
- return false;
- }
- }
- bool resereveBook()
- {
- string ISBN;
- cout << "enter ISBN" << endl;
- cin >> ISBN;
- for(int i = 0; i < books.size(); i++)
- {
- if(ISBN == books.at(i).getISBN() && !books.at(i).isCheckedOut())
- {
- books.at(i).checkOutBook();
- cout << "This book has now been checked out for you" << endl;
- return true;
- }
- }
- return false;
- }
- };
- bool driver()
- {
- ofstream storeBooks;
- ofstream storePatrons;
- ofstream storeTransactions;
- ifstream readBooks;
- ifstream readPatrons;
- ifstream readTransactions;
- bool adminMode = false;
- string userName = "admin";
- string password = "admin";
- string userEntered;
- string passEntered;
- storeBooks.open("storeBooks.txt",ofstream::app);
- storePatrons.open("storePatrons.txt",ofstream::app);
- storeTransactions.open("storeTransactions.txt",ofstream::app);
- readBooks.open("storeBooks.txt");
- readPatrons.open("storePatrons.txt");
- readTransactions.open("storeTransactions.txt");
- Library Library;
- Dates::Date today(11,Dates::APR,Year(2018));
- Library.setDate(today);
- Library.readFile(readBooks,readPatrons,readTransactions);
- cout << "*****WELCOME TO THE LIBRARY SYSTEM*****" << endl;
- cout << "enter username" << endl;
- getline(cin,userEntered);
- cout << "enter password" << endl;
- getline(cin,passEntered);
- while(true)
- {
- if(userEntered == userName && passEntered == password)
- {
- cout << "press (1) to print books by name" << endl;
- cout << "press (2) to print books by ISBN" << endl;
- cout << "press (3) to print view all books" << endl;
- cout << "press (4) to reserve book for patron" << endl;
- cout << "press (5) to add patron" << endl;
- cout << "press (6) to view all patrons" << endl;
- cout << "press (7) to add a book to Library" << endl;
- cout << "press (8) to view transactions" << endl;
- cout << "press (9) to quit" << endl;
- cout << "press (10) to print today's date" << endl;
- cout << "press (11) to clear screen(clean up)" << endl;
- int choice;
- cin >> choice;
- cin.get();
- switch(choice)
- {
- case 1:
- {
- string name;
- cout << "Enter name of book" << endl;
- getline(cin,name);
- Library.searchByName(name);
- }
- break;
- case 2:
- {
- string ISBN;
- cout << "enter author" << endl;
- getline(cin,ISBN);
- Library.searchByISBN(ISBN);
- }
- break;
- case 3:
- Library.printBooks();
- break;
- case 4:
- {
- cout << "**Reserve a book** " << endl;
- cout << "please enter the card number of the patron" << endl;
- string cn;
- getline(cin,cn);
- for(int i = 0; i < Library.patrons.size(); i++)
- {
- if(cn == Library.patrons.at(i).getCardNumber())
- {
- cout << "please enter the ISBN of the book you want to reserve for patron " << endl;
- string ISBN;
- getline(cin,ISBN);
- for(int i = 0; i < Library.books.size(); i++)
- {
- if(ISBN == Library.books.at(i).getISBN())
- {
- if(Library.books.at(i).checkOutBook())
- {
- Library.addTransaction(storeTransactions,ISBN,cn);
- }
- }
- }
- }
- }
- }
- break;
- case 5:
- Library.addPatron(storePatrons);
- break;
- case 6:
- Library.printPatrons();
- break;
- case 7:
- Library.addBook(storeBooks);
- break;
- case 8:
- for(int i = 0; i < Library.transactions.size(); i++)
- {
- cout << " Patron card number : " << Library.transactions.at(i).patron.getCardNumber()
- << " Book ISBN : " << Library.transactions.at(i).book.getISBN() <<
- "date checked out : " << Library.transactions.at(i).dateTaken.getDay() <<
- "/" << Library.transactions.at(i).dateTaken.getMonth() <<
- "/" << Library.transactions.at(i).dateTaken.getYear().y <<
- " date due back : " << Library.transactions.at(i).dateDue.getDay() <<
- "/" << Library.transactions.at(i).dateDue.getMonth() <<
- "/" << Library.transactions.at(i).dateDue.getYear().y << endl;
- // NEXT DAY IMPLEMENT A WAY TO CHECK IF FEE HAS BEEN PAID
- // DELETE TRANSACTION FROM DATABASE FILE IF IT HAS
- // ALSO CHECK ALL TRANSACTIONS TO SEE IF DATE IS DUE BACK AND IF IT IS PAST THAT
- // ADD FINES TO PATRONS ACCOUNT
- }
- break;
- case 9:
- return true;
- case 10:
- Library.printDate();
- break;
- case 11:
- system("cls");
- }
- }
- else
- {
- return false;
- }
- }
- return true;
- }
- int main()
- {
- driver();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement