diff --git a/config.json b/config.json index 12c8752da..46fa47592 100644 --- a/config.json +++ b/config.json @@ -315,10 +315,15 @@ "difficulty": 1, "topics": [] }, - { + { "slug": "isogram", "difficulty": 1, "topics": [] + }, + { + "slug": "book-store", + "difficulty": 1, + "topics": [] } ], "deprecated": [ 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..54908ed64 --- /dev/null +++ b/exercises/book-store/src/example/java/Bookstore.java @@ -0,0 +1,61 @@ +import java.awt.print.Printable; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class Bookstore{ + + private static int BOOK_PRICE = 8, MAX_GROUP_SIZE = 5; + private List books; + private static double[] DISCOUNT_TIERS = {0,5,10,20,25}; + + public Bookstore (List books){ + this.books = books; + } + + + + + public double calculateTotalCost(){ + return calculateTotalCost(this.books,0); + } + + 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 MAX_GROUP_SIZE){ + throw new Exception("Invalid group size : " + groupSize ); + } + return BOOK_PRICE * groupSize * (100 - DISCOUNT_TIERS[groupSize-1])/100; + } + + +} diff --git a/exercises/book-store/src/main/java/.keep b/exercises/book-store/src/main/java/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/exercises/book-store/src/test/java/.keep b/exercises/book-store/src/test/java/.keep new file mode 100644 index 000000000..e69de29bb 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..1593dc405 --- /dev/null +++ b/exercises/book-store/src/test/java/BookstoreTest.java @@ -0,0 +1,117 @@ +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(){ + List books = new ArrayList<>(Arrays.asList(1)); + Bookstore bookstore = new Bookstore(books); + assertEquals(8,bookstore.calculateTotalCost(),2); + } + + @Ignore + @Test + public void twoOfSameBook(){ + List books = new ArrayList<>(Arrays.asList(1,1)); + 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(){ + List books = new ArrayList<>(Arrays.asList(1,2)); + Bookstore bookstore = new Bookstore(books); + assertEquals(15.20,bookstore.calculateTotalCost(),2); + } + + @Ignore + @Test + public void threeDifferentBooks(){ + List books = new ArrayList<>(Arrays.asList(1,2,3)); + Bookstore bookstore = new Bookstore(books); + assertEquals(21.6,bookstore.calculateTotalCost(),2); + } + + @Ignore + @Test + public void fourDifferentBooks(){ + ArrayList books = new ArrayList<>(Arrays.asList(1,2,3,4)); + Bookstore bookstore = new Bookstore(books); + assertEquals(25.6,bookstore.calculateTotalCost(),2); + + } + + @Ignore + @Test + public void fiveDifferentBooks(){ + ArrayList books = new ArrayList<>(Arrays.asList(1,2,3,4,5)); + Bookstore bookstore = new Bookstore(books); + assertEquals(30,bookstore.calculateTotalCost(),2); + } + + @Ignore + @Test + public void twoGroupsOfFourIsCheaperThanGroupOfFivePlusGroupOfThree(){ + 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); + } + + @Ignore + @Test + public void groupOfFourPlusGroupOfTwoIsCheaperThanTwoGroupsOfThree(){ + List books = new ArrayList<>(Arrays.asList(1,1,2,2,3,4)); + 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(1,1,2,2,3,3,4,4,5)); + Bookstore bookstore = new Bookstore(books); + assertEquals(55.60,bookstore.calculateTotalCost(),2); + } + + @Ignore + @Test + public void twoCopiesOfEachBook(){ + 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); + } + + @Ignore + @Test + public void threeCopiesOfFirstBookAnd2EachOfRemaining(){ + 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); + } + + @Ignore + @Test + public void threeEachOFirst2BooksAnd2EachOfRemainingBooks(){ + 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); + } + +} + diff --git a/exercises/settings.gradle b/exercises/settings.gradle index f8cdb9962..ebc009cae 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 'change' include 'clock'