Skip to content

Commit

Permalink
cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
Mintas committed Feb 24, 2016
1 parent cf9a71f commit 5436e5d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public class Main {
public static void main(String[] args) {
JackSparrowHelperImpl jackSparrowHelper = new JackSparrowHelperImpl(new CSVCommodityParserImpl(";"));
Purchases abr = jackSparrowHelper.helpJackSparrow("C:\\Users\\SBT-Kovalev-DA\\Downloads\\pirates\\sources.csv", 300);
Purchases abr = jackSparrowHelper.helpJackSparrow("C:\\Users\\SBT-Kovalev-DA\\Downloads\\pirates\\sources.csv", 500);

System.out.println(abr.calculateAveragePrice());
abr.getPurchases().forEach(p -> System.out.println(p.getTotalPrice()));
Expand Down
1 change: 0 additions & 1 deletion src/excel/CSVCommodityParserImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import model.Commodity;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
Expand Down
20 changes: 5 additions & 15 deletions src/service/JackSparrowHelperImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,27 @@ public Purchases helpJackSparrow(String pathToPrices, int numberOfGallons) {
}

private Purchase decidePurchase(int needGallons, List<Commodity> commodities) {
List<Commodity> collect = commodities.stream()
List<Commodity> cmdtsLeft = commodities.stream()
.filter(c -> c.getAmountLeft() != 0)
.collect(toList());
for (Commodity commodity : collect) {
Purchase purchase = bestForComdty(commodity, needGallons);
for (Commodity commodity : cmdtsLeft) {
Purchase purchase = bestForCommodity(commodity, needGallons);
if (purchase != null) return purchase;
}
return null;
}


private Purchase bestForComdty(Commodity cmdty, int needGallons) {
int amountLeft = cmdty.getAmountLeft();
int canTake = min(amountLeft, needGallons);
private Purchase bestForCommodity(Commodity cmdty, int needGallons) {
int canTake = min(cmdty.getAmountLeft(), needGallons);
int canTakeByServings = (canTake / cmdty.getServingSize()) * cmdty.getServingSize();
if (canTake < cmdty.getMinSize() || canTakeByServings == 0) return null;

cmdty.decreaseAmount(canTakeByServings);
return new Purchase(cmdty.getSource(), canTakeByServings, cmdty.getAvgPrice());
}

//todo : implement export from csv
private List<Commodity> getCommodities(String pathToPrices) {
/*Arrays.asList(
new Commodity("M", 50, 50.0, 1, 1),
new Commodity("S", 60, 52.0, 1, 1),
new Commodity("B", 200, 55.0, 1, 1),
new Commodity("D", 100, 51.0, 100, 100),
new Commodity("H", 300, 50.5, 200, 100),
new Commodity("K", 200, 54.0, 200, 200)
);*/
return parser.parseFile(pathToPrices)
.stream().sorted(comparing(Commodity::getAvgPrice)).collect(toList());
}
Expand Down

0 comments on commit 5436e5d

Please sign in to comment.