Hier mein erster Code Entwurf:
Code: Alles auswählen
class Author(object):
def __init__(self, name):
self.name = name
class Book(object):
def __init__(self, title, author):
self.title = title
self.author = author
self.versions = []
def displayBook(self):
print "Title: %s - Author: %s" % (self.title, self.author.name )
if len(self.versions) > 0:
print "%d versions known:" % len(self.versions)
for i, v in enumerate(self.versions):
print "%d - Type: %s - Language: %s - Owned: %s" % (i + 1,
v.type, v.language, v.owned)
def add_version(self, type = "Print", lang = "DE", owned = True):
self.versions.append(Version(self, type, lang, owned))
class Version(object):
def __init__(self, book, type = "Print", lang = "DE", owned = True):
self.book = book
self.type = type
self.language = lang
self.owned = owned
class Shelf(object):
shelfCount = 0
def __init__(self):
self.authors = []
self.books = []
def add_book(self, author_name, book_title, type = "Print", lang = "DE"):
# Author known? Else add her.
for a in self.authors:
if a.name == author_name:
break
else:
a = Author(author_name)
self.authors.append(a)
# Book known? Else add it.
for b in self.books:
if b.title == book_title:
break
else:
b = Book(book_title, a)
self.books.append(b)
# Add new Version to the book.
b.add_version(type, lang, True)
def display(self):
print "%d books in this shelf." % (len(self.books))
for b in self.books:
b.displayBook()
def displayCount(self):
print "Total shelves: %d" % Shelf.shelfCount
def test():
Erdgeschoss = Shelf()
Erdgeschoss.add_book("Douglas Hofstede", "Goedel, Escher, Bach")
Erdgeschoss.add_book("Douglas Hofstede", "Goedel, Escher, Bach", "Digital")
Erdgeschoss.display()
if __name__ == "__main__":
test()
1 books in this shelf.
Title: Goedel, Escher, Bach - Author: Douglas Hofstede
2 versions known:
1 - Type: Print - Language: DE - Owned: True
2 - Type: Digital - Language: DE - Owned: True