Ver código fonte

Merge pull request #422 from exercism/bookstore-test-fixes

bookstore: fix test suite equality checks
FridaTveit 9 anos atrás
pai
commit
a44a30baac

+ 36
- 40
exercises/book-store/src/example/java/Bookstore.java Ver arquivo

@@ -1,61 +1,57 @@
1
-import java.awt.print.Printable;
2 1
 import java.util.ArrayList;
3
-import java.util.Arrays;
4 2
 import java.util.List;
5 3
 import java.util.stream.Collectors;
6 4
 
7
-public class Bookstore{
5
+class Bookstore {
8 6
 
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;
12 8
 
13
-	public Bookstore (List<Integer> books){
14
-		this.books = books;
15
-	}	
16
-	
9
+  private static double[] DISCOUNT_TIERS = {0, 5, 10, 20, 25};
17 10
 
11
+  private List<Integer> books;
18 12
 
13
+  Bookstore(List<Integer> books) {
14
+    this.books = books;
15
+  }
19 16
 
20
-	public double calculateTotalCost(){
21
-		return calculateTotalCost(this.books,0);
22
-	}
17
+  double calculateTotalCost() {
18
+    return calculateTotalCost(this.books, 0);
19
+  }
23 20
 
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
+    }
26 25
 
26
+    List<Integer> availableBookNumbers = books.stream()
27
+        .distinct()
28
+        .collect(Collectors.toList());
27 29
 
28
-		if(books.size() == 0){
29
-			return priceSoFar;
30
-		}
30
+    double minPrice = Double.MAX_VALUE;
31 31
 
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);
33 35
 
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
+      }
36 40
 
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()));
49 42
 
43
+      minPrice = Math.min(minPrice, price);
44
+    }
50 45
 
51
-	}
46
+    return minPrice;
47
+  }
52 48
 
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
+    }
59 53
 
54
+    return BOOK_PRICE * groupSize * (100 - DISCOUNT_TIERS[groupSize - 1]) / 100;
55
+  }
60 56
 
61 57
 }

+ 108
- 107
exercises/book-store/src/test/java/BookstoreTest.java Ver arquivo

@@ -1,5 +1,6 @@
1 1
 import static org.junit.Assert.assertEquals;
2 2
 
3
+import java.util.Collections;
3 4
 import java.util.List;
4 5
 import java.util.ArrayList;
5 6
 import java.util.Arrays;
@@ -7,112 +8,112 @@ import java.util.Arrays;
7 8
 import org.junit.Ignore;
8 9
 import org.junit.Test;
9 10
 
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
+  }
116 118
 
117 119
 }
118
-