Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
ostapzat committed Dec 20, 2024
1 parent 2d980d8 commit 32ece95
Show file tree
Hide file tree
Showing 21 changed files with 265 additions and 9 deletions.
9 changes: 0 additions & 9 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

51 changes: 51 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package core.basesyntax;

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.service.DataConverter;
import core.basesyntax.service.DataConverterImpl;
import core.basesyntax.service.FileReaderService;
import core.basesyntax.service.FileReaderServiceImpl;
import core.basesyntax.service.FileWriterService;
import core.basesyntax.service.FileWriterServiceImpl;
import core.basesyntax.service.ReportGenerator;
import core.basesyntax.service.ReportGeneratorImpl;
import core.basesyntax.service.ShopService;
import core.basesyntax.service.ShopServiceImpl;
import core.basesyntax.strategy.BalanceOperationHandler;
import core.basesyntax.strategy.OperationHandler;
import core.basesyntax.strategy.OperationStrategy;
import core.basesyntax.strategy.OperationStrategyImpl;
import core.basesyntax.strategy.PurchaseOperationHandler;
import core.basesyntax.strategy.ReturnOperationHandler;
import core.basesyntax.strategy.SupplyOperationHandler;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
public static void main(String[] arg) {
FileReaderService fileReader = new FileReaderServiceImpl();
List<String> inputReport = fileReader.readFile("reportToRead.csv");

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

Map<FruitTransaction.Operation, OperationHandler> operationHandlers = new HashMap<>();
operationHandlers.put(FruitTransaction.Operation.BALANCE, new BalanceOperationHandler());
operationHandlers.put(FruitTransaction.Operation.PURCHASE, new PurchaseOperationHandler());
operationHandlers.put(FruitTransaction.Operation.RETURN, new ReturnOperationHandler());
operationHandlers.put(FruitTransaction.Operation.SUPPLY, new SupplyOperationHandler());
OperationStrategy operationStrategy = new OperationStrategyImpl(operationHandlers);

// 4. Process the incoming transactions with applicable OperationHandler implementations
ShopService shopService = new ShopServiceImpl(operationStrategy);
shopService.process(transactions);
ReportGenerator reportGenerator = new ReportGeneratorImpl();
String resultingReport = reportGenerator.getReport();

// 6. Write the received report into the destination file
FileWriterService fileWriter = new FileWriterServiceImpl();
fileWriter.writeFile(resultingReport, "finalReport.csv");
}
}
9 changes: 9 additions & 0 deletions src/main/java/core/basesyntax/reportToRead.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type,fruit,quantity
b,banana,20
b,apple,100
s,banana,100
p,banana,13
r,apple,10
p,apple,20
p,banana,5
s,banana,50
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/service/DataConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core.basesyntax.service;

import core.basesyntax.model.FruitTransaction;
import java.util.List;

public interface DataConverter {
List<FruitTransaction> convertToTransaction(List<String> data);
}
20 changes: 20 additions & 0 deletions src/main/java/core/basesyntax/service/DataConverterImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package core.basesyntax.service;

import core.basesyntax.model.FruitTransaction;
import java.util.ArrayList;
import java.util.List;

public class DataConverterImpl implements DataConverter {
@Override
public List<FruitTransaction> convertToTransaction(List<String> data) {
List<FruitTransaction> transactions = new ArrayList<>();
for (String line: data) {
String[] elements = line.split(",");
FruitTransaction fruitTransaction =
new FruitTransaction(FruitTransaction.Operation.fromCode(elements[0]),
elements[1], Integer.parseInt(elements[2]));
transactions.add(fruitTransaction);
}
return transactions;
}
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/service/FileReaderService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.service;

import java.util.List;

public interface FileReaderService {
List<String> readFile(String fileName);
}
23 changes: 23 additions & 0 deletions src/main/java/core/basesyntax/service/FileReaderServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package core.basesyntax.service;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileReaderServiceImpl implements FileReaderService {
@Override
public List<String> readFile(String filepath) {
List<String> lines = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(filepath))) {
String line;
while ((line = br.readLine()) != null) {
lines.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return lines;
}
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/service/FileWriterService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax.service;

public interface FileWriterService {
void writeFile(String report, String fileName);
}
16 changes: 16 additions & 0 deletions src/main/java/core/basesyntax/service/FileWriterServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package core.basesyntax.service;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterServiceImpl implements FileWriterService {
@Override
public void writeFile(String report, String filePath) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
writer.write(report);
} catch (IOException e) {
throw new RuntimeException("Can't write a " + filePath, e);
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/service/ReportGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax.service;

public interface ReportGenerator {
String getReport();
}
13 changes: 13 additions & 0 deletions src/main/java/core/basesyntax/service/ReportGeneratorImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package core.basesyntax.service;

import core.basesyntax.storage.Storage;

public class ReportGeneratorImpl implements ReportGenerator {
@Override
public String getReport() {
StringBuilder report = new StringBuilder("fruit,quantity\n");
Storage.fruits.forEach((fruit, quantity) -> report.append(fruit)
.append(",").append(quantity).append("\n"));
return report.toString();
}
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/service/ShopService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core.basesyntax.service;

import core.basesyntax.model.FruitTransaction;
import java.util.List;

public interface ShopService {
void process(List<FruitTransaction> transactions);
}
22 changes: 22 additions & 0 deletions src/main/java/core/basesyntax/service/ShopServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package core.basesyntax.service;

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.strategy.OperationStrategy;
import java.util.List;

public class ShopServiceImpl implements ShopService {
private OperationStrategy operationStrategy;

public ShopServiceImpl(OperationStrategy operationStrategy) {
this.operationStrategy = operationStrategy;
}

@Override
public void process(List<FruitTransaction> transactions) {
for (FruitTransaction fruitTransaction: transactions) {
operationStrategy.getHandler(
fruitTransaction.getOperation()).apply(fruitTransaction.getFruit(),
fruitTransaction.getQuantity());
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/storage/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core.basesyntax.storage;

import java.util.HashMap;
import java.util.Map;

public class Storage {
public static final Map<String, Integer> fruits = new HashMap<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package core.basesyntax.strategy;

import core.basesyntax.storage.Storage;

public class BalanceOperationHandler implements OperationHandler {
@Override
public void apply(String fruit, int quantity) {
Storage.fruits.put(fruit, quantity);
}
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/strategy/OperationHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax.strategy;

public interface OperationHandler {
void apply(String fruit, int quantity);
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/strategy/OperationStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.strategy;

import core.basesyntax.model.FruitTransaction;

public interface OperationStrategy {
OperationHandler getHandler(FruitTransaction.Operation operation);
}
18 changes: 18 additions & 0 deletions src/main/java/core/basesyntax/strategy/OperationStrategyImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package core.basesyntax.strategy;

import core.basesyntax.model.FruitTransaction;
import java.util.Map;

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

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

@Override
public OperationHandler getHandler(FruitTransaction.Operation operation) {
return operationHandlers.get(operation);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package core.basesyntax.strategy;

import core.basesyntax.storage.Storage;

public class PurchaseOperationHandler implements OperationHandler {
@Override
public void apply(String fruit, int quantity) {
Storage.fruits.put(fruit, Storage.fruits.get(fruit) + quantity);
}
}
10 changes: 10 additions & 0 deletions src/main/java/core/basesyntax/strategy/ReturnOperationHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package core.basesyntax.strategy;

import core.basesyntax.storage.Storage;

public class ReturnOperationHandler implements OperationHandler {
@Override
public void apply(String fruit, int quantity) {
Storage.fruits.put(fruit, Storage.fruits.get(fruit) + quantity);
}
}
10 changes: 10 additions & 0 deletions src/main/java/core/basesyntax/strategy/SupplyOperationHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package core.basesyntax.strategy;

import core.basesyntax.storage.Storage;

public class SupplyOperationHandler implements OperationHandler {
@Override
public void apply(String fruit, int quantity) {
Storage.fruits.put(fruit, Storage.fruits.get(fruit) + quantity);
}
}

0 comments on commit 32ece95

Please sign in to comment.