Simple Classes Assignment

Simple Classes Assignment

CPSC 301

Simple Classes Assignment

This assignment is very similar to the program you did earlier with books. Please note the changes.

Function: This program reads in information about books being checked out and prints out a table containing that information formatted in a different way. It asks the user to enter the current date. It then presents the user with a simple menu: the letter C to check out, T for the list of books checked out so far, and Q for quitting the program. When C is entered, it will read in information about the book, set its check out date to be the current date, set its due date to be two weeks from the current date, and enter the information in a table. When T is entered, the table printed out should include the checkout date and the return date. The books do not need to be in any sorted order, but you can have them sorted if you like.

Input: The input will be from the keyboard (standard input). The program asks the user to enter the current date using integers for month, day and year. In the interactive part, the user enters one of the following letters: C, T, or Q. If the letter C is entered, the user is asked to input a line consisting the first and last name of the author followed by the title of the book.

Output: Any time the letter T is entered, the program should print a list containing the information about all books checked out so far. The list should contain for each book the last name, first name, title, the check out date, and the return date. (See sample run below.)

Sample run: (You may change the wording and format, but all information asked for should be present and easily read. The user input is in bold type.)

Welcome to CS library program.

Please enter the date in integers according to the following format -

mm dd yyyy: 10 23 2005

Please enter your one letter choice as follows:

C: Check out a book

T: Print all books checked out so far ordered by title

Q: quit this program

C

Please enter one line consisting the first and last names of the author followed by the title of the book.

Ernest Hemingway For Whom the Bell Tolls

Please enter your one letter choice as follows:

C: Check out a book

T: Print all books checked out so far

Q: quit this program

C

Please enter one line consisting the first and last names of the author followed by the title of the book.

Stephen Crane The Red Badge of Courage

Please enter your one letter choice as follows:

C: Check out a book

T: Print all books checked out so far

Q: quit this program

T

AuthorTitleCheck Out DateDue Date

Hemingway, ErnestFor Whom the Bells TollsOctober 23, 2005November 6, 2005

Crane, StephenThe Red Badge of CourageOctober 23, 2005November 6, 2005

Please enter your one letter choice as follows:

C: Check out a book

T: Print all books checked out so far

Q: quit this program

Q

Processing requirements: You need to create and use two classes: the Date class and the Book class. For the Date class, you may copy what is at the end of this assignment with the following changes:

  1. Remove the pre- and post-increment operators from the class.
  2. The overloaded operator for += will have the prototype

void operator+=(int); // Does not return anything

3.Both the leapYear and the endOfMonth functions will have no parameters so they will have prototypes

bool leapYear( ) const; // is the year for the date a leap year?

bool endOfMonth( ) const; // is the date the last day of the month?

Note that this will mean that corresponding changes will need to be made for all function definitions that use the two functions.

For the class Book, you should have the following interface (public) member functions:

Book ( const char*, const char*, const char*, Date, Date);

~Book();

Book & setCheckOutDate ( Date);

Book & setDueDate( Date);

Book & setFirstName ( const char*);

Book & setLastName ( const char*);

Book & setTitle ( const char*):

const char* getFirstName();

const char* getLastName();

const char* getTitle();

Date getCheckOutDate();

Date getDueDate();

const Book & operator= ( const Book ); //OPTIONAL overloaded = operator

static int getCount();

The following private data members should be defined:

char firstName[30];

char lastName[30];

char title[30];

Date checkOut;

Datedue;

static int count;

You also may, but do not need to, overload the = operator to copy book objects. The examples of Employee class in Section 10.3 (Composition) and in Section 10.7 (static Class members) may help you with the implementations for the class Book.

Assumptions: You may assume that all entries are correct. The above makes assumptions about the lengths of names and titles; you need to document it. You may also assume that all books have just one author and only the first and last names of the authors are entered.

Testing: You should perform your own tests to make sure that each component of your program is working. At the end, run your program using the input in the file as given in the file newbooks.txt.

Optional extra: Have another option of sorting the table by the last names of the authors.

As always, your program should include the following internal documentation:

  • Your name, date, assignment topic
  • For each function including the main:

oa purpose that explain what the function does, making sure to include the role played by each parameter

oany assumptions made or credits due

Date class (Before changes asked for above)

// Definition of class Date

#ifndef DATE1_H

#define DATE1_H

#include <iostream>

using std::ostream;

class Date {

friend ostream &operator<( ostream &, const Date & );

public:

Date( int m = 1, int d = 1, int y = 1900 ); // constructor

void setDate( int, int, int ); // set the date

Date &operator++(); // preincrement operator

Date operator++( int ); // postincrement operator

const Date &operator+=( int ); // add days, modify object

bool leapYear( int) const; // is this a leap year?

bool endOfMonth( int ) const; // is this end of month?

private:

int month;

int day;

int year;

static const int days[]; // array of days per month

void helpIncrement(); // utility function

};

#endif

// Member function definitions for Date class

#include <iostream>

#include "date.h"

// Initialize static member at file scope;

// one class-wide copy.

const int Date::days[] = { 0, 31, 28, 31, 30, 31, 30,

31, 31, 30, 31, 30, 31 };

// Date constructor

Date::Date( int m, int d, int y ) { setDate( m, d, y ); }

// Set the date

void Date::setDate( int mm, int dd, int yy )

{

month = ( mm >= 1 & mm <= 12 ) ? mm : 1;

year = ( yy >= 1900 & yy <= 2100 ) ? yy : 1900;

// test for a leap year

if ( month == 2 & leapYear(year ) )

day = ( dd >= 1 & dd <= 29 ) ? dd : 1;

else

day = ( dd >= 1 & dd <= days[ month ] ) ? dd : 1;

}

// Preincrement operator overloaded as a member function.

Date &Date::operator++()

{

helpIncrement();

return *this; // reference return to create an lvalue

}

// Postincrement operator overloaded as a member function.

// Note that the dummy integer parameter does not have a

// parameter name.

Date Date::operator++( int )

{

Date temp = *this;

helpIncrement();

// return non-incremented, saved, temporary object

return temp; // value return; not a reference return

}

// Add a specific number of days to a date

const Date &Date::operator+=( int additionalDays )

{

for ( int i = 0; i < additionalDays; i++ )

helpIncrement();

return *this; // enables cascading

}

// If the year is a leap year, return true;

// otherwise, return false

bool Date::leapYear( int testYear ) const

{

if ( testYear % 400 == 0 || ( testYear % 100 != 0 & testYear % 4 == 0 ) )

return true; // a leap year

else

return false; // not a leap year

}

// Determine if the day is the end of the month

bool Date::endOfMonth(int testDay ) const

{

if ( month == 2 & leapYear( year ) )

return (testDay == 29); // last day of Feb. in leap year

else

return (testday == days[ month ]);

}

// Function to help increment the date

void Date::helpIncrement()

{

if ( ! endOfMonth( day )) { // date is not at the end of the month

day++

}

else if (month < 12 ) { // date is at the end of the month, but month < 12

day = 1;

++month;

}

else // end of month and year: last day of the year

{

day = 1;

month = 1;

++year;

}

}

// Overloaded output operator

ostream &operator<( ostream &output, const Date &d )

{

static char *monthName[ 13 ] = { "", "January",

"February", "March", "April", "May", "June",

"July", "August", "September", "October",

"November", "December" };

output < monthName[ d.month ] < ' '

< d.day < ", " < d.year;

return output; // enables cascading

}

1