Skip to content

Commit

Permalink
implement tasks for fruit shop
Browse files Browse the repository at this point in the history
  • Loading branch information
skotsurskiy committed Nov 2, 2024
1 parent 2d980d8 commit b08583e
Show file tree
Hide file tree
Showing 25 changed files with 453 additions and 9 deletions.
9 changes: 9 additions & 0 deletions src/DataBase.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.

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

import core.basesyntax.dao.StorageDao;
import core.basesyntax.dao.StorageDaoImpl;
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.service.DataConverterService;
import core.basesyntax.service.DataConverterServiceImpl;
import core.basesyntax.service.OperationStrategy;
import core.basesyntax.service.OperationStrategyImpl;
import core.basesyntax.service.ReportService;
import core.basesyntax.service.ReportServiceImpl;
import core.basesyntax.service.ShopService;
import core.basesyntax.service.ShopServiceImpl;
import core.basesyntax.service.file.FileReaderService;
import core.basesyntax.service.file.FileReaderServiceImpl;
import core.basesyntax.service.file.FileWriterService;
import core.basesyntax.service.file.FileWriterServiceImpl;
import core.basesyntax.service.operation.BalanceOperationHandler;
import core.basesyntax.service.operation.OperationHandler;
import core.basesyntax.service.operation.PurchaseOperationHandler;
import core.basesyntax.service.operation.ReturnOperationHandler;
import core.basesyntax.service.operation.SupplyOperationHandler;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
public static void main(String[] args) {
String dataBasePath = "src/DataBase.csv";

FileReaderService readerService = new FileReaderServiceImpl();
List<String> read = readerService.read(dataBasePath);

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

DataConverterService dataConverterService = new DataConverterServiceImpl();
List<FruitTransaction> convert = dataConverterService.convert(read);

ShopService service = new ShopServiceImpl(operationStrategy);
service.process(convert);

ReportService reportService = new ReportServiceImpl();
StorageDao storageDao = new StorageDaoImpl();
String report = reportService.getReport(storageDao.getAll());

FileWriterService fileWriterService = new FileWriterServiceImpl();
fileWriterService.write(report, "src/result");
}
}
11 changes: 11 additions & 0 deletions src/main/java/core/basesyntax/dao/StorageDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package core.basesyntax.dao;

import java.util.Map;

public interface StorageDao {
Map<String, Integer> save(String fruit, int quantity);

Map<String, Integer> getAll();

int getQuantity(String key);
}
28 changes: 28 additions & 0 deletions src/main/java/core/basesyntax/dao/StorageDaoImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package core.basesyntax.dao;

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

public class StorageDaoImpl implements StorageDao {
private Storage storage;

public StorageDaoImpl() {
storage = new Storage();
}

@Override
public Map<String, Integer> save(String fruit, int quantity) {
storage.getFruitStorage().put(fruit, quantity);
return storage.getFruitStorage();
}

@Override
public Map<String, Integer> getAll() {
return storage.getFruitStorage();
}

@Override
public int getQuantity(String key) {
return storage.getFruitStorage().get(key);
}
}
72 changes: 72 additions & 0 deletions src/main/java/core/basesyntax/model/FruitTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package core.basesyntax.model;

public class FruitTransaction {
private Operation type;
private String fruit;
private int quantity;

public FruitTransaction(Operation type, String fruit, int quantity) {
this.type = type;
this.fruit = fruit;
this.quantity = quantity;
}

public Operation getType() {
return type;
}

public void setType(Operation type) {
this.type = type;
}

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;
}

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

private String code;

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

public String getCode() {
return code;
}

public static boolean startWithOperation(String line) {
for (Operation operation : Operation.values()) {
if (line.startsWith(operation.getCode())) {
return true;
}
}
return false;
}

public static Operation convertToOperaion(String data) {
for (Operation operation : Operation.values()) {
if (data.equals(operation.getCode())) {
return operation;
}
}
throw new RuntimeException("Unknown operation code: " + data);
}
}
}
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 DataConverterService {
List<FruitTransaction> convert(List<String> data);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package core.basesyntax.service;

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

public class DataConverterServiceImpl implements DataConverterService {
private static final String COMMA_SEPARATOR = ",";
private static final int OPERATION_TYPE_INDEX = 0;
private static final int FRUIT_INDEX = 1;
private static final int QUANTITY_INDEX = 2;

@Override
public List<FruitTransaction> convert(List<String> data) {
return data.stream()
.filter(FruitTransaction.Operation::startWithOperation)
.map(this::convertToFruitTransaction)
.collect(Collectors.toList());
}

private FruitTransaction convertToFruitTransaction(String data) {
String[] dataSplit = data.split(COMMA_SEPARATOR);
FruitTransaction.Operation operation
= FruitTransaction.Operation.convertToOperaion(dataSplit[OPERATION_TYPE_INDEX]);
String fruit = dataSplit[FRUIT_INDEX];
int quantity = Integer.parseInt(dataSplit[QUANTITY_INDEX]);
return new FruitTransaction(operation, fruit, quantity);
}
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/service/OperationStrategy.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 core.basesyntax.service.operation.OperationHandler;

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

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.service.operation.OperationHandler;
import java.util.Map;

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

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

@Override
public OperationHandler get(FruitTransaction.Operation operation) {
return operationHandlerMap.get(operation);
}
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/service/ReportService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.service;

import java.util.Map;

public interface ReportService {
String getReport(Map<String, Integer> data);
}
22 changes: 22 additions & 0 deletions src/main/java/core/basesyntax/service/ReportServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package core.basesyntax.service;

import java.util.Map;
import java.util.stream.Collectors;

public class ReportServiceImpl implements ReportService {
private static final String HEADER = "fruit,quantity";
private static final String COMMA_SEPARATOR = ",";
private StringBuilder builder;

public ReportServiceImpl() {
builder = new StringBuilder();
}

@Override
public String getReport(Map<String, Integer> data) {
String body = data.entrySet().stream()
.map(s -> s.getKey() + COMMA_SEPARATOR + s.getValue())
.collect(Collectors.joining(System.lineSeparator()));
return HEADER + System.lineSeparator() + body;
}
}
9 changes: 9 additions & 0 deletions src/main/java/core/basesyntax/service/ShopService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package core.basesyntax.service;

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

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

import core.basesyntax.dao.StorageDao;
import core.basesyntax.dao.StorageDaoImpl;
import core.basesyntax.model.FruitTransaction;
import java.util.List;
import java.util.Map;

public class ShopServiceImpl implements ShopService {
private OperationStrategy operationStrategy;
private StorageDao storageDao;

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

@Override
public Map<String, Integer> process(List<FruitTransaction> transactions) {
for (FruitTransaction transaction : transactions) {
operationStrategy.get(transaction.getType()).getOperation(transaction);
}
return storageDao.getAll();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.service.file;

import java.util.List;

public interface FileReaderService {
List<String> read(String path);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package core.basesyntax.service.file;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

public class FileReaderServiceImpl implements FileReaderService {

@Override
public List<String> read(String path) {
try {
return Files.readAllLines(Path.of(path));
} catch (IOException e) {
throw new RuntimeException("Cannot read file: " + path);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax.service.file;

public interface FileWriterService {
void write(String data, String path);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package core.basesyntax.service.file;

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

public class FileWriterServiceImpl implements FileWriterService {
@Override
public void write(String data, String path) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(path))) {
writer.write(data);
} catch (IOException e) {
throw new RuntimeException("Cannot write to file: " + path);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package core.basesyntax.service.operation;

import core.basesyntax.dao.StorageDao;
import core.basesyntax.dao.StorageDaoImpl;
import core.basesyntax.model.FruitTransaction;
import java.util.Map;

public class BalanceOperationHandler implements OperationHandler {
private StorageDao storageDao;

public BalanceOperationHandler() {
storageDao = new StorageDaoImpl();
}

@Override
public Map<String, Integer> getOperation(FruitTransaction transaction) {
storageDao.save(transaction.getFruit(), transaction.getQuantity());
return storageDao.getAll();
}
}
Loading

0 comments on commit b08583e

Please sign in to comment.