Skip to content

Commit

Permalink
solved task-3
Browse files Browse the repository at this point in the history
  • Loading branch information
aniliashenko committed Dec 7, 2024
1 parent 8b49049 commit 88dd8c1
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 24 deletions.
3 changes: 2 additions & 1 deletion src/main/java/core/basesyntax/FruitTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public enum Operation {
BALANCE("b"),
SUPPLY("s"),
PURCHASE("p"),
RETURN("r");
RETURN("r"),
NONE("n");

private String code;

Expand Down
27 changes: 16 additions & 11 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,44 @@
import core.basesyntax.dao.FileReaderImpl;
import core.basesyntax.dao.FileWriter;
import core.basesyntax.dao.FileWriterImpl;
import core.basesyntax.service.*;
import core.basesyntax.service.operations.*;
import core.basesyntax.service.DataConverter;
import core.basesyntax.service.DataConverterImpl;
import core.basesyntax.service.OperationStrategy;
import core.basesyntax.service.OperationStrategyImpl;
import core.basesyntax.service.ShopService;
import core.basesyntax.service.ShopServiceImpl;
import core.basesyntax.service.ReportGenerator;
import core.basesyntax.service.ReportGeneratorImpl;
import core.basesyntax.service.operations.BalanceOperation;
import core.basesyntax.service.operations.OperationHandler;
import core.basesyntax.service.operations.PurchaseOperation;
import core.basesyntax.service.operations.ReturnOperation;
import core.basesyntax.service.operations.SupplyOperation;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
public static void main(String[] args) {
// 1. Read the data from the input CSV file
FileReader fileReader = new FileReaderImpl();
List<String> inputReport = fileReader.read("reportToRead.txt");

// 2. Convert the incoming data into FruitTransactions list
DataConverter dataConverter = new DataConverterImpl();
List<FruitTransaction> transactions = dataConverter.convertToTransaction(inputReport);

// 3. Create and feel the map with all OperationHandler implementations
Map<FruitTransaction.Operation, OperationHandler> operationHandlers = new HashMap<>();
operationHandlers.put(FruitTransaction.Operation.BALANCE, new BalanceOperation());
operationHandlers.put(FruitTransaction.Operation.PURCHASE, new PurchaseOperation());
operationHandlers.put(FruitTransaction.Operation.RETURN, new ReturnOperation());
operationHandlers.put(FruitTransaction.Operation.SUPPLY, new SupplyOperation());
OperationStrategy operationStrategy = new OperationStrategyImpl(operationHandlers);

// 4. Process the incoming transactions with applicable OperationHandler implementations
DataConverter dataConverter = new DataConverterImpl();
List<FruitTransaction> transactions = dataConverter.convertToTransaction(inputReport);

ShopService shopService = new ShopServiceImpl(operationStrategy);
shopService.process(transactions);

// 5.Generate report based on the current Storage state
ReportGenerator reportGenerator = new ReportGeneratorImpl();
String resultingReport = reportGenerator.getReport(transactions);

// 6. Write the received report into the destination file
FileWriter fileWriter = new FileWriterImpl();
fileWriter.write(resultingReport, "finalReport.csv");
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/core/basesyntax/service/DataConverter.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package core.basesyntax.service;

import core.basesyntax.FruitTransaction;

import java.util.List;

public interface DataConverter {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/core/basesyntax/service/DataConverterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import java.util.List;

public class DataConverterImpl implements DataConverter {
private final static int TYPE = 0;
private final static int FRUIT = 1;
private final static int QUANTITY = 2;
private static final int TYPE = 0;
private static final int FRUIT = 1;
private static final int QUANTITY = 2;

@Override
public List<FruitTransaction> convertToTransaction(List<String> input) {
Expand All @@ -23,6 +23,7 @@ public List<FruitTransaction> convertToTransaction(List<String> input) {
case "s" -> fruitTransaction.setOperation(FruitTransaction.Operation.SUPPLY);
case "p" -> fruitTransaction.setOperation(FruitTransaction.Operation.PURCHASE);
case "r" -> fruitTransaction.setOperation(FruitTransaction.Operation.RETURN);
default -> fruitTransaction.setOperation(FruitTransaction.Operation.NONE);
}
fruitTransaction.setFruit(transaction[FRUIT]);
fruitTransaction.setQuantity(Integer.parseInt(transaction[QUANTITY]));
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/core/basesyntax/service/OperationStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
import core.basesyntax.FruitTransaction;

public interface OperationStrategy {
void makeOperation(FruitTransaction.Operation operation, FruitTransaction fruitTransaction, Integer quantity);
void makeOperation(FruitTransaction.Operation operation,
FruitTransaction fruitTransaction,
Integer quantity);
}
11 changes: 7 additions & 4 deletions src/main/java/core/basesyntax/service/OperationStrategyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@

import core.basesyntax.FruitTransaction;
import core.basesyntax.service.operations.OperationHandler;

import java.util.Map;

public class OperationStrategyImpl implements OperationStrategy {
private Map<FruitTransaction.Operation, OperationHandler> operationHandlers;

public OperationStrategyImpl(Map<FruitTransaction.Operation, OperationHandler> operationHandlers) {
public OperationStrategyImpl(Map<FruitTransaction.Operation,
OperationHandler> operationHandlers) {
this.operationHandlers = operationHandlers;
}

@Override
public void makeOperation(FruitTransaction.Operation operation, FruitTransaction fruitTransaction, Integer quantity) {
public void makeOperation(FruitTransaction.Operation operation,
FruitTransaction fruitTransaction,
Integer quantity) {
operationHandlers.get(operation).doOperation(fruitTransaction, quantity);
}

public Map<FruitTransaction.Operation, OperationHandler> getOperationHandlers() {
return operationHandlers;
}

public void setOperationHandlers(Map<FruitTransaction.Operation, OperationHandler> operationHandlers) {
public void setOperationHandlers(Map<FruitTransaction.Operation,
OperationHandler> operationHandlers) {
this.operationHandlers = operationHandlers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.stream.Collectors;

public class ReportGeneratorImpl implements ReportGenerator {
private final static String DEFAULT_FIRST_LINE = "fruit,quantity\n";
private static final String DEFAULT_FIRST_LINE = "fruit,quantity\n";

@Override
public String getReport(List<FruitTransaction> fruitTransactionList) {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/core/basesyntax/service/ShopService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package core.basesyntax.service;

import core.basesyntax.FruitTransaction;

import java.util.List;

public interface ShopService {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/core/basesyntax/service/ShopServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public void process(List<FruitTransaction> transactions) {
if (fruitTransaction.getOperation() == FruitTransaction.Operation.BALANCE) {
calculatedTransactionsMap.put(fruitTransaction.getFruit(), fruitTransaction);
} else {
FruitTransaction calculatedTransaction = calculatedTransactionsMap.get(fruitTransaction.getFruit());
FruitTransaction calculatedTransaction = calculatedTransactionsMap
.get(fruitTransaction.getFruit());
if (calculatedTransaction != null) {
operationStrategy.makeOperation(
fruitTransaction.getOperation(),
Expand Down

0 comments on commit 88dd8c1

Please sign in to comment.