Skip to content

bookstore : add to track #310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Mar 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,15 @@
"difficulty": 1,
"topics": []
},
{
{
"slug": "isogram",
"difficulty": 1,
"topics": []
},
{
"slug": "book-store",
"difficulty": 1,
"topics": []
}
],
"deprecated": [
Expand Down
17 changes: 17 additions & 0 deletions exercises/book-store/build.gradle
Original file line number Diff line number Diff line change
@@ -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"]
}
}
Empty file.
61 changes: 61 additions & 0 deletions exercises/book-store/src/example/java/Bookstore.java
Original file line number Diff line number Diff line change
@@ -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<Integer> books;
private static double[] DISCOUNT_TIERS = {0,5,10,20,25};

public Bookstore (List<Integer> books){
this.books = books;
}




public double calculateTotalCost(){
return calculateTotalCost(this.books,0);
}

private double calculateTotalCost (List<Integer> books,double priceSoFar ){
double minPrice = Double.MAX_VALUE;


if(books.size() == 0){
return priceSoFar;
}

List<Integer> groups = (ArrayList<Integer>) books.stream().distinct().collect(Collectors.toList());


double price = 0;

for(int i = 0;i<groups.size();i++){
books.remove(groups.get(i));
}

try {
price = calculateTotalCost(books,priceSoFar + costPerGroup(groups.size()));
} catch (Exception e) {
e.printStackTrace();
}

minPrice = Math.min(minPrice, price);
return minPrice;


}

private double costPerGroup(int groupSize) throws Exception{
if (groupSize < 1 || groupSize > MAX_GROUP_SIZE){
throw new Exception("Invalid group size : " + groupSize );
}
return BOOK_PRICE * groupSize * (100 - DISCOUNT_TIERS[groupSize-1])/100;
}


}
Empty file.
Empty file.
117 changes: 117 additions & 0 deletions exercises/book-store/src/test/java/BookstoreTest.java
Original file line number Diff line number Diff line change
@@ -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<Integer> books = new ArrayList<>(Arrays.asList(1));
Bookstore bookstore = new Bookstore(books);
assertEquals(8,bookstore.calculateTotalCost(),2);
}

@Ignore
@Test
public void twoOfSameBook(){
List<Integer> books = new ArrayList<>(Arrays.asList(1,1));
Bookstore bookstore = new Bookstore(books);
assertEquals(16,bookstore.calculateTotalCost(),2);
}

@Ignore
@Test
public void emptyBasket(){
List<Integer> books = new ArrayList<>();
Bookstore bookstore = new Bookstore(books);
assertEquals(0,bookstore.calculateTotalCost(),2);
}

@Ignore
@Test
public void twoDifferentBooks(){
List<Integer> books = new ArrayList<>(Arrays.asList(1,2));
Bookstore bookstore = new Bookstore(books);
assertEquals(15.20,bookstore.calculateTotalCost(),2);
}

@Ignore
@Test
public void threeDifferentBooks(){
List<Integer> 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<Integer> 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<Integer> 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<Integer> 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<Integer> 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<Integer> 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<Integer> 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<Integer> 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<Integer> 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);
}

}

1 change: 1 addition & 0 deletions exercises/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down