CS21 Lab 11: the Swindle

Due 11:59pm, Friday night, Apr 26, 2013

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

$ update21
$ cd cs21/labs/11

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 of 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 a display_text() method that takes care of some details involved in displaying a certain number of lines from the book. To finish up your swindle class definition, you should define these methods:

Here's an example of testing the Swindle class:

>>> from swindle import *
>>> myreader = Swindle("George Washington")
>>> print myreader
George Washington's Swindle!
----------------------------
Books you own:
...None so far...
>>> print myreader.getOwner()
George Washington
>>> myreader.showAvailable()
Available Books:
----------------

 1.         Alice in Wonderland by Lewis Carroll             (1865)
 2.         Pride and Prejudice by Jane Austen               (1813)
 3.                   Peter Pan by J. M. Barrie              (1911)
 4.                     Ulysses by James Joyce               (1922)
 5.      Mary Had A Little Lamb by Sarah Josepha Hale        (1830)
 6.               Metamorphosis by Franz Kafka               (1915)
 7.            Romeo and Juliet by William Shakespeare       (1591)
 8.        A Tale of Two Cities by Charles Dickens           (1859)
 9.   Tess of the d'Urbervilles by Thomas Hardy              (1891)
10.   The Count of Monte Cristo by Alexandre Dumas           (1844)
11.            The Time Machine by H. G. Wells               (1895)
12.             Treasure Island by Robert Louis Stevenson    (1883)

SWINDLE API SUMMARY:

Testing your classes with ereader.py

Once you have your Book and Swindle classes written, according to the above requirements, you should be able to run ereader.py. An example is shown below. Please test out all features of your classes by trying all options in ereader.py.

$ python ereader.py 


Since this is the first time you've used it,
let's customize your Swindle...


Please enter your name: Lisa Meeden


Welcome to Lisa Meeden's Swindle v0.1!!


----------------------------------------

Would you like to: 

1) Buy/See available books
2) See owned books
3) Read a book
4) Exit

    ---> 3

You don't own any books! Why don't you buy one now?
----------------------------------------

Would you like to: 

1) Buy/See available books
2) See owned books
3) Read a book
4) Exit

    ---> 7

Please choose an option from the list!
----------------------------------------

Would you like to: 

1) Buy/See available books
2) See owned books
3) Read a book
4) Exit

    ---> 1


Available Books: 
---------------  
 1:       Alice in Wonderland by Lewis Carroll        (1865)
 2:       Pride and Prejudice by Jane Austen          (1813)
 3:                 Peter Pan by J. M. Barrie         (1911)
 4:                   Ulysses by James Joyce          (1922)
 5:    Mary Had A Little Lamb by Sarah Josepha Hale   (1830)

Which book would you like to buy?  (0 to skip) ---> 1
----------------------------------------

Would you like to: 

1) Buy/See available books
2) See owned books
3) Read a book
4) Exit

    ---> 1



Available Books: 
---------------  
 1:       Pride and Prejudice by Jane Austen          (1813)
 2:                 Peter Pan by J. M. Barrie         (1911)
 3:                   Ulysses by James Joyce          (1922)
 4:    Mary Had A Little Lamb by Sarah Josepha Hale   (1830)

Which book would you like to buy?  (0 to skip) ---> 2
----------------------------------------

Would you like to: 

1) Buy/See available books
2) See owned books
3) Read a book
4) Exit

    ---> 2

  Books You Own: 
---------------  
 1:       Alice in Wonderland by Lewis Carroll        (1865)
 2:                 Peter Pan by J. M. Barrie         (1911)

----------------------------------------

Would you like to: 

1) Buy/See available books
2) See owned books
3) Read a book
4) Exit

    ---> 3



  Books You Own: 
---------------  
 1:       Alice in Wonderland by Lewis Carroll        (1865)
 2:                 Peter Pan by J. M. Barrie         (1911)

Which book would you like to R-E-A-D?  (0 to skip) ---> 1


CHAPTER I. Down the Rabbit-Hole

Alice was beginning to get very tired of sitting by her sister on the
bank, and of having nothing to do: once or twice she had peeped into the
book her sister was reading, but it had no pictures or conversations in
it, 'and what is the use of a book,' thought Alice 'without pictures or
conversation?'

So she was considering in her own mind (as well as she could, for the
hot day made her feel very sleepy and stupid), whether the pleasure
of making a daisy-chain would be worth the trouble of getting up and
picking the daisies, when suddenly a White Rabbit with pink eyes ran
close by her.

There was nothing so VERY remarkable in that; nor did Alice think it so
VERY much out of the way to hear the Rabbit say to itself, 'Oh dear!
Oh dear! I shall be late!' (when she thought it over afterwards, it
occurred to her that she ought to have wondered at this, but at the time

(showing...page 1 out of 167, lines 1-20 out of 3334) 

enter (continue); q (quit); p (previous); b (bookmark):  

it all seemed quite natural); but when the Rabbit actually TOOK A WATCH
OUT OF ITS WAISTCOAT-POCKET, and looked at it, and then hurried on,
Alice started to her feet, for it flashed across her mind that she had
never before seen a rabbit with either a waistcoat-pocket, or a watch
to take out of it, and burning with curiosity, she ran across the field
after it, and fortunately was just in time to see it pop down a large
rabbit-hole under the hedge.

In another moment down went Alice after it, never once considering how
in the world she was to get out again.

The rabbit-hole went straight on like a tunnel for some way, and then
dipped suddenly down, so suddenly that Alice had not a moment to think
about stopping herself before she found herself falling down a very deep
well.

Either the well was very deep, or she fell very slowly, for she had
plenty of time as she went down to look about her and to wonder what was
going to happen next. First, she tried to look down and make out what
she was coming to, but it was too dark to see anything; then she

(showing...page 2 out of 167, lines 21-40 out of 3334) 

enter (continue); q (quit); p (previous); b (bookmark): b

enter (continue); q (quit); p (previous); b (bookmark): q

----------------------------------------

Would you like to: 

1) Buy/See available books
2) See owned books
3) Read a book
4) Exit

    ---> 3

  Books You Own: 
---------------  
 1:       Alice in Wonderland by Lewis Carroll        (1865)
 2:                 Peter Pan by J. M. Barrie         (1911)

Which book would you like to R-E-A-D?  (0 to skip) ---> 1

it all seemed quite natural); but when the Rabbit actually TOOK A WATCH
OUT OF ITS WAISTCOAT-POCKET, and looked at it, and then hurried on,
Alice started to her feet, for it flashed across her mind that she had
never before seen a rabbit with either a waistcoat-pocket, or a watch
to take out of it, and burning with curiosity, she ran across the field
after it, and fortunately was just in time to see it pop down a large
rabbit-hole under the hedge.

In another moment down went Alice after it, never once considering how
in the world she was to get out again.

The rabbit-hole went straight on like a tunnel for some way, and then
dipped suddenly down, so suddenly that Alice had not a moment to think
about stopping herself before she found herself falling down a very deep
well.

Either the well was very deep, or she fell very slowly, for she had
plenty of time as she went down to look about her and to wonder what was
going to happen next. First, she tried to look down and make out what
she was coming to, but it was too dark to see anything; then she

(showing...page 2 out of 167, lines 21-40 out of 3334) 

enter (continue); q (quit); p (previous); b (bookmark): q

----------------------------------------

Would you like to: 

1) Buy/See available books
2) See owned books
3) Read a book
4) Exit

    ---> 4

$
Submit

Once you are satisfied with your program, hand it in by typing handin21 in a terminal window.