CS21 Lab 10: the Swindle

Due 11:59pm **Wednesday**, Apr 18 2012

Run update21, if you haven't already, to create the cs21/labs/10 directory. Then cd into your cs21/labs/10 directory and create the python programs for lab 10 in this directory (handin21 looks for your lab 10 assignments in your cs21/labs/10 directory).

$ update21
$ cd cs21/labs/10

Introduction

Printed books are so old school. The *it* technology product everyone is clamoring for are electronic readers, or e-readers as they call them on the street. In this lab, you are going to throw your hat into the e-reader market and develop your very own Swarthmore e-reader a.k.a. the Swindle. Any resemblance to current e-readers in the market is mere coincidence.

In this lab, you will learn object-oriented programming to create a virtual e-reader program. You will define two classes and create several instances of these classes. The first class you will develop is the Book class which encapsulates one electronic format book (or ebook). We have made several free ebooks available for you to load and experiment with once you get your Book class working.

Second, you will create your Swindle class which encapsulates the concept of an e-reader. You will be able to perform several core operations of an e-reader, including buying books, picking a book from your virtual shelf, and finally reading a book.

To get started, take note of the files we have provided for you:


The Book Class

For this lab you should implement each class and test them before you move on to the next step. Start with the Book class.

A Book object represents an electronic book to read on your e-reader. You should define your class such that each book maintains the following data (also called instance variables):

As discussed in class, objects interact via methods. Your Book class should define the following methods: At this point, you should be able to test out your Book class. Use your unit testing skills to create an instance of a Book in a test program or in the Python interpreter and test out all of your methods. It is not a good idea to start implementation on the Swindle class until you thoroughly test your Book class. The ninjas will ask you to go back and test your Book class if it is clear that you did not do so already.

Here's an example of testing the Book class:

>>> from book import *
>>> mybook = Book("Gettysburg Address", "Abe Lincoln", 1863, '/usr/local/doc/GettysburgAddress')
>>> print mybook
    Title: Gettysburg Address
   Author: Abe Lincoln
     Year: 1863
 Bookmark: on page 0
 Filename: /usr/local/doc/GettysburgAddress
>>> print mybook.getText()
  Four Score and seven years ago our fathers brought forth on
this continent a new nation, conceived in Liberty, and dedicated
...

BOOK API SUMMARY:


The Swindle Class
The Swindle class will define your very own (virtual) e-reader! Every instance must maintain a few pieces of data: We have provided the beginning of the class definition by defining the constructor (which includes the functionality for loading the database of possible books). You only need to add one or two lines of code where we have added the comment:
#TODO: using the information just obtained from the file,
# create a Book object with this data and add it to the
# appropriate list
We have also provided a display_text() method that takes care of some details involved in displaying a certain number of lines from the book. You should read swindle.py and understand these methods before proceeding.

To finish up your class definition, we need to define the interface for the Swindle; that is, the list of the methods defined for the object. You should define methods that:

Here's an example of testing the Swindle class:

>>> from swindle import *
>>> myreader = Swindle("George Washington")
>>> print myreader
  Owner: George Washington
  Number of Books: 0
  Titles:

BOOK API SUMMARY: