|
1 | | -class BOOK_COLLECTION |
| 1 | +class |
| 2 | + BOOK_COLLECTION |
2 | 3 |
|
3 | 4 | create |
4 | 5 | make |
5 | 6 |
|
6 | 7 | feature {NONE} -- Initialization |
7 | 8 |
|
8 | 9 | make (a_name: STRING_32) |
| 10 | + -- Create a collection of book with `a_name' as `name' |
9 | 11 | do |
10 | 12 | set_name (a_name) |
11 | 13 | create book_index.make (10) |
| 14 | + ensure |
| 15 | + name_set: name = a_name |
12 | 16 | end |
13 | 17 |
|
14 | 18 | feature -- Access |
15 | 19 |
|
16 | 20 | name: STRING_32 |
| 21 | + -- Name. |
17 | 22 |
|
18 | 23 | books: LIST [BOOK] |
| 24 | + -- collection of book. |
19 | 25 | do |
20 | | - from |
21 | | - create {LINKED_LIST [BOOK]} Result.make |
22 | | - book_index.start |
23 | | - until |
24 | | - book_index.after |
25 | | - loop |
26 | | - Result.append (book_index.item_for_iteration) |
27 | | - book_index.forth |
| 26 | + create {LINKED_LIST [BOOK]} Result.make |
| 27 | + across book_index as it loop |
| 28 | + Result.append (it.item) |
28 | 29 | end |
29 | 30 | end |
30 | 31 |
|
31 | | - books_by_author (an_author: STRING_32): detachable LIST [BOOK] |
| 32 | + books_by_author (a_author: STRING_32): LIST [BOOK] |
| 33 | + -- Books wrote by `a_author' in this collection. |
32 | 34 | do |
33 | | - if book_index.has (an_author) then |
34 | | - Result := book_index @ an_author |
| 35 | + if attached book_index [a_author] as l_result then |
| 36 | + Result := l_result |
35 | 37 | else |
36 | 38 | create {LINKED_LIST [BOOK]} Result.make |
37 | 39 | end |
38 | 40 | end |
39 | 41 |
|
40 | | -feature -- Status setting |
| 42 | +feature -- Change |
41 | 43 |
|
42 | 44 | set_name (a_name: STRING_32) |
| 45 | + -- Set `name' with `a_name'. |
43 | 46 | do |
44 | 47 | name := a_name |
| 48 | + ensure |
| 49 | + name_set: name = a_name |
45 | 50 | end |
46 | 51 |
|
47 | 52 | add_book (a_book: BOOK) |
| 53 | + -- Extend collection with `a_book'. |
48 | 54 | local |
49 | 55 | l: detachable LIST [BOOK] |
50 | 56 | do |
51 | | - if book_index.has (a_book.author.name) then |
52 | | - l := book_index.at ( a_book.author.name ) |
53 | | - else |
| 57 | + l := book_index.at (a_book.author.name ) |
| 58 | + if l = Void then |
54 | 59 | create {LINKED_LIST [BOOK]} l.make |
55 | 60 | book_index.put (l, a_book.author.name) |
56 | 61 | end |
57 | | - if attached l as la then |
58 | | - la.force (a_book) |
59 | | - end |
60 | | - |
| 62 | + l.force (a_book) |
61 | 63 | end |
62 | 64 |
|
63 | 65 | add_books (book_list: like books) |
64 | | - |
| 66 | + -- Append collection with `book_list'. |
65 | 67 | do |
66 | | - from |
67 | | - book_list.start |
68 | | - until |
69 | | - book_list.after |
70 | | - loop |
71 | | - add_book (book_list.item) |
72 | | - book_list.forth |
| 68 | + across book_list as it loop |
| 69 | + add_book (it.item) |
73 | 70 | end |
74 | 71 | end |
75 | 72 |
|
76 | 73 | feature {NONE} -- Implementation |
77 | 74 |
|
78 | 75 | book_index: HASH_TABLE [LIST [BOOK], STRING_32] |
| 76 | + -- Association of author name and its books. |
79 | 77 |
|
80 | 78 | end -- class BOOK_COLLECTION |
0 commit comments