Lesson 13 – Continue checkOut Method
- Test checkOut, checkIn methods
- Fix compiler error – misplaced {}
- Add test for maximum books
- Create test for getBooksForPerson() method
- Refactoring – extrack method
1 package org.totalbeginner.tutorial;
2
3 import java.util.ArrayList;
4
5 import org.omg.CORBA.PUBLIC_MEMBER;
6 import org.totoalbeginner.tutorial.Person;
7
8 import junit.framework.TestCase;
9
10 public class MyLibraryTest extends TestCase {
11 private Book b1;
12 private Book b2;
13 private Person p1;
14 private Person p2;
15 private MyLibrary ml;
16
17 public void testMyLibrary() {
18 MyLibrary ml = new MyLibrary("Test");
19
20 assertEquals("Test", ml.name);
21
22 assertTrue(ml.books instanceof ArrayList);
23 assertTrue(ml.people instanceof ArrayList);
24
25
26 }
27
28 public void setup(){
29 b1 = new Book("Book1");
30 b2 = new Book("Book2");
31
32 p1 = new Person();
33 p2 = new Person();
34 p1.setName("Fred");
35 p2.setName("Sue");
36
37 ml = new MyLibrary("Test");
38 }
39
40 public void testAddBook(){
41 //create test objects
42 setup();
43
44 //test initial size is 0
45 assertEquals(0,ml.getBooks().size());
46
47 ml.addBook(b1);
48 ml.addBook(b2);
49
50 assertEquals(2, ml.getBooks().size());
51 assertEquals(0, ml.getBooks().indexOf(b1));
52 assertEquals(1,ml.getBooks().indexOf(b2));
53
54 ml.removeBook(b1);
55 assertEquals(1, ml.getBooks().size());
56 assertEquals(0, ml.getBooks().indexOf(b2));
57
58 ml.removeBook(b2);
59 assertEquals(0, ml.getBooks().size());
60 }
61
62 public void testCheckOut() {
63 // set up objects
64 setup();
65
66 addItems();
67
68 assertTrue("Book did not check out correctly",
69 ml.checkOut(b1,p1));
70
71 assertEquals("Fred",b1.getPerson().getName());
72
73 assertFalse("Book was already checked out",
74 ml.checkOut(b1,p2));
75
76 assertTrue("Book check in failed", ml.checkIn(b1));
77
78 assertFalse("Book was already checked in", ml.checkIn(b1));
79
80 assertFalse("Book was never checked out", ml.checkIn(b2));
81
82 // additional test for maximumBooks
83 setup();
84 p1.setMaximumBooks(1);
85 addItems();
86
87 assertTrue("First book did not check out",
88 ml.checkOut(b2, p1));
89 assertFalse("Second book should not have checked out",
90 ml.checkOut(b1, p1));
91 }
92
93 private void addItems() {
94 ml.addBook(b1);
95 ml.addBook(b2);
96 ml.addPerson(p1);
97 ml.addPerson(p2);
98 }
99
100 public void testGetBooksForPerson() {
101 setup();
102 addItems();
103 assertEquals(0, ml.getBooksForPerson(p1).size());
104
105 ml.checkOut(b1, p1);
106
107 ArrayList<Book> testBooks = ml.getBooksForPerson(p1);
108 assertEquals(1, testBooks.size());
109 assertEquals(0, testBooks.indexOf(b1));
110
111 ml.checkOut(b2, p1);
112 testBooks = ml.getBooksForPerson(p1);
113 assertEquals(2, testBooks.size());
114 assertEquals(1, testBooks.indexOf(b2));
115
116 }
117
118
119 }
1 package org.totalbeginner.tutorial;
2
3 import java.util.ArrayList;
4
5 import org.totoalbeginner.tutorial.Person;
6
7 public class MyLibrary {
8
9 String name;
10 public String getName() {
11 return name;
12 }
13
14 public ArrayList<Person> getPeople() {
15 return people;
16 }
17
18 public ArrayList<Book> getBooks() {
19 return books;
20 }
21
22 ArrayList<Person> people;
23 ArrayList<Book> books;
24
25 public MyLibrary(String name) {
26 this.name = name;
27 books = new ArrayList<Book>();
28 people = new ArrayList<Person>();
29
30 }
31
32 public void addBook(Book b1) {
33 this.books.add(b1);
34
35 }
36
37 public void removeBook(Book b1) {
38 this.books.remove(b1);
39
40 }
41
42 public void addPerson(Person p1){
43 this.people.add(p1);
44 }
45
46 public void removePerson(Person p1){
47 this.people.remove(p1);
48 }
49
50 public boolean checkOut(Book b1, Person p1) {
51 if (b1.getPerson() == null) {
52 b1.setPerson(p1);
53 return true;
54 }
55 else {
56 return false;
57 }
58 }
59
60 public boolean checkIn(Book b1) {
61 if (b1.getPerson() != null) {
62 b1.setPerson(null);
63 return true;
64 }
65 else {
66 return false;
67 }
68 }
69
70 public ArrayList<Book> getBooksForPerson(Person p1) {
71 // TODO Auto-generated method stub
72 return null;
73 }
74 }