Skip to content

Commit 4e7f6bf

Browse files
lukbukkitsjwarner-bp
authored andcommitted
Updates the book-store exercise to 1.4 (exercism#1562)
* Updates the book-store exercise to 1.4 (See exercism#1531) * Adds @ignore again to the tests
1 parent 1df9a87 commit 4e7f6bf

3 files changed

Lines changed: 37 additions & 4 deletions

File tree

‎exercises/book-store/.meta/src/reference/java/BookStore.java‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import java.util.ArrayList;
2+
import java.util.HashMap;
23
import java.util.List;
34
import java.util.stream.Collectors;
45

@@ -9,7 +10,32 @@ class BookStore {
910
private static double[] DISCOUNT_TIERS = {0, 5, 10, 20, 25};
1011

1112
double calculateBasketCost(final List<Integer> books) {
12-
return calculateBasketCost(books, 0);
13+
List<Integer> reorderedBooks = reorderBooks(books);
14+
return calculateBasketCost(reorderedBooks, 0);
15+
}
16+
17+
private List<Integer> reorderBooks(final List<Integer> books) {
18+
// Counting how often a book number appears in the basket list
19+
HashMap<Integer, Integer> numberCount = new HashMap<>();
20+
for (Integer book : books) {
21+
numberCount.computeIfPresent(book, (key, value) -> value + 1);
22+
numberCount.putIfAbsent(book, 1);
23+
}
24+
25+
return books.stream()
26+
.sorted((bookNumberOne, bookNumberTwo) -> {
27+
Integer countOne = numberCount.get(bookNumberOne);
28+
Integer countTwo = numberCount.get(bookNumberTwo);
29+
// Books whose numbers appear more often should be in front of the basket list
30+
if (countOne > countTwo) {
31+
return -1;
32+
} else if (countOne.equals(countTwo)) {
33+
return 0;
34+
} else {
35+
return 1;
36+
}
37+
})
38+
.collect(Collectors.toList());
1339
}
1440

1541
private double calculateBasketCost(final List<Integer> books, final double priceSoFar) {
@@ -18,8 +44,8 @@ private double calculateBasketCost(final List<Integer> books, final double price
1844
}
1945

2046
List<Integer> availableBookNumbers = books.stream()
21-
.distinct()
22-
.collect(Collectors.toList());
47+
.distinct()
48+
.collect(Collectors.toList());
2349

2450
double minPrice = Double.MAX_VALUE;
2551

‎exercises/book-store/.meta/version‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.3.0
1+
1.4.0

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ public void twoGroupsOfFourIsCheaperThanGroupOfFivePlusGroupOfThree() {
7575
assertEquals(51.20, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
7676
}
7777

78+
@Ignore("Remove to run test")
79+
@Test
80+
public void twoGroupsOfFourIsCheaperThanGroupsOfFiveAndThree() {
81+
List<Integer> books = Arrays.asList(1, 1, 2, 3, 4, 4, 5, 5);
82+
assertEquals(51.20, bookStore.calculateBasketCost(books), EQUALITY_TOLERANCE);
83+
}
84+
7885
@Ignore("Remove to run test")
7986
@Test
8087
public void groupOfFourPlusGroupOfTwoIsCheaperThanTwoGroupsOfThree() {

0 commit comments

Comments
 (0)