-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
51 lines (41 loc) · 1.57 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Chapter:
def __init__(self, name, pages):
self.title = name
self.pages = pages
class Book:
def __init__(self, title, chapters):
self.title = title
self.chapters = chapters
self.__max = "max"
@property
def chapterCount(self):
return len(self.chapters)
def getPageCount(self):
return sum([a.pages for a in self.chapters])
def getChapter(self, num):
return self.chapters[num]
def moveChapter(self, num, other):
other.chapters.append(self.getChapter(num))
self.chapters = self.chapters[:num] + self.chapters[num + 1:]
chapterA = Chapter('I love CS!', 30) # chapter title, # of pages
chapterB = Chapter('So do I!', 15)
book1 = Book('CS is Fun!', [chapterA, chapterB]) # book title, chapters
book2 = Book('The Short Book', [Chapter('Quick Read!', 5)])
print(book1._Book__max)
assert (book1.chapterCount == 2)
assert (book1.getPageCount() == 45)
assert (book2.chapterCount == 1)
assert (book2.getPageCount() == 5)
assert (book1.getChapter(0).title == 'I love CS!')
assert (book1.getChapter(1).title == 'So do I!')
assert (book2.getChapter(0).title == 'Quick Read!')
# Move chapter 0 from book1 to the end of book2
# so moveChapter always moves to the end of the target book.
book1.moveChapter(0, book2)
assert (book1.chapterCount == 1)
assert (book1.getPageCount() == 15)
assert (book1.getChapter(0).title == 'So do I!')
assert (book2.chapterCount == 2)
assert (book2.getPageCount() == 35)
assert (book2.getChapter(0).title == 'Quick Read!')
assert (book2.getChapter(1).title == 'I love CS!')