CS 0002-Python-Assignment 1
For this assignment you will be writing a single program that will contain three Python object-oriented
classes and methods associated with each class. The classes will model a library with its books, patrons
(library users), and check-outs (books checked out to patrons). Name your program file in the manner you
have done in previous assignments and submit it in Brightspace as usual.
If you work with another person on this program, please be sure to credit them in the comments in the
header of the program. Remember to comment your programs well and to put a header at the top of the
program with your name, the course number and section, the date, and the assignment number.
You do not have to use IPO notation for the methods you write for this assignment but put at least a one-
line header comment above each method you write. However, you should use IPO notation for any
functions you write outside the class definitions.
Problem: Define Classes Describing Books, Patrons, and Checkouts
For this problem you will be defining three classes, Book, Patron, and Checkout. For each, you will need
to define the __init__ and __str__ methods to respectively initialize objects in the classes and to print
them.
The Book class has four attributes, three of which are passed to it at instance initialization. The first three
are the author (a string), the title of the book (a string), and the number of copies that the library has (a
positive integer from 1 to 4). The fourth is the number of copies checked out, which should be set to zero
at initialization.
The Patron class has two attributes, both of which are passed to it at instance initialization. These are the
patron’s library card number (an integer) and their name (string).
The Checkout class has two attributes, both of which are passed to it at instance initialization. These are
the patron (a Patron object) and the book (which is a Book object). When a checkout object is created, the
number of copies checked out of its Book object attribute should be increased by one.
You should write a function called borrow that takes as parameters a Patron object, a Book object, and a
list of all the current Checkout objects. If a patron tries to borrow a book for which all copies are already
loaned, it should refuse to check it out. If a copy is available, then it should create a Checkout object and
add it to the list of Checkout objects. It should return the updated list of Checkout objects.
You should also write a function called library_return which takes the same parameters as borrow. It
should search the list of the current Checkout objects for a Checkout that matches the book and patron. If
it finds it, it should remove it from the list and subtract one from the number of copies checked out for
that book.
Finally, you should write a function called print_checkouts which prints out all the books currently
checked out. It takes one parameter, the list of the current Checkout objects.
After defining these classes, you should be able to run the following code in your main program:
HuckFinn=Book("Mark Twain","The Adventures of Huckleberry Finn",2)
MaryMiller=Patron(1,"Mary Miller")
DavidChang=Patron(2,"David Chang")
SusanMikulski=Patron(3,"Susan Mikulski")
print(HuckFinn)
print(SusanMikulski)
borrowed=[]
borrowed=borrow(SusanMikulski,HuckFinn,borrowed)
borrowed=borrow(DavidChang,HuckFinn,borrowed)
borrowed=borrow(MaryMiller,HuckFinn,borrowed)
print_checkouts(borrowed)
borrowed=library_return(DavidChang,HuckFinn,borrowed)
borrowed=library_return(DavidChang,HuckFinn,borrowed)
print(HuckFinn)
and get the following output:
Author: Mark Twain; Title: The Adventures of Huckleberry Finn; Copies: 2; Copies Out: 0
Patron Name: Susan Mikulski; Library Card Number: 3
All copies out.
The Adventures of Huckleberry Finn checked out by Susan Mikulski
The Adventures of Huckleberry Finn checked out by David Chang
Title The Adventures of Huckleberry Finn not borrowed by David Chang
Author: Mark Twain; Title: The Adventures of Huckleberry Finn; Copies: 2; Copies Out: 1
Grading: 25 points correct logic, 10 points correct output, 5 points comments.
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。