Reputation: 21
In my
class Library(object):
def __init__(self):
self.books = []
def add_book(self, book):
if book.get_title() not in self.books:
self.books.append(book)
else:
print("This book is already added.")
and then in my main() I got:
if command == "add":
title = input("Please type in the title of the book:")
author = input("Please type in the author of the book:")
year = int(input("Please type in the year of the book:"))
my_lib.add_book( Book(title, author, year) )
print("The book has been added.")
elif command == "view":
my_lib.view_library()
elif command == "get":
book_title = input("Please type in the title of the book:")
book = my_lib.find_book(book_title)
if book != False:
print("Here's the book:")
print("Title:", book.get_title() + ".\nAuthor:",
book.get_author() + ".\nYear:",
str(book.get_year()) + ".\nBorrowed:",
str(book.get_borrowed()) + ".\n")
else:
print("This book does not exist.")
elif command == "borrow":
book_title = input("Type in the title of the book you want to borrow:")
book = my_lib.find_book(book_title)
if book != False and book.get_borrowed() == None:
pass
elif book == False:
print("This book does not exist in our catalog!")
else:
print("This book is already borrowed!")
My problem is when i add several books with the same title, author and year I can only borrow one of those books. Probably a simple solution but I'm stuck. I'd also like to use Another class called User where i can store all the names of the people instead of just doing it in the borrow function with:
##name = input("Please type in your name:")
##book.set_borrowed(name)
Where i now have pass. I got Another class that is just Book. with all the normal getters and setters.
Clarification, this doesnt work:
def add_book(self, book):
if book.get_title() not in self.books:
self.books.append(book)
This doesnt work. I can still add a book with the exact same info including title!
Upvotes: 1
Views: 81
Reputation: 148900
You can just add the number of books as an attribute of Book class, or better of a subclass of Book if you do not need to differenciate copies of same book, or add a unique id (same, in Book class or in a subclass) if you want to diffenrentiate them. In fact, what a human librarian would do in a real library with many copies of same book :-) (never forget the real question ...)
Assuming you only need the count of identical books, but want to know the number of existing book and the part that has been borrowed, an implementation could be :
class LibBook(Book):
def __init__(self, nb, *bookParams):
super(Book, self).__init__(*bookParams)
self.count = nb
def add_copies(nb = 1): # nb can be <0 to remove copies
'add nb new copies of the book (if nb < 0 removes them)'
if (self.present + nb) < 0:
raise Exception('Not enough books')
self.count += nb
self.present += nb
def borrow(n = 1):
if self.present < n:
raise Exception('Not enough books')
self.present -= n
def return(n = 1):
if self.present + n > self.count:
raise Exception('Not so many books were borrowed')
self.present += n
You can then rewrite, provided book is a LibBook :
def add_book(self, book):
if book.get_title() not in self.books:
self.books.append(book)
else
book.add_copies(1)
You should adapt the same the borrow part of your program ...
Upvotes: 0
Reputation: 6359
I think you need to rethink your program.
If you create two books with the same attribute values, a customer can't distinguish between them.
One possibility would be to add a unique ID to every book (using a class variable in the Book class that increases with every book) and use this ID to borrow books. Another possibility would be to store how many of one type of book are available. You could implement this with an int counter in the book class. Increase it if a book with matching attributes is added, decrease if the counter is greater that 0 (= book available) and "get" command is called.
This should work for your problem.
BTW: setters and getters are not pythonic. in python you usually use direct access to the attributes. if you need further processing (value checking etc.), you can use properties.
Upvotes: 2