// This is the answer to test1 part1 // written by Peter Clarke // #include #include #include #include //---------------------------------- // Define a Book class as requested class Book { private: std::string m_title ; // Book title std::string m_author ; // Book author int m_isbn ; // ISBN number bool m_onLoan ; // Indicates if on loan int m_returnDate ; // Day in year book is on loan until int m_borrowerIdentity;// Library Borrower identity code public: // ------------------------------------------- // Initialisation method void initialise( std::string title, std::string author, int isbn ) { // Initialise some things from the arguments passed to this method m_title = title ; m_author = author ; m_isbn = isbn ; // Initialise other things to sensible default values m_onLoan = false ; m_returnDate = 365 ; m_borrowerIdentity = 0 ; return ; } // -------------------------------------------- // Method to set the book to be on loan and set the relevant member variables void setLoaned( int borrower, int returnDate ) { m_onLoan = true ; m_returnDate = returnDate ; m_borrowerIdentity = borrower ; } // --------------------------------------------- // Method to tell the caller if the book is on laon and overdue bool isOverdue( int todaysDate ) { // Test is the return date is before todays date if( m_onLoan && (m_returnDate < todaysDate) ) { return true ; } else { return false ; } } // ----------------------------------------------- // Method to print out title and author to screen void dump( ) { std::cout << "This book is called " << m_title << " and is written by " << m_author << std::endl ; } // ---------------------------------- // Method added for part three int borrower() { return m_borrowerIdentity ; } }; // End of Book class // ------------------------------------------------ class LoanChecker { private: // It will be initialised to have a vector of books set inside it and todays date std::vector m_bookList ; int m_todaysDate ; public: // -------------------------------------------- // This is the initialisation method void initialise( std::vector bookList, int todaysDate ) { m_bookList = bookList; m_todaysDate = todaysDate; } // -------------------------------------------- // This is the method to tell the caller how many are overdue int overdueCount( ) { int count ; for( int ind=0; ind << m_bookList.size() ; ++ind) { if( m_bookList[ind].isOverdue( m_todaysDate ) ) count++ ; } return count ; } // ---------------------------------------------------- // This is the method to print out the borrower id of overdue books void sendReminder( ) { // Open an output file std::ofstream outputFile( "badlist.dat" ) ; // Traverse the booklist for( int ind=0; ind < m_bookList.size() ; ++ind) { if( m_bookList[ind].isOverdue( m_todaysDate ) ) { std::cout << " Found overdue book loaned to borrower " << m_bookList[ind].borrower() << std::endl ; outputFile << " Overdue borrower id is " << m_bookList[ind].borrower() << std::endl ; } } return ; } }; // ---------------------------------------------- // Function to demonstrate part 1 void part1() { // announce std::cout << " \n This is the answer to part 1 \n " << std::endl ; // Make some books and initialise them Book bk1, bk2, bk3 ; bk1.initialise( "Startide Rising", "David Brin", 12345 ) ; bk2.initialise( "Excession", "Ian Banks", 45678 ) ; bk3.initialise( "Name of the Rose", "Umberto Eco", 65432 ) ; // Now dump them out bk1.dump() ; bk2.dump() ; bk3.dump() ; // Now set one on loan int borrowerId = 45 ; int returnTime = 160 ; bk2.setLoaned( borrowerId, returnTime ) ; // ----------------------------- // Now check which are on loan // set todays date int todaysDate = 170 ; if( bk1.isOverdue( todaysDate ) ) { std::cout << " Book1 is overdue " << std::endl ; bk1.dump() ; } if( bk2.isOverdue( todaysDate ) ) { std::cout << " Book2 is overdue " << std::endl ; bk2.dump() ; } if( bk3.isOverdue( todaysDate ) ) { std::cout << " Book3 is overdue " << std::endl ; bk3.dump() ; } return; } // ------------------------------- // Function to demonstrate part2 void part2() { // announce std::cout << " \n This is the answer to part 2 \n " << std::endl ; // First let us make a vector to store books in std::vector library ; // Now open the given file for reading and check it opned ok std::ifstream inputFile( "booklist.dat" ) ; if( ! inputFile ) { std::cout << " Couldnt open file " << std::endl ; return ; } // Now read in 10 books int numberToRead = 10 ; for( int ind=0; ind < numberToRead; ++ind ) { std::string title ; std::string author ; int isbn ; // Read in a line inputFile >> title >> author >> isbn ; // Create a new book Book newBook ; newBook.initialise( title, author, isbn ) ; // Add it to the vector library.push_back( newBook ) ; } // Now print them out to show this worked ok for( ind=0; ind < library.size(); ++ind ) { library[ind].dump() ; } // ---------------------------------------- // Here is the final bit to demonstrate part3 // First set some books to be overdue library[2].setLoaned( 67, 150 ) ; library[5].setLoaned( 98, 155 ) ; // Now make a loan checker and initialise it LoanChecker worker; int todaysDate = 165 ; worker.initialise( library, todaysDate ) ; // Now ask it to do the check worker.sendReminder( ) ; return; } // ---------------------------------------------- // Main function to test everything int main() { part1() ; part2() ; return 1 ; }