Skip to content
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

bookstore : add to track #310

Merged
merged 22 commits into from
Mar 17, 2017
Merged
Show file tree
Hide file tree
Changes from 17 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
5 changes: 5 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@
"slug": "ocr-numbers",
"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.
64 changes: 64 additions & 0 deletions exercises/book-store/src/example/java/Bookstore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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 int bookPrice, maxGroupSize;
private double discountIncrement;
private List<Integer> books;
private static double[] discount_tiers = {0,5,10,20,25};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally a constant would be ALL_CAPITALS like this. Most editors will recognize them as such and make them easier to spot in the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added static to constants BOOK_PRICE, MAX_GROUP_SIZE.
Also, I deleted the discountIncrement because it's useless now.


public Bookstore (List<Integer> books){
this.books = books;
this.bookPrice = 8;
this.maxGroupSize = 5;
this.discountIncrement = 0.05;
}




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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curly braces here please.


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 > maxGroupSize){
throw new Exception("Invalid group size : " + groupSize );
}
return bookPrice * 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