From 5436e5d2c89798747bb05127ca9585ec2c90844b Mon Sep 17 00:00:00 2001 From: Mintas Date: Thu, 25 Feb 2016 00:42:02 +0300 Subject: [PATCH] cleaning --- src/Main.java | 2 +- src/excel/CSVCommodityParserImpl.java | 1 - src/service/JackSparrowHelperImpl.java | 20 +++++--------------- 3 files changed, 6 insertions(+), 17 deletions(-) diff --git a/src/Main.java b/src/Main.java index 664f88a..a89d176 100644 --- a/src/Main.java +++ b/src/Main.java @@ -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())); diff --git a/src/excel/CSVCommodityParserImpl.java b/src/excel/CSVCommodityParserImpl.java index 1552adc..0e46a1e 100644 --- a/src/excel/CSVCommodityParserImpl.java +++ b/src/excel/CSVCommodityParserImpl.java @@ -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; diff --git a/src/service/JackSparrowHelperImpl.java b/src/service/JackSparrowHelperImpl.java index 23cb866..ad30232 100644 --- a/src/service/JackSparrowHelperImpl.java +++ b/src/service/JackSparrowHelperImpl.java @@ -36,20 +36,19 @@ public Purchases helpJackSparrow(String pathToPrices, int numberOfGallons) { } private Purchase decidePurchase(int needGallons, List commodities) { - List collect = commodities.stream() + List 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; @@ -57,16 +56,7 @@ private Purchase bestForComdty(Commodity cmdty, int needGallons) { return new Purchase(cmdty.getSource(), canTakeByServings, cmdty.getAvgPrice()); } - //todo : implement export from csv private List 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()); }