Skip to content

Commit

Permalink
final
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolya777777777777 committed Nov 21, 2024
1 parent 7aa87a4 commit 3210831
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ Operation.SUPPLY, new SupplyAction()
public static void main(String[] arg) {

CsvFileReader fileReader = new CsvFileReaderImpl();
String[] textFromDatabase = fileReader.read(FILE_PATH_FOR_DATABASE);
List<String> textFromDatabase = fileReader.read(FILE_PATH_FOR_DATABASE);

FruitTransactionParser fruitTransactionParser = new FruitTransactionParser();
List<FruitTransaction> allTransactions = fruitTransactionParser
.parseTransaction(textFromDatabase);

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);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/core/basesyntax/dao/CsvFileReader.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package core.basesyntax.dao;

import java.util.List;

public interface CsvFileReader {
String[] read(String nameOfFile);
List<String> read(String nameOfFile);
}
12 changes: 4 additions & 8 deletions src/main/java/core/basesyntax/dao/CsvFileReaderImpl.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,13 +8,11 @@
public class CsvFileReaderImpl implements CsvFileReader {

@Override
public String[] read(String nameOfFile) {
List<String> textInFile = null;
try (BufferedReader reader = new BufferedReader(new FileReader(nameOfFile))) {
textInFile = Files.readAllLines(Path.of(nameOfFile));
return textInFile.toArray(String[]::new);
public List<String> 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);
}
}
}
5 changes: 0 additions & 5 deletions src/main/java/core/basesyntax/dao/CsvFileWriter.java

This file was deleted.

17 changes: 0 additions & 17 deletions src/main/java/core/basesyntax/dao/CsvFileWriterImpl.java

This file was deleted.

7 changes: 3 additions & 4 deletions src/main/java/core/basesyntax/dao/CsvReportGeneratorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ",";

Expand All @@ -15,9 +14,9 @@ public String generateReport(String fileName) {
for (Map.Entry<String, Integer> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ public class FruitTransactionParser {
private static final int QUANTITY_INDEX = 2;
private static final int OFFSET = 1;

public List<FruitTransaction> parseTransaction(String[] transactions) {
public List<FruitTransaction> parseTransaction(List<String> transactions) {
List<FruitTransaction> 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]);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/core/basesyntax/service/ShopService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
import java.util.List;

public interface ShopService {
void generate(List<FruitTransaction> transactions);
void process(List<FruitTransaction> transactions);
}
6 changes: 3 additions & 3 deletions src/main/java/core/basesyntax/service/ShopServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ public ShopServiceImpl(ActionStrategy strategy) {
}

@Override
public void generate(List<FruitTransaction> transactions) {
public void process(List<FruitTransaction> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions src/main/resources/report.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fruit,quantity
banana, 152
apple, 90
banana,152
apple,90

0 comments on commit 3210831

Please sign in to comment.