ソースを参照

book-store: cleanup

- Swap Bookstore for BookStore;
- Pass basket of books to method rather than constructor (simpler mental
  model);
- Remove redundant Arraylist wrappers.
Stuart Kent 9 年 前
コミット
c20bc3bcd4

exercises/book-store/src/example/java/Bookstore.java → exercises/book-store/src/example/java/BookStore.java ファイルの表示

2
 import java.util.List;
2
 import java.util.List;
3
 import java.util.stream.Collectors;
3
 import java.util.stream.Collectors;
4
 
4
 
5
-class Bookstore {
5
+class BookStore {
6
 
6
 
7
   private static final int BOOK_PRICE = 8, MAX_GROUP_SIZE = 5;
7
   private static final int BOOK_PRICE = 8, MAX_GROUP_SIZE = 5;
8
 
8
 
9
   private static double[] DISCOUNT_TIERS = {0, 5, 10, 20, 25};
9
   private static double[] DISCOUNT_TIERS = {0, 5, 10, 20, 25};
10
 
10
 
11
-  private List<Integer> books;
12
-
13
-  Bookstore(List<Integer> books) {
14
-    this.books = books;
15
-  }
16
-
17
-  double calculateTotalCost() {
18
-    return calculateTotalCost(this.books, 0);
11
+  double calculateBasketCost(final List<Integer> books) {
12
+    return calculateBasketCost(books, 0);
19
   }
13
   }
20
 
14
 
21
-  private double calculateTotalCost(List<Integer> books, double priceSoFar) {
15
+  private double calculateBasketCost(final List<Integer> books, final double priceSoFar) {
22
     if (books.size() == 0) {
16
     if (books.size() == 0) {
23
       return priceSoFar;
17
       return priceSoFar;
24
     }
18
     }
38
         remainingBooks.remove(newGroupBook);
32
         remainingBooks.remove(newGroupBook);
39
       }
33
       }
40
 
34
 
41
-      double price = calculateTotalCost(remainingBooks, priceSoFar + costOfGroupSize(newGroupBooks.size()));
42
-
35
+      double price = calculateBasketCost(remainingBooks, priceSoFar + costOfGroupSize(newGroupBooks.size()));
43
       minPrice = Math.min(minPrice, price);
36
       minPrice = Math.min(minPrice, price);
44
     }
37
     }
45
 
38
 

+ 113
- 0
exercises/book-store/src/test/java/BookStoreTest.java ファイルの表示

1
+import static org.junit.Assert.assertEquals;
2
+
3
+import java.util.Collections;
4
+import java.util.List;
5
+import java.util.Arrays;
6
+
7
+import org.junit.Before;
8
+import org.junit.Ignore;
9
+import org.junit.Test;
10
+
11
+public class BookStoreTest {
12
+
13
+  // This is sufficient accuracy since we're handling currency values, which should be equal to within 2 decimal places.
14
+  private static final double EQUALITY_TOLERANCE = 0.001;
15
+
16
+  private BookStore bookStore;
17
+
18
+  @Before
19
+  public void setUp() {
20
+    bookStore = new BookStore();
21
+  }
22
+
23
+  @Test
24
+  public void onlyASingleBook() {
25
+    List<Integer> books = Collections.singletonList(1);
26
+    assertEquals(8, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
27
+  }
28
+
29
+  @Ignore("Remove to run test")
30
+  @Test
31
+  public void twoOfSameBook() {
32
+    List<Integer> books = Arrays.asList(1, 1);
33
+    assertEquals(16, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
34
+  }
35
+
36
+  @Ignore("Remove to run test")
37
+  @Test
38
+  public void emptyBasket() {
39
+    List<Integer> books = Collections.emptyList();
40
+    assertEquals(0, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
41
+  }
42
+
43
+  @Ignore("Remove to run test")
44
+  @Test
45
+  public void twoDifferentBooks() {
46
+    List<Integer> books = Arrays.asList(1, 2);
47
+    assertEquals(15.20, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
48
+  }
49
+
50
+  @Ignore("Remove to run test")
51
+  @Test
52
+  public void threeDifferentBooks() {
53
+    List<Integer> books = Arrays.asList(1, 2, 3);
54
+    assertEquals(21.6, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
55
+  }
56
+
57
+  @Ignore("Remove to run test")
58
+  @Test
59
+  public void fourDifferentBooks() {
60
+    List<Integer> books = Arrays.asList(1, 2, 3, 4);
61
+    assertEquals(25.6, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
62
+  }
63
+
64
+  @Ignore("Remove to run test")
65
+  @Test
66
+  public void fiveDifferentBooks() {
67
+    List<Integer> books = Arrays.asList(1, 2, 3, 4, 5);
68
+    assertEquals(30, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
69
+  }
70
+
71
+  @Ignore("Remove to run test")
72
+  @Test
73
+  public void twoGroupsOfFourIsCheaperThanGroupOfFivePlusGroupOfThree() {
74
+    List<Integer> books = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 5);
75
+    assertEquals(51.20, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
76
+  }
77
+
78
+  @Ignore("Remove to run test")
79
+  @Test
80
+  public void groupOfFourPlusGroupOfTwoIsCheaperThanTwoGroupsOfThree() {
81
+    List<Integer> books = Arrays.asList(1, 1, 2, 2, 3, 4);
82
+    assertEquals(40.8, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
83
+  }
84
+
85
+  @Ignore("Remove to run test")
86
+  @Test
87
+  public void twoEachOfFirst4BooksAnd1CopyEachOfRest() {
88
+    List<Integer> books = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5);
89
+    assertEquals(55.60, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
90
+  }
91
+
92
+  @Ignore("Remove to run test")
93
+  @Test
94
+  public void twoCopiesOfEachBook() {
95
+    List<Integer> books = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5);
96
+    assertEquals(60.00, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
97
+  }
98
+
99
+  @Ignore("Remove to run test")
100
+  @Test
101
+  public void threeCopiesOfFirstBookAnd2EachOfRemaining() {
102
+    List<Integer> books = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1);
103
+    assertEquals(68.00, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
104
+  }
105
+
106
+  @Ignore("Remove to run test")
107
+  @Test
108
+  public void threeEachOFirst2BooksAnd2EachOfRemainingBooks() {
109
+    List<Integer> books = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2);
110
+    assertEquals(75.20, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
111
+  }
112
+
113
+}

+ 0
- 119
exercises/book-store/src/test/java/BookstoreTest.java ファイルの表示

1
-import static org.junit.Assert.assertEquals;
2
-
3
-import java.util.Collections;
4
-import java.util.List;
5
-import java.util.ArrayList;
6
-import java.util.Arrays;
7
-
8
-import org.junit.Ignore;
9
-import org.junit.Test;
10
-
11
-public class BookstoreTest {
12
-
13
-  // This is sufficient accuracy since we're handling currency values, which should be equal to within 2 decimal places.
14
-  private static final double EQUALITY_TOLERANCE = 0.001;
15
-
16
-  @Test
17
-  public void onlyASingleBook() {
18
-    List<Integer> books = new ArrayList<>(Collections.singletonList(1));
19
-    Bookstore bookstore = new Bookstore(books);
20
-    assertEquals(8, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
21
-  }
22
-
23
-  @Ignore("Remove to run test")
24
-  @Test
25
-  public void twoOfSameBook() {
26
-    List<Integer> books = new ArrayList<>(Arrays.asList(1, 1));
27
-    Bookstore bookstore = new Bookstore(books);
28
-    assertEquals(16, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
29
-  }
30
-
31
-  @Ignore("Remove to run test")
32
-  @Test
33
-  public void emptyBasket() {
34
-    List<Integer> books = new ArrayList<>();
35
-    Bookstore bookstore = new Bookstore(books);
36
-    assertEquals(0, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
37
-  }
38
-
39
-  @Ignore("Remove to run test")
40
-  @Test
41
-  public void twoDifferentBooks() {
42
-    List<Integer> books = new ArrayList<>(Arrays.asList(1, 2));
43
-    Bookstore bookstore = new Bookstore(books);
44
-    assertEquals(15.20, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
45
-  }
46
-
47
-  @Ignore("Remove to run test")
48
-  @Test
49
-  public void threeDifferentBooks() {
50
-    List<Integer> books = new ArrayList<>(Arrays.asList(1, 2, 3));
51
-    Bookstore bookstore = new Bookstore(books);
52
-    assertEquals(21.6, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
53
-  }
54
-
55
-  @Ignore("Remove to run test")
56
-  @Test
57
-  public void fourDifferentBooks() {
58
-    List<Integer> books = new ArrayList<>(Arrays.asList(1, 2, 3, 4));
59
-    Bookstore bookstore = new Bookstore(books);
60
-    assertEquals(25.6, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
61
-  }
62
-
63
-  @Ignore("Remove to run test")
64
-  @Test
65
-  public void fiveDifferentBooks() {
66
-    List<Integer> books = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
67
-    Bookstore bookstore = new Bookstore(books);
68
-    assertEquals(30, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
69
-  }
70
-
71
-  @Ignore("Remove to run test")
72
-  @Test
73
-  public void twoGroupsOfFourIsCheaperThanGroupOfFivePlusGroupOfThree() {
74
-    List<Integer> books = new ArrayList<>(Arrays.asList(1, 1, 2, 2, 3, 3, 4, 5));
75
-    Bookstore bookstore = new Bookstore(books);
76
-    assertEquals(51.20, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
77
-  }
78
-
79
-  @Ignore("Remove to run test")
80
-  @Test
81
-  public void groupOfFourPlusGroupOfTwoIsCheaperThanTwoGroupsOfThree() {
82
-    List<Integer> books = new ArrayList<>(Arrays.asList(1, 1, 2, 2, 3, 4));
83
-    Bookstore bookstore = new Bookstore(books);
84
-    assertEquals(40.8, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
85
-  }
86
-
87
-  @Ignore("Remove to run test")
88
-  @Test
89
-  public void twoEachOfFirst4BooksAnd1CopyEachOfRest() {
90
-    List<Integer> books = new ArrayList<>(Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5));
91
-    Bookstore bookstore = new Bookstore(books);
92
-    assertEquals(55.60, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
93
-  }
94
-
95
-  @Ignore("Remove to run test")
96
-  @Test
97
-  public void twoCopiesOfEachBook() {
98
-    List<Integer> books = new ArrayList<>(Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5));
99
-    Bookstore bookstore = new Bookstore(books);
100
-    assertEquals(60.00, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
101
-  }
102
-
103
-  @Ignore("Remove to run test")
104
-  @Test
105
-  public void threeCopiesOfFirstBookAnd2EachOfRemaining() {
106
-    List<Integer> books = new ArrayList<>(Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1));
107
-    Bookstore bookstore = new Bookstore(books);
108
-    assertEquals(68.00, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
109
-  }
110
-
111
-  @Ignore("Remove to run test")
112
-  @Test
113
-  public void threeEachOFirst2BooksAnd2EachOfRemainingBooks() {
114
-    List<Integer> books = new ArrayList<>(Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2));
115
-    Bookstore bookstore = new Bookstore(books);
116
-    assertEquals(75.20, bookstore.calculateTotalCost(), EQUALITY_TOLERANCE);
117
-  }
118
-
119
-}