From 9b71eb0d5b2d4eca28c37c1591e71b50a18e822a Mon Sep 17 00:00:00 2001 From: Ladent Thibault Date: Fri, 17 Feb 2017 14:01:50 +0100 Subject: [PATCH 01/17] dibs : I will implement exercise bookstore From de35a3f30df26dd156cc640eb43a89f396220ed3 Mon Sep 17 00:00:00 2001 From: Ladent Thibault Date: Fri, 3 Mar 2017 15:50:19 +0100 Subject: [PATCH 02/17] bookstore : add to track --- exercises/book-store/build.gradle | 17 +++ exercises/book-store/src/example/java/.keep | 0 .../src/example/java/BookStore.java | 62 +++++++++++ exercises/book-store/src/main/java/.keep | 0 exercises/book-store/src/test/java/.keep | 0 .../src/test/java/BookStoreTest.java | 105 ++++++++++++++++++ exercises/settings.gradle | 1 + 7 files changed, 185 insertions(+) create mode 100644 exercises/book-store/build.gradle create mode 100644 exercises/book-store/src/example/java/.keep create mode 100644 exercises/book-store/src/example/java/BookStore.java create mode 100644 exercises/book-store/src/main/java/.keep create mode 100644 exercises/book-store/src/test/java/.keep create mode 100644 exercises/book-store/src/test/java/BookStoreTest.java diff --git a/exercises/book-store/build.gradle b/exercises/book-store/build.gradle new file mode 100644 index 000000000..d019b23c7 --- /dev/null +++ b/exercises/book-store/build.gradle @@ -0,0 +1,17 @@ +apply plugin: "java" +apply plugin: "eclipse" +apply plugin: "idea" + +repositories { + mavenCentral() +} + +dependencies { + testCompile "junit:junit:4.12" +} +test { + testLogging { + exceptionFormat = 'full' + events = ["passed", "failed", "skipped"] + } +} diff --git a/exercises/book-store/src/example/java/.keep b/exercises/book-store/src/example/java/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/exercises/book-store/src/example/java/BookStore.java b/exercises/book-store/src/example/java/BookStore.java new file mode 100644 index 000000000..d0f5860c5 --- /dev/null +++ b/exercises/book-store/src/example/java/BookStore.java @@ -0,0 +1,62 @@ +import java.util.ArrayList; +import java.util.stream.Collectors; + +public class BookStore{ + public static double CalculateTotalCost(ArrayList books ){ + return CalculateTotalCost(books,0); + } + + public static double CalculateTotalCost (ArrayList books,double priceSoFar ){ + double minPrice = Double.MAX_VALUE; + + if(books.size() == 0) + return priceSoFar; + + ArrayList groups = (ArrayList) books.stream().distinct().collect(Collectors.toList()); + + + double price = 0; + + for(int i = 0;i books = new ArrayList<>(Arrays.asList(p)); + assertEquals(8,BookStore.CalculateTotalCost(books),2); + } + + + @Test + public void Basket_with_two_of_same_book(){ + Integer[] p = {1,1}; + ArrayList books = new ArrayList<>(Arrays.asList(p)); + assertEquals(16,BookStore.CalculateTotalCost(books),2); + } + + + @Test + public void Empty_basket(){ + ArrayList books = new ArrayList<>(); + assertEquals(0,BookStore.CalculateTotalCost(books),2); + } + + + @Test + public void Basket_with_two_different_books(){ + Integer[] p = {1,2}; + ArrayList books = new ArrayList<>(Arrays.asList(p)); + + assertEquals(15.20,BookStore.CalculateTotalCost(books),2); + } + + + @Test + public void Basket_with_three_different_books(){ + Integer[] p = {1,2,3}; + ArrayList books = new ArrayList<>(Arrays.asList(p)); + assertEquals(21.6,BookStore.CalculateTotalCost(books),2); + } + + + @Test + public void Basket_with_four_different_books(){ + Integer[] p = {1,2,3,4}; + ArrayList books = new ArrayList<>(Arrays.asList(p)); + assertEquals(25.6,BookStore.CalculateTotalCost(books),2); + + } + + + @Test + public void Basket_with_five_different_books(){ + Integer[] p = {1,2,3,4,5}; + ArrayList books = new ArrayList<>(Arrays.asList(p)); + assertEquals(30,BookStore.CalculateTotalCost(books),2); + } + + + @Test + public void Basket_with_eight_books(){ + Integer[] p = {1,1,2,2,3,3,4,5}; + ArrayList books = new ArrayList<>(Arrays.asList(p)); + assertEquals(51.20,BookStore.CalculateTotalCost(books),2); + } + + + + @Test + public void Basket_with_nine_books(){ + Integer[] p = {1,1,2,2,3,3,4,4,5}; + ArrayList books = new ArrayList<>(Arrays.asList(p)); + assertEquals(55.60,BookStore.CalculateTotalCost(books),2); + } + + @Test + public void Basket_with_ten_books(){ + Integer[] p = {1,1,2,2,3,3,4,4,5,5}; + ArrayList books = new ArrayList<>(Arrays.asList(p)); + assertEquals(60,BookStore.CalculateTotalCost(books),2); + } + + + @Test + public void Basket_with_eleven_books(){ + Integer[] p = {1,1,2,2,3,3,4,4,5,5,1}; + ArrayList books = new ArrayList<>(Arrays.asList(p)); + assertEquals(68,BookStore.CalculateTotalCost(books),2); + } + + + @Test + public void Basket_with_twelve_books(){ + Integer[] p = {1,1,2,2,3,3,4,4,5,5,1,2}; + ArrayList books = new ArrayList<>(Arrays.asList(p)); + assertEquals(75.20,BookStore.CalculateTotalCost(books),2); + } +} + diff --git a/exercises/settings.gradle b/exercises/settings.gradle index 76f89c7e3..6a42a76f6 100644 --- a/exercises/settings.gradle +++ b/exercises/settings.gradle @@ -9,6 +9,7 @@ include 'binary' include 'binary-search' include 'binary-search-tree' include 'bob' +include 'book-store' include 'bracket-push' include 'clock' include 'crypto-square' From c61580aed4b99be7cabee1ba86062993502032cb Mon Sep 17 00:00:00 2001 From: Thibault LADENT Date: Fri, 3 Mar 2017 16:19:22 +0100 Subject: [PATCH 03/17] bookstore : add to track update config.json --- config.json | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/config.json b/config.json index 0c975057a..7b4fe2bde 100644 --- a/config.json +++ b/config.json @@ -62,7 +62,8 @@ "diamond", "secret-handshake", "flatten-array", - "perfect-numbers" + "perfect-numbers", + "book-store" ], "exercises": [ { @@ -359,6 +360,11 @@ "slug": "perfect-numbers", "difficulty": 1, "topics": [] + }, + { + "slug": "book-store", + "difficulty": 1, + "topics": [] } ], "deprecated": [ From 7a9a4b4e6ba61528a94147b0be76f581a9f360fb Mon Sep 17 00:00:00 2001 From: Thibault LADENT Date: Fri, 10 Mar 2017 14:22:45 +0100 Subject: [PATCH 04/17] Update config.json : added orc-numbers --- config.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/config.json b/config.json index 027d6247b..d8bf703a7 100644 --- a/config.json +++ b/config.json @@ -309,6 +309,11 @@ "difficulty": 1, "topics": [] }, + { + "slug": "ocr-numbers", + "difficulty": 1, + "topics": [] + }, { "slug": "book-store", "difficulty": 1, From c8dd1ffea182e4c2b3edf263b4661047dad2d877 Mon Sep 17 00:00:00 2001 From: Ladent Thibault Date: Fri, 10 Mar 2017 16:39:41 +0100 Subject: [PATCH 05/17] add missing test from the canonical spec and delete the remaning test --- .../src/test/java/BookStoreTest.java | 51 +++++++++++++------ 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/exercises/book-store/src/test/java/BookStoreTest.java b/exercises/book-store/src/test/java/BookStoreTest.java index 7283399fe..fc1b28a44 100644 --- a/exercises/book-store/src/test/java/BookStoreTest.java +++ b/exercises/book-store/src/test/java/BookStoreTest.java @@ -7,7 +7,7 @@ public class BookStoreTest{ @Test - public void Basket_with_single_book(){ + public void Only_a_single_book(){ Integer[] p = {1}; ArrayList books = new ArrayList<>(Arrays.asList(p)); assertEquals(8,BookStore.CalculateTotalCost(books),2); @@ -15,7 +15,7 @@ public void Basket_with_single_book(){ @Test - public void Basket_with_two_of_same_book(){ + public void Two_of_same_book(){ Integer[] p = {1,1}; ArrayList books = new ArrayList<>(Arrays.asList(p)); assertEquals(16,BookStore.CalculateTotalCost(books),2); @@ -28,18 +28,19 @@ public void Empty_basket(){ assertEquals(0,BookStore.CalculateTotalCost(books),2); } - @Test - public void Basket_with_two_different_books(){ + public void Two_different_books(){ Integer[] p = {1,2}; ArrayList books = new ArrayList<>(Arrays.asList(p)); - assertEquals(15.20,BookStore.CalculateTotalCost(books),2); } + + + @Test - public void Basket_with_three_different_books(){ + public void Three_different_books(){ Integer[] p = {1,2,3}; ArrayList books = new ArrayList<>(Arrays.asList(p)); assertEquals(21.6,BookStore.CalculateTotalCost(books),2); @@ -47,7 +48,7 @@ public void Basket_with_three_different_books(){ @Test - public void Basket_with_four_different_books(){ + public void Four_different_books(){ Integer[] p = {1,2,3,4}; ArrayList books = new ArrayList<>(Arrays.asList(p)); assertEquals(25.6,BookStore.CalculateTotalCost(books),2); @@ -56,50 +57,70 @@ public void Basket_with_four_different_books(){ @Test - public void Basket_with_five_different_books(){ + public void Five_different_books(){ Integer[] p = {1,2,3,4,5}; ArrayList books = new ArrayList<>(Arrays.asList(p)); assertEquals(30,BookStore.CalculateTotalCost(books),2); } + + @Test - public void Basket_with_eight_books(){ + public void Two_groups_of_four_is_cheaper_than_group_of_five_plus_group_of_three(){ Integer[] p = {1,1,2,2,3,3,4,5}; ArrayList books = new ArrayList<>(Arrays.asList(p)); assertEquals(51.20,BookStore.CalculateTotalCost(books),2); } + @Test + public void Group_of_four_plus_group_of_two_is_cheaper_than_two_groups_of_three(){ + Integer[] p = {1,1,2,2,3,4}; + ArrayList books = new ArrayList<>(Arrays.asList(p)); + assertEquals(40.8,BookStore.CalculateTotalCost(books),2); + } + @Test - public void Basket_with_nine_books(){ + public void Two_each_of_first_4_books_and_1_copy_each_of_rest(){ Integer[] p = {1,1,2,2,3,3,4,4,5}; ArrayList books = new ArrayList<>(Arrays.asList(p)); assertEquals(55.60,BookStore.CalculateTotalCost(books),2); } + + + @Test - public void Basket_with_ten_books(){ + public void Two_copies_of_each_book(){ Integer[] p = {1,1,2,2,3,3,4,4,5,5}; ArrayList books = new ArrayList<>(Arrays.asList(p)); - assertEquals(60,BookStore.CalculateTotalCost(books),2); + assertEquals(60.00,BookStore.CalculateTotalCost(books),2); } @Test - public void Basket_with_eleven_books(){ + public void Three_copies_of_first_book_and_2_each_of_remaining(){ Integer[] p = {1,1,2,2,3,3,4,4,5,5,1}; ArrayList books = new ArrayList<>(Arrays.asList(p)); - assertEquals(68,BookStore.CalculateTotalCost(books),2); + assertEquals(68.00,BookStore.CalculateTotalCost(books),2); } @Test - public void Basket_with_twelve_books(){ + public void Three_each_of_first_2_books_and_2_each_of_remaining_books(){ Integer[] p = {1,1,2,2,3,3,4,4,5,5,1,2}; ArrayList books = new ArrayList<>(Arrays.asList(p)); assertEquals(75.20,BookStore.CalculateTotalCost(books),2); } + + + + + + + + } From 812e86ebc405563ff6776df71f9a94ef11972114 Mon Sep 17 00:00:00 2001 From: Ladent Thibault Date: Thu, 16 Mar 2017 11:33:54 +0100 Subject: [PATCH 06/17] using camelCase instead of snake_case for tests methods --- .../src/test/java/BookStoreTest.java | 50 +++++-------------- 1 file changed, 13 insertions(+), 37 deletions(-) diff --git a/exercises/book-store/src/test/java/BookStoreTest.java b/exercises/book-store/src/test/java/BookStoreTest.java index fc1b28a44..6ea7fdf6b 100644 --- a/exercises/book-store/src/test/java/BookStoreTest.java +++ b/exercises/book-store/src/test/java/BookStoreTest.java @@ -7,120 +7,96 @@ public class BookStoreTest{ @Test - public void Only_a_single_book(){ + public void OnlyASingleBook(){ Integer[] p = {1}; ArrayList books = new ArrayList<>(Arrays.asList(p)); assertEquals(8,BookStore.CalculateTotalCost(books),2); } - @Test - public void Two_of_same_book(){ + public void TwoOfSameBook(){ Integer[] p = {1,1}; ArrayList books = new ArrayList<>(Arrays.asList(p)); assertEquals(16,BookStore.CalculateTotalCost(books),2); } - @Test - public void Empty_basket(){ + public void EmptyBasket(){ ArrayList books = new ArrayList<>(); assertEquals(0,BookStore.CalculateTotalCost(books),2); } @Test - public void Two_different_books(){ + public void TwoDifferentBooks(){ Integer[] p = {1,2}; ArrayList books = new ArrayList<>(Arrays.asList(p)); assertEquals(15.20,BookStore.CalculateTotalCost(books),2); } - - - - @Test - public void Three_different_books(){ + public void ThreeDifferentBooks(){ Integer[] p = {1,2,3}; ArrayList books = new ArrayList<>(Arrays.asList(p)); assertEquals(21.6,BookStore.CalculateTotalCost(books),2); } - @Test - public void Four_different_books(){ + public void FourDifferentBooks(){ Integer[] p = {1,2,3,4}; ArrayList books = new ArrayList<>(Arrays.asList(p)); assertEquals(25.6,BookStore.CalculateTotalCost(books),2); } - @Test - public void Five_different_books(){ + public void FiveDifferentBooks(){ Integer[] p = {1,2,3,4,5}; ArrayList books = new ArrayList<>(Arrays.asList(p)); assertEquals(30,BookStore.CalculateTotalCost(books),2); } - - - @Test - public void Two_groups_of_four_is_cheaper_than_group_of_five_plus_group_of_three(){ + public void TwoGroupsOfFourIsCheaperThanGroupOfFivePlusGroupOfThree(){ Integer[] p = {1,1,2,2,3,3,4,5}; ArrayList books = new ArrayList<>(Arrays.asList(p)); assertEquals(51.20,BookStore.CalculateTotalCost(books),2); } @Test - public void Group_of_four_plus_group_of_two_is_cheaper_than_two_groups_of_three(){ + public void GroupOfFourPlusGroupOfTwoIsCheaperThanTwoGroupsOfThree(){ Integer[] p = {1,1,2,2,3,4}; ArrayList books = new ArrayList<>(Arrays.asList(p)); assertEquals(40.8,BookStore.CalculateTotalCost(books),2); } - @Test - public void Two_each_of_first_4_books_and_1_copy_each_of_rest(){ + public void TwoEachOfFirst4BooksAnd1CopyEachOfRest(){ Integer[] p = {1,1,2,2,3,3,4,4,5}; ArrayList books = new ArrayList<>(Arrays.asList(p)); assertEquals(55.60,BookStore.CalculateTotalCost(books),2); } - - - @Test - public void Two_copies_of_each_book(){ + public void TwoCopiesOfEachBook(){ Integer[] p = {1,1,2,2,3,3,4,4,5,5}; ArrayList books = new ArrayList<>(Arrays.asList(p)); assertEquals(60.00,BookStore.CalculateTotalCost(books),2); } - @Test - public void Three_copies_of_first_book_and_2_each_of_remaining(){ + public void ThreeCopiesOfFirstBookAnd2EachOfRemaining(){ Integer[] p = {1,1,2,2,3,3,4,4,5,5,1}; ArrayList books = new ArrayList<>(Arrays.asList(p)); assertEquals(68.00,BookStore.CalculateTotalCost(books),2); } - @Test - public void Three_each_of_first_2_books_and_2_each_of_remaining_books(){ + public void ThreeEachOFirst2BooksAnd2EachOfRemainingBooks(){ Integer[] p = {1,1,2,2,3,3,4,4,5,5,1,2}; ArrayList books = new ArrayList<>(Arrays.asList(p)); assertEquals(75.20,BookStore.CalculateTotalCost(books),2); } - - - - - - - } From 92527865a98789848679d06cb07930bd54af0dda Mon Sep 17 00:00:00 2001 From: Ladent Thibault Date: Thu, 16 Mar 2017 11:36:21 +0100 Subject: [PATCH 07/17] adding @Ignore annotation --- .../book-store/src/test/java/BookStoreTest.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/exercises/book-store/src/test/java/BookStoreTest.java b/exercises/book-store/src/test/java/BookStoreTest.java index 6ea7fdf6b..81d04cc30 100644 --- a/exercises/book-store/src/test/java/BookStoreTest.java +++ b/exercises/book-store/src/test/java/BookStoreTest.java @@ -12,7 +12,8 @@ public void OnlyASingleBook(){ ArrayList books = new ArrayList<>(Arrays.asList(p)); assertEquals(8,BookStore.CalculateTotalCost(books),2); } - + + @Ignore @Test public void TwoOfSameBook(){ Integer[] p = {1,1}; @@ -20,12 +21,14 @@ public void TwoOfSameBook(){ assertEquals(16,BookStore.CalculateTotalCost(books),2); } + @Ignore @Test public void EmptyBasket(){ ArrayList books = new ArrayList<>(); assertEquals(0,BookStore.CalculateTotalCost(books),2); } + @Ignore @Test public void TwoDifferentBooks(){ Integer[] p = {1,2}; @@ -33,6 +36,7 @@ public void TwoDifferentBooks(){ assertEquals(15.20,BookStore.CalculateTotalCost(books),2); } + @Ignore @Test public void ThreeDifferentBooks(){ Integer[] p = {1,2,3}; @@ -40,6 +44,7 @@ public void ThreeDifferentBooks(){ assertEquals(21.6,BookStore.CalculateTotalCost(books),2); } + @Ignore @Test public void FourDifferentBooks(){ Integer[] p = {1,2,3,4}; @@ -48,6 +53,7 @@ public void FourDifferentBooks(){ } + @Ignore @Test public void FiveDifferentBooks(){ Integer[] p = {1,2,3,4,5}; @@ -55,6 +61,7 @@ public void FiveDifferentBooks(){ assertEquals(30,BookStore.CalculateTotalCost(books),2); } + @Ignore @Test public void TwoGroupsOfFourIsCheaperThanGroupOfFivePlusGroupOfThree(){ Integer[] p = {1,1,2,2,3,3,4,5}; @@ -62,6 +69,7 @@ public void TwoGroupsOfFourIsCheaperThanGroupOfFivePlusGroupOfThree(){ assertEquals(51.20,BookStore.CalculateTotalCost(books),2); } + @Ignore @Test public void GroupOfFourPlusGroupOfTwoIsCheaperThanTwoGroupsOfThree(){ Integer[] p = {1,1,2,2,3,4}; @@ -69,7 +77,7 @@ public void GroupOfFourPlusGroupOfTwoIsCheaperThanTwoGroupsOfThree(){ assertEquals(40.8,BookStore.CalculateTotalCost(books),2); } - + @Ignore @Test public void TwoEachOfFirst4BooksAnd1CopyEachOfRest(){ Integer[] p = {1,1,2,2,3,3,4,4,5}; @@ -77,6 +85,7 @@ public void TwoEachOfFirst4BooksAnd1CopyEachOfRest(){ assertEquals(55.60,BookStore.CalculateTotalCost(books),2); } + @Ignore @Test public void TwoCopiesOfEachBook(){ Integer[] p = {1,1,2,2,3,3,4,4,5,5}; @@ -84,6 +93,7 @@ public void TwoCopiesOfEachBook(){ assertEquals(60.00,BookStore.CalculateTotalCost(books),2); } + @Ignore @Test public void ThreeCopiesOfFirstBookAnd2EachOfRemaining(){ Integer[] p = {1,1,2,2,3,3,4,4,5,5,1}; @@ -91,6 +101,7 @@ public void ThreeCopiesOfFirstBookAnd2EachOfRemaining(){ assertEquals(68.00,BookStore.CalculateTotalCost(books),2); } + @Ignore @Test public void ThreeEachOFirst2BooksAnd2EachOfRemainingBooks(){ Integer[] p = {1,1,2,2,3,3,4,4,5,5,1,2}; From 84268420f90eb98d8144230708c55e2cae4fc1f3 Mon Sep 17 00:00:00 2001 From: Ladent Thibault Date: Thu, 16 Mar 2017 11:48:35 +0100 Subject: [PATCH 08/17] using List instead of ArrayList --- .../src/example/java/BookStore.java | 15 ++++++++---- .../src/test/java/BookStoreTest.java | 24 ++++++++++--------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/exercises/book-store/src/example/java/BookStore.java b/exercises/book-store/src/example/java/BookStore.java index d0f5860c5..63420d28b 100644 --- a/exercises/book-store/src/example/java/BookStore.java +++ b/exercises/book-store/src/example/java/BookStore.java @@ -1,18 +1,19 @@ import java.util.ArrayList; +import java.util.List; import java.util.stream.Collectors; public class BookStore{ - public static double CalculateTotalCost(ArrayList books ){ + public static double CalculateTotalCost(List books ){ return CalculateTotalCost(books,0); } - public static double CalculateTotalCost (ArrayList books,double priceSoFar ){ + public static double CalculateTotalCost (List books,double priceSoFar ){ double minPrice = Double.MAX_VALUE; if(books.size() == 0) return priceSoFar; - ArrayList groups = (ArrayList) books.stream().distinct().collect(Collectors.toList()); + List groups = (ArrayList) books.stream().distinct().collect(Collectors.toList()); double price = 0; @@ -34,6 +35,10 @@ public static double CalculateTotalCost (ArrayList books,double priceSo } private static double CostPerGroup(int groupSize) throws Exception{ + double[] discountPercentage = {0, 0, 0.05, 0.1, 0.2, 0.25}; + + + /* double discountPercentage; switch (groupSize) { @@ -55,8 +60,8 @@ private static double CostPerGroup(int groupSize) throws Exception{ default: throw new Exception("Invalide group size : " + groupSize ); } - - return 8 * groupSize * (100 - discountPercentage) / 100; +*/ + return 8 * groupSize * (1 - discountPercentage[groupSize]); } } diff --git a/exercises/book-store/src/test/java/BookStoreTest.java b/exercises/book-store/src/test/java/BookStoreTest.java index 81d04cc30..a41d6c43f 100644 --- a/exercises/book-store/src/test/java/BookStoreTest.java +++ b/exercises/book-store/src/test/java/BookStoreTest.java @@ -1,7 +1,9 @@ import static org.junit.Assert.assertEquals; +import java.util.List; import java.util.ArrayList; import java.util.Arrays; + import org.junit.Test; public class BookStoreTest{ @@ -9,7 +11,7 @@ public class BookStoreTest{ @Test public void OnlyASingleBook(){ Integer[] p = {1}; - ArrayList books = new ArrayList<>(Arrays.asList(p)); + List books = new ArrayList<>(Arrays.asList(p)); assertEquals(8,BookStore.CalculateTotalCost(books),2); } @@ -17,14 +19,14 @@ public void OnlyASingleBook(){ @Test public void TwoOfSameBook(){ Integer[] p = {1,1}; - ArrayList books = new ArrayList<>(Arrays.asList(p)); + List books = new ArrayList<>(Arrays.asList(p)); assertEquals(16,BookStore.CalculateTotalCost(books),2); } @Ignore @Test public void EmptyBasket(){ - ArrayList books = new ArrayList<>(); + List books = new ArrayList<>(); assertEquals(0,BookStore.CalculateTotalCost(books),2); } @@ -32,7 +34,7 @@ public void EmptyBasket(){ @Test public void TwoDifferentBooks(){ Integer[] p = {1,2}; - ArrayList books = new ArrayList<>(Arrays.asList(p)); + List books = new ArrayList<>(Arrays.asList(p)); assertEquals(15.20,BookStore.CalculateTotalCost(books),2); } @@ -40,7 +42,7 @@ public void TwoDifferentBooks(){ @Test public void ThreeDifferentBooks(){ Integer[] p = {1,2,3}; - ArrayList books = new ArrayList<>(Arrays.asList(p)); + List books = new ArrayList<>(Arrays.asList(p)); assertEquals(21.6,BookStore.CalculateTotalCost(books),2); } @@ -65,7 +67,7 @@ public void FiveDifferentBooks(){ @Test public void TwoGroupsOfFourIsCheaperThanGroupOfFivePlusGroupOfThree(){ Integer[] p = {1,1,2,2,3,3,4,5}; - ArrayList books = new ArrayList<>(Arrays.asList(p)); + List books = new ArrayList<>(Arrays.asList(p)); assertEquals(51.20,BookStore.CalculateTotalCost(books),2); } @@ -73,7 +75,7 @@ public void TwoGroupsOfFourIsCheaperThanGroupOfFivePlusGroupOfThree(){ @Test public void GroupOfFourPlusGroupOfTwoIsCheaperThanTwoGroupsOfThree(){ Integer[] p = {1,1,2,2,3,4}; - ArrayList books = new ArrayList<>(Arrays.asList(p)); + List books = new ArrayList<>(Arrays.asList(p)); assertEquals(40.8,BookStore.CalculateTotalCost(books),2); } @@ -81,7 +83,7 @@ public void GroupOfFourPlusGroupOfTwoIsCheaperThanTwoGroupsOfThree(){ @Test public void TwoEachOfFirst4BooksAnd1CopyEachOfRest(){ Integer[] p = {1,1,2,2,3,3,4,4,5}; - ArrayList books = new ArrayList<>(Arrays.asList(p)); + List books = new ArrayList<>(Arrays.asList(p)); assertEquals(55.60,BookStore.CalculateTotalCost(books),2); } @@ -89,7 +91,7 @@ public void TwoEachOfFirst4BooksAnd1CopyEachOfRest(){ @Test public void TwoCopiesOfEachBook(){ Integer[] p = {1,1,2,2,3,3,4,4,5,5}; - ArrayList books = new ArrayList<>(Arrays.asList(p)); + List books = new ArrayList<>(Arrays.asList(p)); assertEquals(60.00,BookStore.CalculateTotalCost(books),2); } @@ -97,7 +99,7 @@ public void TwoCopiesOfEachBook(){ @Test public void ThreeCopiesOfFirstBookAnd2EachOfRemaining(){ Integer[] p = {1,1,2,2,3,3,4,4,5,5,1}; - ArrayList books = new ArrayList<>(Arrays.asList(p)); + List books = new ArrayList<>(Arrays.asList(p)); assertEquals(68.00,BookStore.CalculateTotalCost(books),2); } @@ -105,7 +107,7 @@ public void ThreeCopiesOfFirstBookAnd2EachOfRemaining(){ @Test public void ThreeEachOFirst2BooksAnd2EachOfRemainingBooks(){ Integer[] p = {1,1,2,2,3,3,4,4,5,5,1,2}; - ArrayList books = new ArrayList<>(Arrays.asList(p)); + List books = new ArrayList<>(Arrays.asList(p)); assertEquals(75.20,BookStore.CalculateTotalCost(books),2); } From f26f9e72a6d44c023fa9507bf9dc835cc03b793f Mon Sep 17 00:00:00 2001 From: Ladent Thibault Date: Thu, 16 Mar 2017 13:22:53 +0100 Subject: [PATCH 09/17] using instance methods instead of class method --- .../src/example/java/BookStore.java | 51 ++++++++++++++----- .../src/test/java/BookStoreTest.java | 39 +++++++++----- 2 files changed, 65 insertions(+), 25 deletions(-) diff --git a/exercises/book-store/src/example/java/BookStore.java b/exercises/book-store/src/example/java/BookStore.java index 63420d28b..e53b7b58f 100644 --- a/exercises/book-store/src/example/java/BookStore.java +++ b/exercises/book-store/src/example/java/BookStore.java @@ -1,44 +1,56 @@ import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class BookStore{ - public static double CalculateTotalCost(List books ){ - return CalculateTotalCost(books,0); + + private int bookPrice, maxGroupSize; + private double discountIncrement; + private List books; + + public BookStore (List books){ + this.books = books; + this.bookPrice = 8; + this.maxGroupSize = 5; + this.discountIncrement = 0.05; + } + + public double CalculateTotalCost(){ + return CalculateTotalCost(this.books,0); } - public static double CalculateTotalCost (List books,double priceSoFar ){ + private double CalculateTotalCost (List books,double priceSoFar ){ double minPrice = Double.MAX_VALUE; + if(books.size() == 0) return priceSoFar; List groups = (ArrayList) books.stream().distinct().collect(Collectors.toList()); - + double price = 0; for(int i = 0;i books = new ArrayList<>(Arrays.asList(p)); + BookStore bookStore = new BookStore(books); + System.out.println(bookStore.CalculateTotalCost()); + + + + Integer[] p1 = {1,2}; + List books1 = new ArrayList<>(Arrays.asList(p1)); + BookStore bookStore1 = new BookStore(books1); + System.out.println(bookStore1.CalculateTotalCost()); + + } } diff --git a/exercises/book-store/src/test/java/BookStoreTest.java b/exercises/book-store/src/test/java/BookStoreTest.java index a41d6c43f..65d265cd8 100644 --- a/exercises/book-store/src/test/java/BookStoreTest.java +++ b/exercises/book-store/src/test/java/BookStoreTest.java @@ -12,7 +12,8 @@ public class BookStoreTest{ public void OnlyASingleBook(){ Integer[] p = {1}; List books = new ArrayList<>(Arrays.asList(p)); - assertEquals(8,BookStore.CalculateTotalCost(books),2); + BookStore bookStore = new BookStore(books); + assertEquals(8,bookStore.CalculateTotalCost(),2); } @Ignore @@ -20,14 +21,16 @@ public void OnlyASingleBook(){ public void TwoOfSameBook(){ Integer[] p = {1,1}; List books = new ArrayList<>(Arrays.asList(p)); - assertEquals(16,BookStore.CalculateTotalCost(books),2); + BookStore bookStore = new BookStore(books); + assertEquals(16,bookStore.CalculateTotalCost(),2); } @Ignore @Test public void EmptyBasket(){ List books = new ArrayList<>(); - assertEquals(0,BookStore.CalculateTotalCost(books),2); + BookStore bookStore = new BookStore(books); + assertEquals(0,bookStore.CalculateTotalCost(),2); } @Ignore @@ -35,7 +38,8 @@ public void EmptyBasket(){ public void TwoDifferentBooks(){ Integer[] p = {1,2}; List books = new ArrayList<>(Arrays.asList(p)); - assertEquals(15.20,BookStore.CalculateTotalCost(books),2); + BookStore bookStore = new BookStore(books); + assertEquals(15.20,bookStore.CalculateTotalCost(),2); } @Ignore @@ -43,7 +47,8 @@ public void TwoDifferentBooks(){ public void ThreeDifferentBooks(){ Integer[] p = {1,2,3}; List books = new ArrayList<>(Arrays.asList(p)); - assertEquals(21.6,BookStore.CalculateTotalCost(books),2); + BookStore bookStore = new BookStore(books); + assertEquals(21.6,bookStore.CalculateTotalCost(),2); } @Ignore @@ -51,7 +56,8 @@ public void ThreeDifferentBooks(){ public void FourDifferentBooks(){ Integer[] p = {1,2,3,4}; ArrayList books = new ArrayList<>(Arrays.asList(p)); - assertEquals(25.6,BookStore.CalculateTotalCost(books),2); + BookStore bookStore = new BookStore(books); + assertEquals(25.6,bookStore.CalculateTotalCost(),2); } @@ -60,7 +66,8 @@ public void FourDifferentBooks(){ public void FiveDifferentBooks(){ Integer[] p = {1,2,3,4,5}; ArrayList books = new ArrayList<>(Arrays.asList(p)); - assertEquals(30,BookStore.CalculateTotalCost(books),2); + BookStore bookStore = new BookStore(books); + assertEquals(30,bookStore.CalculateTotalCost(),2); } @Ignore @@ -68,7 +75,8 @@ public void FiveDifferentBooks(){ public void TwoGroupsOfFourIsCheaperThanGroupOfFivePlusGroupOfThree(){ Integer[] p = {1,1,2,2,3,3,4,5}; List books = new ArrayList<>(Arrays.asList(p)); - assertEquals(51.20,BookStore.CalculateTotalCost(books),2); + BookStore bookStore = new BookStore(books); + assertEquals(51.20,bookStore.CalculateTotalCost(),2); } @Ignore @@ -76,7 +84,8 @@ public void TwoGroupsOfFourIsCheaperThanGroupOfFivePlusGroupOfThree(){ public void GroupOfFourPlusGroupOfTwoIsCheaperThanTwoGroupsOfThree(){ Integer[] p = {1,1,2,2,3,4}; List books = new ArrayList<>(Arrays.asList(p)); - assertEquals(40.8,BookStore.CalculateTotalCost(books),2); + BookStore bookStore = new BookStore(books); + assertEquals(40.8,bookStore.CalculateTotalCost(),2); } @Ignore @@ -84,7 +93,8 @@ public void GroupOfFourPlusGroupOfTwoIsCheaperThanTwoGroupsOfThree(){ public void TwoEachOfFirst4BooksAnd1CopyEachOfRest(){ Integer[] p = {1,1,2,2,3,3,4,4,5}; List books = new ArrayList<>(Arrays.asList(p)); - assertEquals(55.60,BookStore.CalculateTotalCost(books),2); + BookStore bookStore = new BookStore(books); + assertEquals(55.60,bookStore.CalculateTotalCost(),2); } @Ignore @@ -92,7 +102,8 @@ public void TwoEachOfFirst4BooksAnd1CopyEachOfRest(){ public void TwoCopiesOfEachBook(){ Integer[] p = {1,1,2,2,3,3,4,4,5,5}; List books = new ArrayList<>(Arrays.asList(p)); - assertEquals(60.00,BookStore.CalculateTotalCost(books),2); + BookStore bookStore = new BookStore(books); + assertEquals(60.00,bookStore.CalculateTotalCost(),2); } @Ignore @@ -100,7 +111,8 @@ public void TwoCopiesOfEachBook(){ public void ThreeCopiesOfFirstBookAnd2EachOfRemaining(){ Integer[] p = {1,1,2,2,3,3,4,4,5,5,1}; List books = new ArrayList<>(Arrays.asList(p)); - assertEquals(68.00,BookStore.CalculateTotalCost(books),2); + BookStore bookStore = new BookStore(books); + assertEquals(68.00,bookStore.CalculateTotalCost(),2); } @Ignore @@ -108,7 +120,8 @@ public void ThreeCopiesOfFirstBookAnd2EachOfRemaining(){ public void ThreeEachOFirst2BooksAnd2EachOfRemainingBooks(){ Integer[] p = {1,1,2,2,3,3,4,4,5,5,1,2}; List books = new ArrayList<>(Arrays.asList(p)); - assertEquals(75.20,BookStore.CalculateTotalCost(books),2); + BookStore bookStore = new BookStore(books); + assertEquals(75.20,bookStore.CalculateTotalCost(),2); } } From 28c89b0b3ba9633c7cbff42d52fab700817ddad2 Mon Sep 17 00:00:00 2001 From: Ladent Thibault Date: Thu, 16 Mar 2017 13:24:38 +0100 Subject: [PATCH 10/17] delete useless stuff --- .../book-store/src/example/java/BookStore.java | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/exercises/book-store/src/example/java/BookStore.java b/exercises/book-store/src/example/java/BookStore.java index e53b7b58f..cdcec725c 100644 --- a/exercises/book-store/src/example/java/BookStore.java +++ b/exercises/book-store/src/example/java/BookStore.java @@ -75,20 +75,4 @@ private double CostPerGroup(int groupSize) throws Exception{ return 8 * groupSize * (100 - discountPercentage) / 100; } - - - public static void main (String[] argv){ - Integer[] p = {1}; - List books = new ArrayList<>(Arrays.asList(p)); - BookStore bookStore = new BookStore(books); - System.out.println(bookStore.CalculateTotalCost()); - - - - Integer[] p1 = {1,2}; - List books1 = new ArrayList<>(Arrays.asList(p1)); - BookStore bookStore1 = new BookStore(books1); - System.out.println(bookStore1.CalculateTotalCost()); - - } } From 1142bffd8a641a01c5ac6b0be2d03f5b3c16c242 Mon Sep 17 00:00:00 2001 From: Ladent Thibault Date: Thu, 16 Mar 2017 13:43:51 +0100 Subject: [PATCH 11/17] modify magic numbers --- exercises/book-store/src/example/java/BookStore.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/exercises/book-store/src/example/java/BookStore.java b/exercises/book-store/src/example/java/BookStore.java index cdcec725c..0858c452b 100644 --- a/exercises/book-store/src/example/java/BookStore.java +++ b/exercises/book-store/src/example/java/BookStore.java @@ -12,8 +12,6 @@ public class BookStore{ public BookStore (List books){ this.books = books; this.bookPrice = 8; - this.maxGroupSize = 5; - this.discountIncrement = 0.05; } public double CalculateTotalCost(){ @@ -72,7 +70,7 @@ private double CostPerGroup(int groupSize) throws Exception{ default: throw new Exception("Invalide group size : " + groupSize ); } - return 8 * groupSize * (100 - discountPercentage) / 100; + return bookPrice * groupSize * (100 - discountPercentage) / 100; } } From a5517932a9ada44d4edecd2683a1b83f90cb8933 Mon Sep 17 00:00:00 2001 From: Ladent Thibault Date: Fri, 17 Mar 2017 12:17:49 +0100 Subject: [PATCH 12/17] update CostPerGroup method --- .../src/example/java/BookStore.java | 46 +++++++------------ 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/exercises/book-store/src/example/java/BookStore.java b/exercises/book-store/src/example/java/BookStore.java index 0858c452b..9f41a7e44 100644 --- a/exercises/book-store/src/example/java/BookStore.java +++ b/exercises/book-store/src/example/java/BookStore.java @@ -1,3 +1,4 @@ +import java.awt.print.Printable; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -7,12 +8,18 @@ public class BookStore{ private int bookPrice, maxGroupSize; private double discountIncrement; - private List books; + private List books; + private static double[] discount_tiers = {0,5,10,20,25}; public BookStore (List books){ this.books = books; this.bookPrice = 8; + this.maxGroupSize = 5; + this.discountIncrement = 0.05; } + + + public double CalculateTotalCost(){ return CalculateTotalCost(this.books,0); @@ -22,25 +29,24 @@ private double CalculateTotalCost (List books,double priceSoFar ){ double minPrice = Double.MAX_VALUE; - if(books.size() == 0) + if(books.size() == 0) return priceSoFar; List groups = (ArrayList) books.stream().distinct().collect(Collectors.toList()); - + double price = 0; for(int i = 0;i books,double priceSoFar ){ } private double CostPerGroup(int groupSize) throws Exception{ + if (groupSize < 1 || groupSize > maxGroupSize){ + throw new Exception("Invalid group size : " + groupSize ); + } + return bookPrice * groupSize * (100 - discount_tiers[groupSize-1])/100; + } - double discountPercentage; - switch (groupSize) - { - case 1: - discountPercentage = 0; - break; - case 2: - discountPercentage = 5; - break; - case 3: - discountPercentage = 10; - break; - case 4: - discountPercentage = 20; - break; - case 5: - discountPercentage = 25; - break; - default: - throw new Exception("Invalide group size : " + groupSize ); - } - return bookPrice * groupSize * (100 - discountPercentage) / 100; - } } From c8233901b9f222ed4700784d179538f4076fff70 Mon Sep 17 00:00:00 2001 From: Ladent Thibault Date: Fri, 17 Mar 2017 12:54:31 +0100 Subject: [PATCH 13/17] update class name and files name to Bookstore from Bookstore + using camelCase for methods --- .../java/{BookStore.java => Bookstore.java} | 14 +- .../src/test/java/BookStoreTest.java | 128 ------------------ .../src/test/java/BookstoreTest.java | 128 ++++++++++++++++++ 3 files changed, 135 insertions(+), 135 deletions(-) rename exercises/book-store/src/example/java/{BookStore.java => Bookstore.java} (76%) delete mode 100644 exercises/book-store/src/test/java/BookStoreTest.java create mode 100644 exercises/book-store/src/test/java/BookstoreTest.java diff --git a/exercises/book-store/src/example/java/BookStore.java b/exercises/book-store/src/example/java/Bookstore.java similarity index 76% rename from exercises/book-store/src/example/java/BookStore.java rename to exercises/book-store/src/example/java/Bookstore.java index 9f41a7e44..473439db3 100644 --- a/exercises/book-store/src/example/java/BookStore.java +++ b/exercises/book-store/src/example/java/Bookstore.java @@ -4,14 +4,14 @@ import java.util.List; import java.util.stream.Collectors; -public class BookStore{ +public class Bookstore{ private int bookPrice, maxGroupSize; private double discountIncrement; private List books; private static double[] discount_tiers = {0,5,10,20,25}; - public BookStore (List books){ + public Bookstore (List books){ this.books = books; this.bookPrice = 8; this.maxGroupSize = 5; @@ -21,11 +21,11 @@ public BookStore (List books){ - public double CalculateTotalCost(){ - return CalculateTotalCost(this.books,0); + public double calculateTotalCost(){ + return calculateTotalCost(this.books,0); } - private double CalculateTotalCost (List books,double priceSoFar ){ + private double calculateTotalCost (List books,double priceSoFar ){ double minPrice = Double.MAX_VALUE; @@ -42,7 +42,7 @@ private double CalculateTotalCost (List books,double priceSoFar ){ } try { - price = CalculateTotalCost(books,priceSoFar + CostPerGroup(groups.size())); + price = calculateTotalCost(books,priceSoFar + costPerGroup(groups.size())); } catch (Exception e) { e.printStackTrace(); } @@ -53,7 +53,7 @@ private double CalculateTotalCost (List books,double priceSoFar ){ } - private double CostPerGroup(int groupSize) throws Exception{ + private double costPerGroup(int groupSize) throws Exception{ if (groupSize < 1 || groupSize > maxGroupSize){ throw new Exception("Invalid group size : " + groupSize ); } diff --git a/exercises/book-store/src/test/java/BookStoreTest.java b/exercises/book-store/src/test/java/BookStoreTest.java deleted file mode 100644 index 65d265cd8..000000000 --- a/exercises/book-store/src/test/java/BookStoreTest.java +++ /dev/null @@ -1,128 +0,0 @@ -import static org.junit.Assert.assertEquals; - -import java.util.List; -import java.util.ArrayList; -import java.util.Arrays; - -import org.junit.Test; - -public class BookStoreTest{ - - @Test - public void OnlyASingleBook(){ - Integer[] p = {1}; - List books = new ArrayList<>(Arrays.asList(p)); - BookStore bookStore = new BookStore(books); - assertEquals(8,bookStore.CalculateTotalCost(),2); - } - - @Ignore - @Test - public void TwoOfSameBook(){ - Integer[] p = {1,1}; - List books = new ArrayList<>(Arrays.asList(p)); - BookStore bookStore = new BookStore(books); - assertEquals(16,bookStore.CalculateTotalCost(),2); - } - - @Ignore - @Test - public void EmptyBasket(){ - List books = new ArrayList<>(); - BookStore bookStore = new BookStore(books); - assertEquals(0,bookStore.CalculateTotalCost(),2); - } - - @Ignore - @Test - public void TwoDifferentBooks(){ - Integer[] p = {1,2}; - List books = new ArrayList<>(Arrays.asList(p)); - BookStore bookStore = new BookStore(books); - assertEquals(15.20,bookStore.CalculateTotalCost(),2); - } - - @Ignore - @Test - public void ThreeDifferentBooks(){ - Integer[] p = {1,2,3}; - List books = new ArrayList<>(Arrays.asList(p)); - BookStore bookStore = new BookStore(books); - assertEquals(21.6,bookStore.CalculateTotalCost(),2); - } - - @Ignore - @Test - public void FourDifferentBooks(){ - Integer[] p = {1,2,3,4}; - ArrayList books = new ArrayList<>(Arrays.asList(p)); - BookStore bookStore = new BookStore(books); - assertEquals(25.6,bookStore.CalculateTotalCost(),2); - - } - - @Ignore - @Test - public void FiveDifferentBooks(){ - Integer[] p = {1,2,3,4,5}; - ArrayList books = new ArrayList<>(Arrays.asList(p)); - BookStore bookStore = new BookStore(books); - assertEquals(30,bookStore.CalculateTotalCost(),2); - } - - @Ignore - @Test - public void TwoGroupsOfFourIsCheaperThanGroupOfFivePlusGroupOfThree(){ - Integer[] p = {1,1,2,2,3,3,4,5}; - List books = new ArrayList<>(Arrays.asList(p)); - BookStore bookStore = new BookStore(books); - assertEquals(51.20,bookStore.CalculateTotalCost(),2); - } - - @Ignore - @Test - public void GroupOfFourPlusGroupOfTwoIsCheaperThanTwoGroupsOfThree(){ - Integer[] p = {1,1,2,2,3,4}; - List books = new ArrayList<>(Arrays.asList(p)); - BookStore bookStore = new BookStore(books); - assertEquals(40.8,bookStore.CalculateTotalCost(),2); - } - - @Ignore - @Test - public void TwoEachOfFirst4BooksAnd1CopyEachOfRest(){ - Integer[] p = {1,1,2,2,3,3,4,4,5}; - List books = new ArrayList<>(Arrays.asList(p)); - BookStore bookStore = new BookStore(books); - assertEquals(55.60,bookStore.CalculateTotalCost(),2); - } - - @Ignore - @Test - public void TwoCopiesOfEachBook(){ - Integer[] p = {1,1,2,2,3,3,4,4,5,5}; - List books = new ArrayList<>(Arrays.asList(p)); - BookStore bookStore = new BookStore(books); - assertEquals(60.00,bookStore.CalculateTotalCost(),2); - } - - @Ignore - @Test - public void ThreeCopiesOfFirstBookAnd2EachOfRemaining(){ - Integer[] p = {1,1,2,2,3,3,4,4,5,5,1}; - List books = new ArrayList<>(Arrays.asList(p)); - BookStore bookStore = new BookStore(books); - assertEquals(68.00,bookStore.CalculateTotalCost(),2); - } - - @Ignore - @Test - public void ThreeEachOFirst2BooksAnd2EachOfRemainingBooks(){ - Integer[] p = {1,1,2,2,3,3,4,4,5,5,1,2}; - List books = new ArrayList<>(Arrays.asList(p)); - BookStore bookStore = new BookStore(books); - assertEquals(75.20,bookStore.CalculateTotalCost(),2); - } - -} - diff --git a/exercises/book-store/src/test/java/BookstoreTest.java b/exercises/book-store/src/test/java/BookstoreTest.java new file mode 100644 index 000000000..7f744a094 --- /dev/null +++ b/exercises/book-store/src/test/java/BookstoreTest.java @@ -0,0 +1,128 @@ +import static org.junit.Assert.assertEquals; + +import java.util.List; +import java.util.ArrayList; +import java.util.Arrays; + +import org.junit.Test; + +public class BookstoreTest{ + + @Test + public void onlyASingleBook(){ + Integer[] p = {1}; + List books = new ArrayList<>(Arrays.asList(p)); + Bookstore bookstore = new Bookstore(books); + assertEquals(8,bookstore.calculateTotalCost(),2); + } + + @Ignore + @Test + public void twoOfSameBook(){ + Integer[] p = {1,1}; + List books = new ArrayList<>(Arrays.asList(p)); + Bookstore bookstore = new Bookstore(books); + assertEquals(16,bookstore.calculateTotalCost(),2); + } + + @Ignore + @Test + public void emptyBasket(){ + List books = new ArrayList<>(); + Bookstore bookstore = new Bookstore(books); + assertEquals(0,bookstore.calculateTotalCost(),2); + } + + @Ignore + @Test + public void twoDifferentBooks(){ + Integer[] p = {1,2}; + List books = new ArrayList<>(Arrays.asList(p)); + Bookstore bookstore = new Bookstore(books); + assertEquals(15.20,bookstore.calculateTotalCost(),2); + } + + @Ignore + @Test + public void threeDifferentBooks(){ + Integer[] p = {1,2,3}; + List books = new ArrayList<>(Arrays.asList(p)); + Bookstore bookstore = new Bookstore(books); + assertEquals(21.6,bookstore.calculateTotalCost(),2); + } + + @Ignore + @Test + public void fourDifferentBooks(){ + Integer[] p = {1,2,3,4}; + ArrayList books = new ArrayList<>(Arrays.asList(p)); + Bookstore bookstore = new Bookstore(books); + assertEquals(25.6,bookstore.calculateTotalCost(),2); + + } + + @Ignore + @Test + public void fiveDifferentBooks(){ + Integer[] p = {1,2,3,4,5}; + ArrayList books = new ArrayList<>(Arrays.asList(p)); + Bookstore bookstore = new Bookstore(books); + assertEquals(30,bookstore.calculateTotalCost(),2); + } + + @Ignore + @Test + public void twoGroupsOfFourIsCheaperThanGroupOfFivePlusGroupOfThree(){ + Integer[] p = {1,1,2,2,3,3,4,5}; + List books = new ArrayList<>(Arrays.asList(p)); + Bookstore bookstore = new Bookstore(books); + assertEquals(51.20,bookstore.calculateTotalCost(),2); + } + + @Ignore + @Test + public void groupOfFourPlusGroupOfTwoIsCheaperThanTwoGroupsOfThree(){ + Integer[] p = {1,1,2,2,3,4}; + List books = new ArrayList<>(Arrays.asList(p)); + Bookstore bookstore = new Bookstore(books); + assertEquals(40.8,bookstore.calculateTotalCost(),2); + } + + @Ignore + @Test + public void twoEachOfFirst4BooksAnd1CopyEachOfRest(){ + Integer[] p = {1,1,2,2,3,3,4,4,5}; + List books = new ArrayList<>(Arrays.asList(p)); + Bookstore bookstore = new Bookstore(books); + assertEquals(55.60,bookstore.calculateTotalCost(),2); + } + + @Ignore + @Test + public void twoCopiesOfEachBook(){ + Integer[] p = {1,1,2,2,3,3,4,4,5,5}; + List books = new ArrayList<>(Arrays.asList(p)); + Bookstore bookstore = new Bookstore(books); + assertEquals(60.00,bookstore.calculateTotalCost(),2); + } + + @Ignore + @Test + public void threeCopiesOfFirstBookAnd2EachOfRemaining(){ + Integer[] p = {1,1,2,2,3,3,4,4,5,5,1}; + List books = new ArrayList<>(Arrays.asList(p)); + Bookstore bookstore = new Bookstore(books); + assertEquals(68.00,bookstore.calculateTotalCost(),2); + } + + @Ignore + @Test + public void threeEachOFirst2BooksAnd2EachOfRemainingBooks(){ + Integer[] p = {1,1,2,2,3,3,4,4,5,5,1,2}; + List books = new ArrayList<>(Arrays.asList(p)); + Bookstore bookstore = new Bookstore(books); + assertEquals(75.20,bookstore.calculateTotalCost(),2); + } + +} + From 94e6ee0dff705d6ba9da4bdce0fd96aada72ea9e Mon Sep 17 00:00:00 2001 From: Ladent Thibault Date: Fri, 17 Mar 2017 13:14:41 +0100 Subject: [PATCH 14/17] pass the integers directly to the Arrays.asList() methods --- .../src/test/java/BookstoreTest.java | 35 +++++++------------ 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/exercises/book-store/src/test/java/BookstoreTest.java b/exercises/book-store/src/test/java/BookstoreTest.java index 7f744a094..1593dc405 100644 --- a/exercises/book-store/src/test/java/BookstoreTest.java +++ b/exercises/book-store/src/test/java/BookstoreTest.java @@ -10,8 +10,7 @@ public class BookstoreTest{ @Test public void onlyASingleBook(){ - Integer[] p = {1}; - List books = new ArrayList<>(Arrays.asList(p)); + List books = new ArrayList<>(Arrays.asList(1)); Bookstore bookstore = new Bookstore(books); assertEquals(8,bookstore.calculateTotalCost(),2); } @@ -19,8 +18,7 @@ public void onlyASingleBook(){ @Ignore @Test public void twoOfSameBook(){ - Integer[] p = {1,1}; - List books = new ArrayList<>(Arrays.asList(p)); + List books = new ArrayList<>(Arrays.asList(1,1)); Bookstore bookstore = new Bookstore(books); assertEquals(16,bookstore.calculateTotalCost(),2); } @@ -36,8 +34,7 @@ public void emptyBasket(){ @Ignore @Test public void twoDifferentBooks(){ - Integer[] p = {1,2}; - List books = new ArrayList<>(Arrays.asList(p)); + List books = new ArrayList<>(Arrays.asList(1,2)); Bookstore bookstore = new Bookstore(books); assertEquals(15.20,bookstore.calculateTotalCost(),2); } @@ -45,8 +42,7 @@ public void twoDifferentBooks(){ @Ignore @Test public void threeDifferentBooks(){ - Integer[] p = {1,2,3}; - List books = new ArrayList<>(Arrays.asList(p)); + List books = new ArrayList<>(Arrays.asList(1,2,3)); Bookstore bookstore = new Bookstore(books); assertEquals(21.6,bookstore.calculateTotalCost(),2); } @@ -54,8 +50,7 @@ public void threeDifferentBooks(){ @Ignore @Test public void fourDifferentBooks(){ - Integer[] p = {1,2,3,4}; - ArrayList books = new ArrayList<>(Arrays.asList(p)); + ArrayList books = new ArrayList<>(Arrays.asList(1,2,3,4)); Bookstore bookstore = new Bookstore(books); assertEquals(25.6,bookstore.calculateTotalCost(),2); @@ -64,8 +59,7 @@ public void fourDifferentBooks(){ @Ignore @Test public void fiveDifferentBooks(){ - Integer[] p = {1,2,3,4,5}; - ArrayList books = new ArrayList<>(Arrays.asList(p)); + ArrayList books = new ArrayList<>(Arrays.asList(1,2,3,4,5)); Bookstore bookstore = new Bookstore(books); assertEquals(30,bookstore.calculateTotalCost(),2); } @@ -73,8 +67,7 @@ public void fiveDifferentBooks(){ @Ignore @Test public void twoGroupsOfFourIsCheaperThanGroupOfFivePlusGroupOfThree(){ - Integer[] p = {1,1,2,2,3,3,4,5}; - List books = new ArrayList<>(Arrays.asList(p)); + List books = new ArrayList<>(Arrays.asList(1,1,2,2,3,3,4,5)); Bookstore bookstore = new Bookstore(books); assertEquals(51.20,bookstore.calculateTotalCost(),2); } @@ -82,8 +75,7 @@ public void twoGroupsOfFourIsCheaperThanGroupOfFivePlusGroupOfThree(){ @Ignore @Test public void groupOfFourPlusGroupOfTwoIsCheaperThanTwoGroupsOfThree(){ - Integer[] p = {1,1,2,2,3,4}; - List books = new ArrayList<>(Arrays.asList(p)); + List books = new ArrayList<>(Arrays.asList(1,1,2,2,3,4)); Bookstore bookstore = new Bookstore(books); assertEquals(40.8,bookstore.calculateTotalCost(),2); } @@ -92,7 +84,7 @@ public void groupOfFourPlusGroupOfTwoIsCheaperThanTwoGroupsOfThree(){ @Test public void twoEachOfFirst4BooksAnd1CopyEachOfRest(){ Integer[] p = {1,1,2,2,3,3,4,4,5}; - List books = new ArrayList<>(Arrays.asList(p)); + List books = new ArrayList<>(Arrays.asList(1,1,2,2,3,3,4,4,5)); Bookstore bookstore = new Bookstore(books); assertEquals(55.60,bookstore.calculateTotalCost(),2); } @@ -100,8 +92,7 @@ public void twoEachOfFirst4BooksAnd1CopyEachOfRest(){ @Ignore @Test public void twoCopiesOfEachBook(){ - Integer[] p = {1,1,2,2,3,3,4,4,5,5}; - List books = new ArrayList<>(Arrays.asList(p)); + List books = new ArrayList<>(Arrays.asList(1,1,2,2,3,3,4,4,5,5)); Bookstore bookstore = new Bookstore(books); assertEquals(60.00,bookstore.calculateTotalCost(),2); } @@ -109,8 +100,7 @@ public void twoCopiesOfEachBook(){ @Ignore @Test public void threeCopiesOfFirstBookAnd2EachOfRemaining(){ - Integer[] p = {1,1,2,2,3,3,4,4,5,5,1}; - List books = new ArrayList<>(Arrays.asList(p)); + List books = new ArrayList<>(Arrays.asList(1,1,2,2,3,3,4,4,5,5,1)); Bookstore bookstore = new Bookstore(books); assertEquals(68.00,bookstore.calculateTotalCost(),2); } @@ -118,8 +108,7 @@ public void threeCopiesOfFirstBookAnd2EachOfRemaining(){ @Ignore @Test public void threeEachOFirst2BooksAnd2EachOfRemainingBooks(){ - Integer[] p = {1,1,2,2,3,3,4,4,5,5,1,2}; - List books = new ArrayList<>(Arrays.asList(p)); + List books = new ArrayList<>(Arrays.asList(1,1,2,2,3,3,4,4,5,5,1,2)); Bookstore bookstore = new Bookstore(books); assertEquals(75.20,bookstore.calculateTotalCost(),2); } From 18fd7315812db405ab0cfad0b38fcafd3a81dec3 Mon Sep 17 00:00:00 2001 From: Ladent Thibault Date: Fri, 17 Mar 2017 14:56:36 +0100 Subject: [PATCH 15/17] adding brackets --- exercises/book-store/src/example/java/Bookstore.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/exercises/book-store/src/example/java/Bookstore.java b/exercises/book-store/src/example/java/Bookstore.java index 473439db3..f6aa07b89 100644 --- a/exercises/book-store/src/example/java/Bookstore.java +++ b/exercises/book-store/src/example/java/Bookstore.java @@ -29,8 +29,9 @@ private double calculateTotalCost (List books,double priceSoFar ){ double minPrice = Double.MAX_VALUE; - if(books.size() == 0) + if(books.size() == 0){ return priceSoFar; + } List groups = (ArrayList) books.stream().distinct().collect(Collectors.toList()); From b1f4e7fef19f14cd82a834d67c1cdec03d7f2f18 Mon Sep 17 00:00:00 2001 From: Ladent Thibault Date: Fri, 17 Mar 2017 15:06:03 +0100 Subject: [PATCH 16/17] fix constant variable --- exercises/book-store/src/example/java/Bookstore.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/exercises/book-store/src/example/java/Bookstore.java b/exercises/book-store/src/example/java/Bookstore.java index f6aa07b89..92edf6caf 100644 --- a/exercises/book-store/src/example/java/Bookstore.java +++ b/exercises/book-store/src/example/java/Bookstore.java @@ -6,16 +6,12 @@ public class Bookstore{ - private int bookPrice, maxGroupSize; - private double discountIncrement; + private int BOOK_PRICE = 8, MAX_GROUP_SIZE = 5; private List books; - private static double[] discount_tiers = {0,5,10,20,25}; + private static double[] DISCOUNT_TIERS = {0,5,10,20,25}; public Bookstore (List books){ this.books = books; - this.bookPrice = 8; - this.maxGroupSize = 5; - this.discountIncrement = 0.05; } @@ -55,10 +51,10 @@ private double calculateTotalCost (List books,double priceSoFar ){ } private double costPerGroup(int groupSize) throws Exception{ - if (groupSize < 1 || groupSize > maxGroupSize){ + if (groupSize < 1 || groupSize > MAX_GROUP_SIZE){ throw new Exception("Invalid group size : " + groupSize ); } - return bookPrice * groupSize * (100 - discount_tiers[groupSize-1])/100; + return BOOK_PRICE * groupSize * (100 - DISCOUNT_TIERS[groupSize-1])/100; } From a8303ad76bf1ad194658e5ad95e79383fd6c09fc Mon Sep 17 00:00:00 2001 From: Thibault LADENT Date: Fri, 17 Mar 2017 15:18:05 +0100 Subject: [PATCH 17/17] using static for constants --- exercises/book-store/src/example/java/Bookstore.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/book-store/src/example/java/Bookstore.java b/exercises/book-store/src/example/java/Bookstore.java index 92edf6caf..54908ed64 100644 --- a/exercises/book-store/src/example/java/Bookstore.java +++ b/exercises/book-store/src/example/java/Bookstore.java @@ -6,7 +6,7 @@ public class Bookstore{ - private int BOOK_PRICE = 8, MAX_GROUP_SIZE = 5; + private static int BOOK_PRICE = 8, MAX_GROUP_SIZE = 5; private List books; private static double[] DISCOUNT_TIERS = {0,5,10,20,25};