Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution for jv-fruit-shop-test #838

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

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

import core.basesyntax.dao.FruitStorageDao;
import core.basesyntax.dao.FruitStorageDaoImpl;
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.model.FruitTransactionOperation;
import core.basesyntax.service.FruitTransactionService;
import core.basesyntax.service.ParserService;
import core.basesyntax.service.ReaderService;
import core.basesyntax.service.ReportService;
import core.basesyntax.service.WriterService;
import core.basesyntax.service.impl.FruitTransactionServiceImpl;
import core.basesyntax.service.impl.ParserServiceImpl;
import core.basesyntax.service.impl.ReaderServiceImpl;
import core.basesyntax.service.impl.ReportServiceImpl;
import core.basesyntax.service.impl.WriterServiceImpl;
import core.basesyntax.service.transaction.BalanceHandler;
import core.basesyntax.service.transaction.OperationHandler;
import core.basesyntax.service.transaction.PurchaseHandler;
import core.basesyntax.service.transaction.ReturnHandler;
import core.basesyntax.service.transaction.SupplyHandler;
import core.basesyntax.strategy.OperationStrategy;
import core.basesyntax.strategy.OperationStrategyImpl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {

private static final String TRANSACTIONS_PATH = "src/main/java/resources/transaction.cvs";
private static final String REPORT_PATH = "src/main/java/resources/report.csv";
private static Map<FruitTransactionOperation, OperationHandler> strategyMap = new HashMap<>();

public static void main(String[] args) {
fillMap();
FruitStorageDao storageDao = new FruitStorageDaoImpl();
ReaderService fileReaderService = new ReaderServiceImpl();
List<String> strings = fileReaderService.readFromFile(TRANSACTIONS_PATH);

ParserService parser = new ParserServiceImpl();
List<FruitTransaction> fruitTransactions = parser.stringsToFruitTransactions(strings);

OperationStrategy operationStrategy = new OperationStrategyImpl(strategyMap);
FruitTransactionService fruitTransactionService =
new FruitTransactionServiceImpl(storageDao, operationStrategy);
fruitTransactionService.processTransactions(fruitTransactions);

ReportService fruitTransactionsReportService = new ReportServiceImpl();
String report = fruitTransactionsReportService.createReport();

WriterService fileWriterService = new WriterServiceImpl();
fileWriterService.writeToFile(REPORT_PATH, report);
}

private static void fillMap() {
strategyMap.put(FruitTransactionOperation.BALANCE, new BalanceHandler());
strategyMap.put(FruitTransactionOperation.PURCHASE, new PurchaseHandler());
strategyMap.put(FruitTransactionOperation.SUPPLY, new SupplyHandler());
strategyMap.put(FruitTransactionOperation.RETURN, new ReturnHandler());
}
}
10 changes: 10 additions & 0 deletions src/main/java/core/basesyntax/dao/FruitStorageDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package core.basesyntax.dao;

import java.util.Map;

public interface FruitStorageDao {

void addAll(Map<String, Integer> map);

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

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

public class FruitStorageDaoImpl implements FruitStorageDao {
@Override
public void addAll(Map<String,Integer> map) {
Storage.fruitTransactions.putAll(map);
}

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

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

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

import java.util.Objects;

public class FruitTransaction {
private FruitTransactionOperation operation;
private String fruit;
private int quantity;

public FruitTransactionOperation getOperation() {
return operation;
}

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

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

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
FruitTransaction that = (FruitTransaction) o;
return getQuantity() == that.getQuantity() && getOperation() == that.getOperation()
&& Objects.equals(getFruit(), that.getFruit());
}

@Override
public int hashCode() {
return Objects.hash(getOperation(), getFruit(), getQuantity());
}
}
22 changes: 22 additions & 0 deletions src/main/java/core/basesyntax/model/FruitTransactionOperation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package core.basesyntax.model;

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

private final String code;
FruitTransactionOperation(String code) {
this.code = code;
}

public static FruitTransactionOperation getOperation(String operationLetter) {
for (FruitTransactionOperation operation : FruitTransactionOperation.values()) {
if (operation.code.equals(operationLetter)) {
return operation;
}
}
throw new IllegalArgumentException("No such operation: " + operationLetter);
}
}
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 FruitTransactionService {
void processTransactions(List<FruitTransaction> fruitTransactions);
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/service/ParserService.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 ParserService {
List<FruitTransaction> stringsToFruitTransactions(List<String> strings);
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/service/ReaderService.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 ReaderService {
List<String> readFromFile(String fileName);
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/service/ReportService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax.service;

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

public interface WriterService {
void writeToFile(String fileName, String content);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package core.basesyntax.service.impl;

import core.basesyntax.dao.FruitStorageDao;
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.service.FruitTransactionService;
import core.basesyntax.service.transaction.OperationHandler;
import core.basesyntax.strategy.OperationStrategy;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FruitTransactionServiceImpl implements FruitTransactionService {
private final OperationStrategy strategy;
private final FruitStorageDao storageDao;

public FruitTransactionServiceImpl(FruitStorageDao storageDao, OperationStrategy strategy) {
this.storageDao = storageDao;
this.strategy = strategy;
}

@Override
public void processTransactions(List<FruitTransaction> fruitTransactions) {
Map<String, Integer> fruitsBalance = new HashMap<>();
for (FruitTransaction fruitTransaction : fruitTransactions) {
calculateOperationForFruit(fruitsBalance, fruitTransaction);
}
storageDao.addAll(fruitsBalance);
}

private void calculateOperationForFruit(Map<String, Integer> fruitsBalance,
FruitTransaction fruitTransaction) {
OperationHandler operationHandler =
strategy.get(fruitTransaction.getOperation());
int quantity;
int currentAmount = 0;
if (fruitsBalance.containsKey(fruitTransaction.getFruit())) {
currentAmount = fruitsBalance.get(fruitTransaction.getFruit());
}
quantity = operationHandler.countQuantity(
currentAmount, fruitTransaction.getQuantity());
fruitsBalance.put(fruitTransaction.getFruit(), quantity);
}
}
43 changes: 43 additions & 0 deletions src/main/java/core/basesyntax/service/impl/ParserServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package core.basesyntax.service.impl;

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.model.FruitTransactionOperation;
import core.basesyntax.service.ParserService;
import java.util.ArrayList;
import java.util.List;

public class ParserServiceImpl implements ParserService {
private static final String COMMA = ",";
private static final String TRANSACTION_REGEX = "\\w,\\w+,\\d+";
private static final int INDEX_OF_OPERATION = 0;
private static final int INDEX_OF_FRUIT = 1;
private static final int INDEX_OF_QUANTITY = 2;
private static final String INVALID_TRANSACTION = "Transaction is not valid, must match: ";

@Override
public List<FruitTransaction> stringsToFruitTransactions(List<String> strings) {
List<FruitTransaction> fruitTransactions = new ArrayList<>();
for (String transaction : strings) {
if (transaction.matches(TRANSACTION_REGEX)) {
FruitTransaction fruitTransaction = getFruitTransaction(transaction);
fruitTransactions.add(fruitTransaction);
} else {
throw new RuntimeException("Transaction: " + transaction
+ " " + INVALID_TRANSACTION + TRANSACTION_REGEX);
}
}
return fruitTransactions;
}

private FruitTransaction getFruitTransaction(String transaction) {
FruitTransaction fruitTransaction = new FruitTransaction();
String[] transactionValues = transaction.split(COMMA);
String operationValue = transactionValues[INDEX_OF_OPERATION];
fruitTransaction.setOperation(
FruitTransactionOperation.getOperation(operationValue));
int quantity = Integer.parseInt(transactionValues[INDEX_OF_QUANTITY]);
fruitTransaction.setQuantity(quantity);
fruitTransaction.setFruit(transactionValues[INDEX_OF_FRUIT]);
return fruitTransaction;
}
}
30 changes: 30 additions & 0 deletions src/main/java/core/basesyntax/service/impl/ReaderServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package core.basesyntax.service.impl;

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

public class ReaderServiceImpl implements ReaderService {
private static final String FILE_READ_ERROR_MESSAGE = "Can't read from file";
private static final String NO_TRANSACTIONS_MESSAGE = "File does not contain transactions";
private static final String READER_NULL_EXCEPTION_MESSAGE = "file Path can't be null";
private static final int FIRST_LINE = 1;

@Override
public List<String> readFromFile(String fileName) {
if (fileName == null) {
throw new RuntimeException(READER_NULL_EXCEPTION_MESSAGE);
}
try {
List<String> strings = Files.readAllLines(Path.of(fileName));
if (strings.size() <= FIRST_LINE) {
throw new RuntimeException(NO_TRANSACTIONS_MESSAGE);
}
return strings.subList(FIRST_LINE, strings.size());
} catch (IOException e) {
throw new RuntimeException(FILE_READ_ERROR_MESSAGE + " " + fileName, e);
}
}
}
32 changes: 32 additions & 0 deletions src/main/java/core/basesyntax/service/impl/ReportServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package core.basesyntax.service.impl;

import core.basesyntax.dao.FruitStorageDao;
import core.basesyntax.dao.FruitStorageDaoImpl;
import core.basesyntax.service.ReportService;
import java.util.Map;

public class ReportServiceImpl implements ReportService {

private static final String COMMA = ",";
private static final String HEADER = "fruit,quantity";
private static final String LINE_SEPARATOR = System.lineSeparator();
private final FruitStorageDao storageDao;

public ReportServiceImpl() {
this.storageDao = new FruitStorageDaoImpl();
}

@Override
public String createReport() {
Map<String, Integer> reportMap = storageDao.getAll();
StringBuilder report = new StringBuilder(HEADER)
.append(LINE_SEPARATOR);
for (Map.Entry<String, Integer> entry : reportMap.entrySet()) {
report.append(entry.getKey())
.append(COMMA)
.append(entry.getValue())
.append(LINE_SEPARATOR);
}
return report.toString();
}
}
25 changes: 25 additions & 0 deletions src/main/java/core/basesyntax/service/impl/WriterServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package core.basesyntax.service.impl;

import core.basesyntax.service.WriterService;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class WriterServiceImpl implements WriterService {
private static final String FILE_WRITE_EXCEPTION_MESSAGE = "Can't write to file: ";
private static final String WRITE_NULL_EXCEPTION_MESSAGE = "file Path can't be null";

@Override
public void writeToFile(String fileName, String content) {
if (fileName == null) {
throw new RuntimeException(WRITE_NULL_EXCEPTION_MESSAGE);
}
Path path = Paths.get(fileName);
try {
Files.writeString(path, content);
} catch (IOException e) {
throw new RuntimeException(FILE_WRITE_EXCEPTION_MESSAGE + fileName, e);
}
}
}
Loading
Loading