Data Structures

PA 01

Writing your own classes


Code due by Thursday, September 10th at noon

Paperwork due Friday, September 11th at the start of class


Introduction

Suppose you were asked to model a simple library system.  In that system you will need data/objects to represent the patrons in the library and the books that can be checked out from the library.  In this activity you will create two classes to represent these objects in a simplified scenario.

Make sure you read the directions carefully.  This assignment consists of several pieces and you need to complete all of the pieces for full credit. I strongly suggest that you either work in the labs and store your materials on your p: drive or that you carry them with you on a flash drive so that you can easily ask me or a TA for advise on the code if you encounter problems.


The Patron Class

The Patron class keeps track of information about each patron of the library.  Each instance of the Patron class keeps track of the name of the patron and how many books that patron currently has checked out.  There is a limit of three books per patron.

In a file called patron.py develop the Patron class as described

Detailed descriptions of the Patron class methods

Method Example Usage Description
__init__
(Patron Constructor)
patron1 = Patron("Ben Schafer") Constructs a patron with the specified name and sets the number of books checked out by that patron to 0.
getName patron1.getName() Returns a string which is the name of the patron
getNumberOfBooksOut patron1.getNumberOfBooksOut() Returns the integer representing how many books the patron currently has checked out
checkOut patron1.checkOut() If the patron does not yet have 3 books checked out it adds one to his/her count of books out and returns True.  If the patron already has 3 books out the method returns False.
returnBook patron1.returnBook() If the patron has books checked out this method decrements one from his/her count of books and returns True.  If the patron has no books out at the moment the method returns False.
__str__ print  patron1 Returns a formatted string indicating the name of the patron and how many books are checked out.  For example, the command to the left might produce

Ben Schafer currently has 2 books checked out.

Hints:

Pay attention to not only the "proper" usage of the class, but what should happen during "improper" usage.  For example, notice what should happen if someone attempts to say:

>>> patron1 = Patron("Ben Schafer")
>>> patron1.checkOut()
>>> patron1.checkOut()
>>> patron1.checkOut()
>>> patron1.checkOut()
>>> print patron1

The Book Class

The Book class keeps track of information about each book in the library.  Each instance of the Book class knows the title and author of the book, the patron who currently has the book checked out, and a waiting list of Patrons waiting to check out the book. When a patron wants to borrow a book, that code first checks to see if the book is available. If it is not available, the patron is placed on a waiting list. If it is available, the code attempts to have the Patron check out the book

In a file called book.py develop the Patron class as described

Detailed descriptions of the Book class methods

Method Example Usage Description
__init__
(Book Constructor)
book1 = Book("Fundamentals of Python","Lambert") Constructs a book object with the title and author that are provided as parameters. Also sets up a waiting list (currently empty) and a variable to store who currently has the book checked out (also currently "empty")
getTitle book1.getTitle() Returns a string which is the title of the book
getAuthor book1.getAuthor() Returns a string which is the author of the book
getPatron book1.getPatron() Returns a Patron object which is the patron currently holding the book
borrowMe book1.borrowMe(patron1) borrowMe() takes a Patron object as a parameter. The code first checks to see if the book is checked out. If the book is already checked out then it adds the patron to the waiting list. If the book is available it checks to see if the given patron can still check out a book (doesn't already have 3 books). If they can still check out the book then the book remembers who has the book. If they already hold 3 books then no significant change should take place for the book or the patron.

The method should print a message in the end which says one of three things depending on what actually happened.

Finally, the method should return True or False indicating whether or not the book was successfully checked out to the listed patron.

The three conditions are

  • "Book checked out to the patron" (True)
  • "Patron added to the waiting list" (False)
  • "Patron cannot check out any more books" (False)

 

returnMe book1.returnMe() First, the book looks at the patron who has the book and decrements his/her book count. Next the book marks its Patron variable as "empty" Finally, the book sees if there is a waiting list. If there is, it attempts to check out the book to the people on the waiting list. Of course, to be proper, if the first person on the list already has 3 books then the code should "offer" the book to the next person on the list until it either finds a patron who can have the book or it reaches the end of the list.
__str__ print book1 Returns a formatted string indicating the title and author of the book and, if appropriate, the name of the person who currently has the book checked out.

"Why I Rock" by Ben Schafer is currently available for checkout.


Testing your code

To get started, download the following file:

This file contains a method called test() which begins to test your Book and Patron objects. You should run this initial test to see if there are obvious problems. However, you should also configure additional tests to see if you can look at all of the possible use cases.


Submitting Your Assignment

To get full credit for this assignment you must: