Skip to content

Commit

Permalink
first solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Rzarcik97 committed Dec 17, 2024
1 parent 2d980d8 commit fe89eba
Show file tree
Hide file tree
Showing 25 changed files with 407 additions and 9 deletions.
9 changes: 9 additions & 0 deletions 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
9 changes: 0 additions & 9 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

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

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.service.DataConverter;
import core.basesyntax.service.FileReader;
import core.basesyntax.service.FileWriter;
import core.basesyntax.service.ReportGenerator;
import core.basesyntax.service.ShopService;
import core.basesyntax.service.impl.DataConverterImpl;
import core.basesyntax.service.impl.FileReaderImpl;
import core.basesyntax.service.impl.FileWriterImpl;
import core.basesyntax.service.impl.ReportGeneratorImpl;
import core.basesyntax.service.impl.ShopServiceImpl;
import core.basesyntax.strategy.BalanceOperation;
import core.basesyntax.strategy.OperationHandler;
import core.basesyntax.strategy.OperationStrategy;
import core.basesyntax.strategy.OperationStrategyImpl;
import core.basesyntax.strategy.PurchaseOperation;
import core.basesyntax.strategy.ReturnOperation;
import core.basesyntax.strategy.SupplyOperation;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

// 2. Convert the incoming data into FruitTransactions list
DataConverter dataConverter = new DataConverterImpl();

// 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
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();

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

import java.util.Map;

public interface FruitDao {

void add(String fruit, int quantity);

void subtract(String fruit, int quantity);

Map<String,Integer> getStorageQuantity();
}
28 changes: 28 additions & 0 deletions src/main/java/core/basesyntax/db/FruitDaoImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package core.basesyntax.db;

import core.basesyntax.db.storage.Storage;
import java.util.Map;

public class FruitDaoImpl implements FruitDao {
@Override
public void add(String fruit, int quantity) {
if (!Storage.fruits.containsKey(fruit)) {
Storage.fruits.put(fruit,quantity);
return;
}
Storage.fruits.compute(fruit, (k, oldQuantity) -> oldQuantity + quantity);
}

@Override
public void subtract(String fruit, int quantity) {
if (!Storage.fruits.containsKey(fruit)) {
throw new RuntimeException("There is not enough " + fruit + " in Storage");
}
Storage.fruits.compute(fruit, (k, oldQuantity) -> oldQuantity - quantity);
}

@Override
public Map<String, Integer> getStorageQuantity() {
return Storage.fruits;
}
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/db/storage/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core.basesyntax.db.storage;

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

public class Storage {
public static final Map<String,Integer> fruits = new HashMap<>();
}
27 changes: 27 additions & 0 deletions src/main/java/core/basesyntax/model/Fruit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package core.basesyntax.model;

public class Fruit {
private String fruit;
private int quantity;

public Fruit(String fruit, int quantity) {
this.fruit = fruit;
this.quantity = quantity;
}

public String getFruit() {
return fruit;
}

public void setFruit(String fruit) {
this.fruit = fruit;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
43 changes: 43 additions & 0 deletions src/main/java/core/basesyntax/model/FruitTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package core.basesyntax.model;

public class FruitTransaction extends Fruit {
private Operation operation;

public FruitTransaction(String operation, String fruit, int quantity) {
super(fruit,quantity);
switch (operation) {
case "b" -> this.operation = Operation.BALANCE;
case "s" -> this.operation = Operation.SUPPLY;
case "p" -> this.operation = Operation.PURCHASE;
case "r" -> this.operation = Operation.RETURN;
default -> throw new RuntimeException("Can't find operation");
}
}

public Operation getOperation() {
return operation;
}

public void setOperation(Operation operation) {
this.operation = operation;
}

// getters, setters, ...

public enum Operation {
BALANCE("b"),
SUPPLY("s"),
PURCHASE("p"),
RETURN("r");

private final String code;

Operation(String code) {
this.code = code;
}

public String getCode() {
return code;
}
}
}
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> listFromFile);
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/service/FileReader.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 FileReader {
List<String> read(String path);
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/service/FileWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax.service;

public interface FileWriter {
void write(String report, String fileName);
}
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();
}
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> fruits);
}
26 changes: 26 additions & 0 deletions src/main/java/core/basesyntax/service/impl/DataConverterImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package core.basesyntax.service.impl;

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

public class DataConverterImpl implements DataConverter {

private static final int OPERATION_TYPE = 0;
private static final int FRUIT_NAME = 1;
private static final int QUANTITY_OF_FRUITS = 2;

@Override
public List<FruitTransaction> convertToTransaction(List<String> listFromFile) {
return listFromFile.stream()
.filter(s -> s.startsWith(FruitTransaction.Operation.BALANCE.getCode())
|| s.startsWith(FruitTransaction.Operation.SUPPLY.getCode())
|| s.startsWith(FruitTransaction.Operation.PURCHASE.getCode())
|| s.startsWith(FruitTransaction.Operation.RETURN.getCode()))
.map(s -> s.split(","))
.map(s -> new FruitTransaction(s[OPERATION_TYPE],
s[FRUIT_NAME],
Integer.parseInt(s[QUANTITY_OF_FRUITS])))
.toList();
}
}
23 changes: 23 additions & 0 deletions src/main/java/core/basesyntax/service/impl/FileReaderImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package core.basesyntax.service.impl;

import core.basesyntax.service.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

public class FileReaderImpl implements FileReader {
private final Path filePath;

public FileReaderImpl() {
this.filePath = Path.of("reportToRead.csv");
}

public List<String> read(String filename) {
try {
return Files.readAllLines(filePath);
} catch (IOException e) {
throw new RuntimeException("Can't read from file", e);
}
}
}
24 changes: 24 additions & 0 deletions src/main/java/core/basesyntax/service/impl/FileWriterImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package core.basesyntax.service.impl;

import core.basesyntax.service.FileWriter;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;

public class FileWriterImpl implements FileWriter {

private final File file;

public FileWriterImpl(String fileName) {
this.file = new File(fileName);
}

@Override
public void write(String report, String fileName) {
try (BufferedWriter writer = new BufferedWriter(new java.io.FileWriter(file))) {
writer.write(report);
} catch (IOException e) {
throw new RuntimeException("can't save report", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package core.basesyntax.service.impl;

import core.basesyntax.db.FruitDao;
import core.basesyntax.db.FruitDaoImpl;
import core.basesyntax.service.ReportGenerator;
import java.util.stream.Collectors;

public class ReportGeneratorImpl implements ReportGenerator {
@Override
public String getReport() {
FruitDao fruitDao = new FruitDaoImpl();
String listFromStorage = fruitDao.getStorageQuantity().keySet().stream()
.map(k -> k + "," + fruitDao.getStorageQuantity().get(k) + "\n")
.collect(Collectors.joining());
return "fruit,quantity\n" + listFromStorage;
}
}
22 changes: 22 additions & 0 deletions src/main/java/core/basesyntax/service/impl/ShopServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package core.basesyntax.service.impl;

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

public class ShopServiceImpl implements ShopService {

private final OperationStrategy operationStrategy;

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

@Override
public void process(List<FruitTransaction> fruits) {
fruits.forEach((f -> operationStrategy
.getOperationStrategy(f.getOperation())
.getOperation(f.getFruit(), f.getQuantity())));
}
}
12 changes: 12 additions & 0 deletions src/main/java/core/basesyntax/strategy/BalanceOperation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package core.basesyntax.strategy;

import core.basesyntax.db.FruitDao;
import core.basesyntax.db.FruitDaoImpl;

public class BalanceOperation implements OperationHandler {
@Override
public void getOperation(String fruit, int quantity) {
FruitDao fruitDao = new FruitDaoImpl();
fruitDao.add(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 getOperation(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 getOperationStrategy(FruitTransaction.Operation operation);
}
Loading

0 comments on commit fe89eba

Please sign in to comment.