diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java index 54e117c759..16c6941fd1 100644 --- a/src/main/java/core/basesyntax/Main.java +++ b/src/main/java/core/basesyntax/Main.java @@ -36,7 +36,7 @@ Operation.SUPPLY, new SupplyAction() public static void main(String[] arg) { CsvFileReader fileReader = new CsvFileReaderImpl(); - String[] textFromDatabase = fileReader.read(FILE_PATH_FOR_DATABASE); + List textFromDatabase = fileReader.read(FILE_PATH_FOR_DATABASE); FruitTransactionParser fruitTransactionParser = new FruitTransactionParser(); List allTransactions = fruitTransactionParser @@ -44,7 +44,7 @@ public static void main(String[] arg) { ActionStrategy actionStrategy = new ActionStrategyImpl(actionHandlerMap); ShopService shopService = new ShopServiceImpl(actionStrategy); - shopService.generate(allTransactions); + shopService.process(allTransactions); CsvReportGenerator reportWriter = new CsvReportGeneratorImpl(); String reportInfo = reportWriter.generateReport(FILE_PATH_FOR_FINALREPORT); diff --git a/src/main/java/core/basesyntax/dao/CsvFileReader.java b/src/main/java/core/basesyntax/dao/CsvFileReader.java index 06c479b375..770bd57878 100644 --- a/src/main/java/core/basesyntax/dao/CsvFileReader.java +++ b/src/main/java/core/basesyntax/dao/CsvFileReader.java @@ -1,5 +1,7 @@ package core.basesyntax.dao; +import java.util.List; + public interface CsvFileReader { - String[] read(String nameOfFile); + List read(String nameOfFile); } diff --git a/src/main/java/core/basesyntax/dao/CsvFileReaderImpl.java b/src/main/java/core/basesyntax/dao/CsvFileReaderImpl.java index 43c3fc412d..045bf48401 100644 --- a/src/main/java/core/basesyntax/dao/CsvFileReaderImpl.java +++ b/src/main/java/core/basesyntax/dao/CsvFileReaderImpl.java @@ -1,7 +1,5 @@ package core.basesyntax.dao; -import java.io.BufferedReader; -import java.io.FileReader; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; @@ -10,13 +8,11 @@ public class CsvFileReaderImpl implements CsvFileReader { @Override - public String[] read(String nameOfFile) { - List textInFile = null; - try (BufferedReader reader = new BufferedReader(new FileReader(nameOfFile))) { - textInFile = Files.readAllLines(Path.of(nameOfFile)); - return textInFile.toArray(String[]::new); + public List read(String fileName) { + try { + return Files.readAllLines(Path.of(fileName)); } catch (IOException e) { - throw new RuntimeException("Can't read the data from the file " + nameOfFile, e); + throw new RuntimeException("Can't read the data from the file " + fileName, e); } } } diff --git a/src/main/java/core/basesyntax/dao/CsvFileWriter.java b/src/main/java/core/basesyntax/dao/CsvFileWriter.java deleted file mode 100644 index f81850a950..0000000000 --- a/src/main/java/core/basesyntax/dao/CsvFileWriter.java +++ /dev/null @@ -1,5 +0,0 @@ -package core.basesyntax.dao; - -public interface CsvFileWriter { - void write(String info, String fileName); -} diff --git a/src/main/java/core/basesyntax/dao/CsvFileWriterImpl.java b/src/main/java/core/basesyntax/dao/CsvFileWriterImpl.java deleted file mode 100644 index 189cedd5ae..0000000000 --- a/src/main/java/core/basesyntax/dao/CsvFileWriterImpl.java +++ /dev/null @@ -1,17 +0,0 @@ -package core.basesyntax.dao; - -import java.io.BufferedWriter; -import java.io.FileWriter; -import java.io.IOException; - -public class CsvFileWriterImpl implements CsvFileWriter { - @Override - public void write(String report, String fileName) { - try (BufferedWriter writer = new BufferedWriter( - new FileWriter(fileName))) { - writer.write(report); - } catch (IOException e) { - throw new RuntimeException("File: " + fileName + " was not created", e); - } - } -} diff --git a/src/main/java/core/basesyntax/dao/CsvReportGeneratorImpl.java b/src/main/java/core/basesyntax/dao/CsvReportGeneratorImpl.java index 4f2a0c22cb..21500e7ce5 100644 --- a/src/main/java/core/basesyntax/dao/CsvReportGeneratorImpl.java +++ b/src/main/java/core/basesyntax/dao/CsvReportGeneratorImpl.java @@ -5,7 +5,6 @@ import java.util.Map; public class CsvReportGeneratorImpl implements CsvReportGenerator { - private static final String HEADER = "fruit,quantity" + System.lineSeparator(); private static final String COMMA = ","; @@ -15,9 +14,9 @@ public String generateReport(String fileName) { for (Map.Entry fruitNameQuantityEntry : storageOfFruits.entrySet()) { String fruitName = fruitNameQuantityEntry.getKey(); Integer quantity = fruitNameQuantityEntry.getValue(); - reportDataBuilder.append(fruitName).append(COMMA).append(quantity); + reportDataBuilder.append(fruitName).append(COMMA).append(quantity) + .append(System.lineSeparator()); } - String reportData = reportDataBuilder.toString(); - return reportData; + return reportDataBuilder.toString(); } } diff --git a/src/main/java/core/basesyntax/model/FruitTransactionParser.java b/src/main/java/core/basesyntax/model/FruitTransactionParser.java index 0e376f2b8a..cd365dd934 100644 --- a/src/main/java/core/basesyntax/model/FruitTransactionParser.java +++ b/src/main/java/core/basesyntax/model/FruitTransactionParser.java @@ -9,11 +9,11 @@ public class FruitTransactionParser { private static final int QUANTITY_INDEX = 2; private static final int OFFSET = 1; - public List parseTransaction(String[] transactions) { + public List parseTransaction(List transactions) { List fruitTransactions = new ArrayList<>(); - for (int i = OFFSET; i < transactions.length; i++) { - String transaction = transactions[i]; + for (int i = OFFSET; i < transactions.size(); i++) { + String transaction = transactions.get(i); String[] parts = transaction.split(","); Operation operation = Operation.getOperation(parts[OPERATION_INDEX]); diff --git a/src/main/java/core/basesyntax/service/ShopService.java b/src/main/java/core/basesyntax/service/ShopService.java index ac4add0cb4..f964d7a652 100644 --- a/src/main/java/core/basesyntax/service/ShopService.java +++ b/src/main/java/core/basesyntax/service/ShopService.java @@ -4,5 +4,5 @@ import java.util.List; public interface ShopService { - void generate(List transactions); + void process(List transactions); } diff --git a/src/main/java/core/basesyntax/service/ShopServiceImpl.java b/src/main/java/core/basesyntax/service/ShopServiceImpl.java index e40a531eb3..2766d4ef82 100644 --- a/src/main/java/core/basesyntax/service/ShopServiceImpl.java +++ b/src/main/java/core/basesyntax/service/ShopServiceImpl.java @@ -11,13 +11,13 @@ public ShopServiceImpl(ActionStrategy strategy) { } @Override - public void generate(List transactions) { + public void process(List transactions) { for (FruitTransaction transaction : transactions) { - generateByRow(transaction); + processTransaction(transaction); } } - void generateByRow(FruitTransaction transaction) { + private void processTransaction(FruitTransaction transaction) { strategy.get(transaction.getOperation()) .count(transaction.getFruit(), transaction.getQuantity()); } diff --git a/src/main/java/core/basesyntax/service/action/PurchaseAction.java b/src/main/java/core/basesyntax/service/action/PurchaseAction.java index 00d69f010a..ac06fc2c2e 100644 --- a/src/main/java/core/basesyntax/service/action/PurchaseAction.java +++ b/src/main/java/core/basesyntax/service/action/PurchaseAction.java @@ -8,6 +8,7 @@ public void count(String fruit, int amount) { checkAmount(amount); int newBalance = storageOfFruits.get(fruit) - amount; + checkAmount(newBalance); storageOfFruits.put(fruit, newBalance); } } diff --git a/src/main/resources/report.csv b/src/main/resources/report.csv index 9ae820d6c8..6158e4419c 100644 --- a/src/main/resources/report.csv +++ b/src/main/resources/report.csv @@ -1,3 +1,3 @@ fruit,quantity -banana, 152 -apple, 90 \ No newline at end of file +banana,152 +apple,90