Skip to content

Commit a2d3890

Browse files
sjwarner-bpFridaTveit
authored andcommitted
book-store: change indent from 2 to 4 spaces (exercism#1029)
* book-store: change indent from 2 to 4 spaces * book-store: Update with @FridaTveit 's improvement
1 parent 49a0e0e commit a2d3890

1 file changed

Lines changed: 30 additions & 30 deletions

File tree

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

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,47 @@
44

55
class BookStore {
66

7-
private static final int BOOK_PRICE = 8, MAX_GROUP_SIZE = 5;
7+
private static final int BOOK_PRICE = 8, MAX_GROUP_SIZE = 5;
88

9-
private static double[] DISCOUNT_TIERS = {0, 5, 10, 20, 25};
9+
private static double[] DISCOUNT_TIERS = {0, 5, 10, 20, 25};
1010

11-
double calculateBasketCost(final List<Integer> books) {
12-
return calculateBasketCost(books, 0);
13-
}
14-
15-
private double calculateBasketCost(final List<Integer> books, final double priceSoFar) {
16-
if (books.size() == 0) {
17-
return priceSoFar;
11+
double calculateBasketCost(final List<Integer> books) {
12+
return calculateBasketCost(books, 0);
1813
}
1914

20-
List<Integer> availableBookNumbers = books.stream()
21-
.distinct()
22-
.collect(Collectors.toList());
15+
private double calculateBasketCost(final List<Integer> books, final double priceSoFar) {
16+
if (books.size() == 0) {
17+
return priceSoFar;
18+
}
2319

24-
double minPrice = Double.MAX_VALUE;
20+
List<Integer> availableBookNumbers = books.stream()
21+
.distinct()
22+
.collect(Collectors.toList());
2523

26-
for (int i = 0; i < availableBookNumbers.size(); i++) {
27-
List<Integer> newGroupBooks = new ArrayList<>(availableBookNumbers.subList(0, i + 1));
28-
List<Integer> remainingBooks = new ArrayList<>(books);
24+
double minPrice = Double.MAX_VALUE;
2925

30-
for (final Integer newGroupBook : newGroupBooks) {
31-
//noinspection UseBulkOperation - we want to remove _one_ of each book number, not _all_ of each book number.
32-
remainingBooks.remove(newGroupBook);
33-
}
26+
for (int i = 0; i < availableBookNumbers.size(); i++) {
27+
List<Integer> newGroupBooks = new ArrayList<>(availableBookNumbers.subList(0, i + 1));
28+
List<Integer> remainingBooks = new ArrayList<>(books);
3429

35-
double price = calculateBasketCost(remainingBooks, priceSoFar + costOfGroupSize(newGroupBooks.size()));
36-
minPrice = Math.min(minPrice, price);
37-
}
30+
for (final Integer newGroupBook : newGroupBooks) {
31+
//noinspection UseBulkOperation - we want to remove _one_ of each book number, not _all_ of each book number.
32+
remainingBooks.remove(newGroupBook);
33+
}
3834

39-
return minPrice;
40-
}
35+
double price = calculateBasketCost(remainingBooks, priceSoFar + costOfGroupSize(newGroupBooks.size()));
36+
minPrice = Math.min(minPrice, price);
37+
}
4138

42-
private double costOfGroupSize(int groupSize) {
43-
if (groupSize < 1 || groupSize > MAX_GROUP_SIZE) {
44-
throw new IllegalStateException("Invalid group size : " + groupSize);
39+
return minPrice;
4540
}
4641

47-
return BOOK_PRICE * groupSize * (100 - DISCOUNT_TIERS[groupSize - 1]) / 100;
48-
}
42+
private double costOfGroupSize(int groupSize) {
43+
if (groupSize < 1 || groupSize > MAX_GROUP_SIZE) {
44+
throw new IllegalStateException("Invalid group size : " + groupSize);
45+
}
46+
47+
return BOOK_PRICE * groupSize * (100 - DISCOUNT_TIERS[groupSize - 1]) / 100;
48+
}
4949

5050
}

0 commit comments

Comments
 (0)