-
-
Notifications
You must be signed in to change notification settings - Fork 686
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
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
9b71eb0
dibs : I will implement exercise bookstore
de35a3f
bookstore : add to track
c61580a
bookstore : add to track
Twisti 6d15792
Merge branch 'master' into bookstore-add
Twisti ab34101
Merge branch 'master' into bookstore-add
Twisti 7a9a4b4
Update config.json : added orc-numbers
Twisti 19ca6d0
Merge branch 'master' into bookstore-add
Twisti c8dd1ff
add missing test from the canonical spec and delete the remaning test
812e86e
using camelCase instead of snake_case for tests methods
9252786
adding @Ignore annotation
8426842
using List instead of ArrayList
f26f9e7
using instance methods instead of class method
28c89b0
delete useless stuff
1142bff
modify magic numbers
a551793
update CostPerGroup method
c823390
update class name and files name to Bookstore from Bookstore + using…
94e6ee0
pass the integers directly to the Arrays.asList() methods
18fd731
adding brackets
b1f4e7f
fix constant variable
6e385d1
Merge branch 'master' into bookstore-add
Twisti 84e3bb6
oMerge branch 'bookstore-add' of https://github.com/Twisti/xjava into…
a8303ad
using static for constants
Twisti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | ||
|
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added
static
to constantsBOOK_PRICE
,MAX_GROUP_SIZE
.Also, I deleted the
discountIncrement
because it's useless now.