Skip to content

Commit 0e0d288

Browse files
committed
bookstore: fix test suite equality checks
1 parent ca92e31 commit 0e0d288

2 files changed

Lines changed: 144 additions & 147 deletions

File tree

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,57 @@
1-
import java.awt.print.Printable;
21
import java.util.ArrayList;
3-
import java.util.Arrays;
42
import java.util.List;
53
import java.util.stream.Collectors;
64

7-
public class Bookstore{
5+
class Bookstore {
86

9-
private static int BOOK_PRICE = 8, MAX_GROUP_SIZE = 5;
10-
private List<Integer> books;
11-
private static double[] DISCOUNT_TIERS = {0,5,10,20,25};
7+
private static final int BOOK_PRICE = 8, MAX_GROUP_SIZE = 5;
128

13-
public Bookstore (List<Integer> books){
14-
this.books = books;
15-
}
16-
9+
private static double[] DISCOUNT_TIERS = {0, 5, 10, 20, 25};
1710

11+
private List<Integer> books;
1812

13+
Bookstore(List<Integer> books) {
14+
this.books = books;
15+
}
1916

20-
public double calculateTotalCost(){
21-
return calculateTotalCost(this.books,0);
22-
}
17+
double calculateTotalCost() {
18+
return calculateTotalCost(this.books, 0);
19+
}
2320

24-
private double calculateTotalCost (List<Integer> books,double priceSoFar ){
25-
double minPrice = Double.MAX_VALUE;
21+
private double calculateTotalCost(List<Integer> books, double priceSoFar) {
22+
if (books.size() == 0) {
23+
return priceSoFar;
24+
}
2625

26+
List<Integer> availableBookNumbers = books.stream()
27+
.distinct()
28+
.collect(Collectors.toList());
2729

28-
if(books.size() == 0){
29-
return priceSoFar;
30-
}
30+
double minPrice = Double.MAX_VALUE;
3131

32-
List<Integer> groups = (ArrayList<Integer>) books.stream().distinct().collect(Collectors.toList());
32+
for (int i = 0; i < availableBookNumbers.size(); i++) {
33+
List<Integer> newGroupBooks = new ArrayList<>(availableBookNumbers.subList(0, i + 1));
34+
List<Integer> remainingBooks = new ArrayList<>(books);
3335

34-
35-
double price = 0;
36+
for (final Integer newGroupBook : newGroupBooks) {
37+
//noinspection UseBulkOperation - we want to remove _one_ of each book number, not _all_ of each book number.
38+
remainingBooks.remove(newGroupBook);
39+
}
3640

37-
for(int i = 0;i<groups.size();i++){
38-
books.remove(groups.get(i));
39-
}
40-
41-
try {
42-
price = calculateTotalCost(books,priceSoFar + costPerGroup(groups.size()));
43-
} catch (Exception e) {
44-
e.printStackTrace();
45-
}
46-
47-
minPrice = Math.min(minPrice, price);
48-
return minPrice;
41+
double price = calculateTotalCost(remainingBooks, priceSoFar + costOfGroupSize(newGroupBooks.size()));
4942

43+
minPrice = Math.min(minPrice, price);
44+
}
5045

51-
}
46+
return minPrice;
47+
}
5248

53-
private double costPerGroup(int groupSize) throws Exception{
54-
if (groupSize < 1 || groupSize > MAX_GROUP_SIZE){
55-
throw new Exception("Invalid group size : " + groupSize );
56-
}
57-
return BOOK_PRICE * groupSize * (100 - DISCOUNT_TIERS[groupSize-1])/100;
58-
}
49+
private double costOfGroupSize(int groupSize) {
50+
if (groupSize < 1 || groupSize > MAX_GROUP_SIZE) {
51+
throw new IllegalStateException("Invalid group size : " + groupSize);
52+
}
5953

54+
return BOOK_PRICE * groupSize * (100 - DISCOUNT_TIERS[groupSize - 1]) / 100;
55+
}
6056

6157
}

‎exercises/book-store/src/test/java/BookstoreTest.java‎

Lines changed: 108 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,119 @@
11
import static org.junit.Assert.assertEquals;
22

3+
import java.util.Collections;
34
import java.util.List;
45
import java.util.ArrayList;
56
import java.util.Arrays;
67

78
import org.junit.Ignore;
89
import org.junit.Test;
910

10-
public class BookstoreTest{
11-
12-
@Test
13-
public void onlyASingleBook(){
14-
List<Integer> books = new ArrayList<>(Arrays.asList(1));
15-
Bookstore bookstore = new Bookstore(books);
16-
assertEquals(8,bookstore.calculateTotalCost(),2);
17-
}
18-
19-
@Ignore
20-
@Test
21-
public void twoOfSameBook(){
22-
List<Integer> books = new ArrayList<>(Arrays.asList(1,1));
23-
Bookstore bookstore = new Bookstore(books);
24-
assertEquals(16,bookstore.calculateTotalCost(),2);
25-
}
26-
27-
@Ignore
28-
@Test
29-
public void emptyBasket(){
30-
List<Integer> books = new ArrayList<>();
31-
Bookstore bookstore = new Bookstore(books);
32-
assertEquals(0,bookstore.calculateTotalCost(),2);
33-
}
34-
35-
@Ignore
36-
@Test
37-
public void twoDifferentBooks(){
38-
List<Integer> books = new ArrayList<>(Arrays.asList(1,2));
39-
Bookstore bookstore = new Bookstore(books);
40-
assertEquals(15.20,bookstore.calculateTotalCost(),2);
41-
}
42-
43-
@Ignore
44-
@Test
45-
public void threeDifferentBooks(){
46-
List<Integer> books = new ArrayList<>(Arrays.asList(1,2,3));
47-
Bookstore bookstore = new Bookstore(books);
48-
assertEquals(21.6,bookstore.calculateTotalCost(),2);
49-
}
50-
51-
@Ignore
52-
@Test
53-
public void fourDifferentBooks(){
54-
ArrayList<Integer> books = new ArrayList<>(Arrays.asList(1,2,3,4));
55-
Bookstore bookstore = new Bookstore(books);
56-
assertEquals(25.6,bookstore.calculateTotalCost(),2);
57-
58-
}
59-
60-
@Ignore
61-
@Test
62-
public void fiveDifferentBooks(){
63-
ArrayList<Integer> books = new ArrayList<>(Arrays.asList(1,2,3,4,5));
64-
Bookstore bookstore = new Bookstore(books);
65-
assertEquals(30,bookstore.calculateTotalCost(),2);
66-
}
67-
68-
@Ignore
69-
@Test
70-
public void twoGroupsOfFourIsCheaperThanGroupOfFivePlusGroupOfThree(){
71-
List<Integer> books = new ArrayList<>(Arrays.asList(1,1,2,2,3,3,4,5));
72-
Bookstore bookstore = new Bookstore(books);
73-
assertEquals(51.20,bookstore.calculateTotalCost(),2);
74-
}
75-
76-
@Ignore
77-
@Test
78-
public void groupOfFourPlusGroupOfTwoIsCheaperThanTwoGroupsOfThree(){
79-
List<Integer> books = new ArrayList<>(Arrays.asList(1,1,2,2,3,4));
80-
Bookstore bookstore = new Bookstore(books);
81-
assertEquals(40.8,bookstore.calculateTotalCost(),2);
82-
}
83-
84-
@Ignore
85-
@Test
86-
public void twoEachOfFirst4BooksAnd1CopyEachOfRest(){
87-
Integer[] p = {1,1,2,2,3,3,4,4,5};
88-
List<Integer> books = new ArrayList<>(Arrays.asList(1,1,2,2,3,3,4,4,5));
89-
Bookstore bookstore = new Bookstore(books);
90-
assertEquals(55.60,bookstore.calculateTotalCost(),2);
91-
}
92-
93-
@Ignore
94-
@Test
95-
public void twoCopiesOfEachBook(){
96-
List<Integer> books = new ArrayList<>(Arrays.asList(1,1,2,2,3,3,4,4,5,5));
97-
Bookstore bookstore = new Bookstore(books);
98-
assertEquals(60.00,bookstore.calculateTotalCost(),2);
99-
}
100-
101-
@Ignore
102-
@Test
103-
public void threeCopiesOfFirstBookAnd2EachOfRemaining(){
104-
List<Integer> books = new ArrayList<>(Arrays.asList(1,1,2,2,3,3,4,4,5,5,1));
105-
Bookstore bookstore = new Bookstore(books);
106-
assertEquals(68.00,bookstore.calculateTotalCost(),2);
107-
}
108-
109-
@Ignore
110-
@Test
111-
public void threeEachOFirst2BooksAnd2EachOfRemainingBooks(){
112-
List<Integer> books = new ArrayList<>(Arrays.asList(1,1,2,2,3,3,4,4,5,5,1,2));
113-
Bookstore bookstore = new Bookstore(books);
114-
assertEquals(75.20,bookstore.calculateTotalCost(),2);
115-
}
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
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
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
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
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
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
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
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
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
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
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
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
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+
}
116118

117119
}
118-

0 commit comments

Comments
 (0)