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

done the task #823

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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.

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

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import model.FruitTransaction;
import service.OperationStrategy;
import service.ReportMaking;
import service.Reporter;
import serviceimpl.FruitPackerImpl;
import serviceimpl.OperationStrategyImpl;
import serviceimpl.ReaderImpl;
import serviceimpl.ReportMakingImpl;
import serviceimpl.ReporterImpl;
import serviceimpl.WriterImpl;
import strategy.BalanceOperation;
import strategy.Operating;
import strategy.PurchaseOperation;
import strategy.ReturnOperation;
import strategy.SupplyOperation;

public class Main {
public static final String SOURCE_FILE_NAME = "src/main/java/resources/sourceFile.csv";
public static final String RESULT_FILE_NAME = "src/main/java/resources/resultFile.csv";
private static final Map<FruitTransaction.Operation, Operating> operationsMap = new HashMap<>();

public static void main(String[] args) {
operationsMap.putAll(allOperationsMap());
List<String> transactionsStringList = new ReaderImpl().readDataFromFile(SOURCE_FILE_NAME);
List<FruitTransaction> transactionsList = new FruitPackerImpl()
.makeList(transactionsStringList);
OperationStrategy operationStrategyService = new OperationStrategyImpl(operationsMap);
ReportMaking fruitTransactionService = new ReportMakingImpl(operationStrategyService);
fruitTransactionService.makeReport(transactionsList);
Reporter reportService = new ReporterImpl();
List<String> dailyTransactionsReport = reportService.report();
new WriterImpl().writeToFile(dailyTransactionsReport, RESULT_FILE_NAME);
}

public static Map<FruitTransaction.Operation, Operating> allOperationsMap() {
Map<FruitTransaction.Operation, Operating> operationsMap = new HashMap<>();
operationsMap.put(FruitTransaction.Operation.SUPPLY, new SupplyOperation());
operationsMap.put(FruitTransaction.Operation.BALANCE, new BalanceOperation());
operationsMap.put(FruitTransaction.Operation.RETURN, new ReturnOperation());
operationsMap.put(FruitTransaction.Operation.PURCHASE, new PurchaseOperation());
return operationsMap;
}
}

35 changes: 35 additions & 0 deletions src/main/java/db/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package db;

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

public class Storage {
private static final String NOT_NULL_MESSAGE = "Null can not be add";
private static final Map<String, Integer> storage = new HashMap<>();

public static Integer getFruitsNumber(String fruit) {
if (storage.isEmpty() || !storage.containsKey(fruit)) {
return 0;
}
return storage.get(fruit);
}

public static void storeFruit(String fruit, Integer amount) {
if (fruit == null) {
throw new RuntimeException(NOT_NULL_MESSAGE);
}
storage.put(fruit, amount);
}

public static Map<String, Integer> getStorage() {
return storage;
}

public static boolean containsFruit(String fruit) {
return storage.containsKey(fruit);
}

public static void clearStorage() {
storage.clear();
}
}
71 changes: 71 additions & 0 deletions src/main/java/model/FruitTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package model;

import java.util.Objects;

public class FruitTransaction {
private static final String CAN_NOT_FIND_OPERATION = "Can not find suitable Operation ";
private final Operation operation;
private String fruit;
private final Integer quantity;

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

public Operation getOperation() {
return operation;
}

public String getFruit() {
return fruit;
}

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

public int getQuantity() {
return quantity;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof FruitTransaction that)) {
return false;
}
return operation == that.operation
&& Objects.equals(fruit, that.fruit)
&& Objects.equals(quantity, that.quantity);
}

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

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

public String getOperation() {
return operation;
}

public static Operation findRightOperation(String operation) {
for (Operation op : Operation.values()) {
if (op.operation.equals(operation)) {
return op;
}
}
throw new RuntimeException(CAN_NOT_FIND_OPERATION + operation);
}
}

}
3 changes: 3 additions & 0 deletions src/main/java/resources/resultFile.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fruit,quantity
banana,70
apple,95
5 changes: 5 additions & 0 deletions src/main/java/resources/sourceFile.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type,fruit,quantity
b,banana,20
b,apple,100
p,apple,5
s,banana,50
9 changes: 9 additions & 0 deletions src/main/java/service/FruitPacker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package service;

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

public interface FruitPacker {
List<FruitTransaction> makeList(List<String> transactionListString);
}

8 changes: 8 additions & 0 deletions src/main/java/service/OperationStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package service;

import model.FruitTransaction;
import strategy.Operating;

public interface OperationStrategy {
Operating findRightStrategy(FruitTransaction.Operation operation);
}
7 changes: 7 additions & 0 deletions src/main/java/service/Reader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package service;

import java.util.List;

public interface Reader {
List<String> readDataFromFile(String transactionFullPath);
}
8 changes: 8 additions & 0 deletions src/main/java/service/ReportMaking.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package service;

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

public interface ReportMaking {
void makeReport(List<FruitTransaction> fruitTransactionList);
}
7 changes: 7 additions & 0 deletions src/main/java/service/Reporter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package service;

import java.util.List;

public interface Reporter {
List<String> report();
}
7 changes: 7 additions & 0 deletions src/main/java/service/Writer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package service;

import java.util.List;

public interface Writer {
void writeToFile(List<String> dailyTransactionsStringList, String reportFullPath);
}
30 changes: 30 additions & 0 deletions src/main/java/serviceimpl/FruitPackerImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package serviceimpl;

import java.util.List;
import java.util.stream.Collectors;
import model.FruitTransaction;
import service.FruitPacker;

public class FruitPackerImpl implements FruitPacker {
public static final String COMMA = ",";
private static final String NOT_NULL_MESSAGE = "List can not be null";
private static final int INDEX_FOR_OPERATION = 0;
private static final int INDEX_FOR_FRUITS = 1;
private static final int INDEX_FOR_NUMBER = 2;

public FruitPackerImpl() {
}

public List<FruitTransaction> makeList(List<String> transactionListString) {
if (transactionListString == null) {
throw new RuntimeException(NOT_NULL_MESSAGE);
}
return transactionListString.stream()
.map(e -> e.split(COMMA))
.map(e -> new FruitTransaction(FruitTransaction
.Operation.findRightOperation(e[INDEX_FOR_OPERATION]),
e[INDEX_FOR_FRUITS],
Integer.parseInt(e[INDEX_FOR_NUMBER])))
.collect(Collectors.toList());
}
}
20 changes: 20 additions & 0 deletions src/main/java/serviceimpl/OperationStrategyImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package serviceimpl;

import java.util.Map;
import model.FruitTransaction;
import service.OperationStrategy;
import strategy.Operating;

public class OperationStrategyImpl implements OperationStrategy {

private final Map<FruitTransaction.Operation, Operating> operationHandlersMap;

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

@Override
public Operating findRightStrategy(FruitTransaction.Operation operation) {
return operationHandlersMap.get(operation);
}
}
23 changes: 23 additions & 0 deletions src/main/java/serviceimpl/ReaderImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package serviceimpl;

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

public class ReaderImpl implements Reader {
private static final String FILE_FINDING_PROBLEM = "Can not find file ";

public List<String> readDataFromFile(String filePath) {
try {
List<String> dataFromFile = Files.readAllLines(Path.of(filePath));
if (!dataFromFile.isEmpty()) {
dataFromFile.remove(0);
}
return dataFromFile;
} catch (IOException e) {
throw new RuntimeException(FILE_FINDING_PROBLEM + filePath, e);
}
}
}
22 changes: 22 additions & 0 deletions src/main/java/serviceimpl/ReportMakingImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package serviceimpl;

import java.util.List;
import model.FruitTransaction;
import service.OperationStrategy;
import service.ReportMaking;

public class ReportMakingImpl implements ReportMaking {
private final OperationStrategy operationStrategy;

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

@Override
public void makeReport(List<FruitTransaction> fruitTransactionList) {
for (FruitTransaction transaction : fruitTransactionList) {
operationStrategy.findRightStrategy(transaction.getOperation())
.doOperation(transaction);
}
}
}
25 changes: 25 additions & 0 deletions src/main/java/serviceimpl/ReporterImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package serviceimpl;

import db.Storage;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import service.Reporter;

public class ReporterImpl implements Reporter {
private static final String FIRST_STRING = "fruit,quantity";

@Override
public List<String> report() {
Map<String, Integer> storage = Storage.getStorage();
List<String> dailyBalanceList =
storage.entrySet()
.stream()
.map(s -> s.getKey().toLowerCase()
+ FruitPackerImpl.COMMA
+ s.getValue())
.collect(Collectors.toList());
dailyBalanceList.add(0, FIRST_STRING);
return dailyBalanceList;
}
}
25 changes: 25 additions & 0 deletions src/main/java/serviceimpl/WriterImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package serviceimpl;

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import service.Writer;

public class WriterImpl implements Writer {
private static final String FILE_FINDING_PROBLEM = "Can not find file ";
private static final String PROBLEM_WITH_ARGUMENTS =
"Invalid arguments: lines or fileName is null";

public void writeToFile(List<String> lines, String fileName) {
if (lines == null || fileName == null) {
throw new IllegalArgumentException(PROBLEM_WITH_ARGUMENTS);
}
try (FileWriter writer = new FileWriter(fileName)) {
for (String line : lines) {
writer.write(line + System.lineSeparator());
}
} catch (IOException e) {
throw new RuntimeException(FILE_FINDING_PROBLEM + fileName, e);
}
}
}
18 changes: 18 additions & 0 deletions src/main/java/strategy/BalanceOperation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package strategy;

import db.Storage;
import model.FruitTransaction;

public class BalanceOperation implements Operating {
public BalanceOperation() {
}

@Override
public void doOperation(FruitTransaction fruitTransaction) {
String fruit = fruitTransaction.getFruit();
Integer transactionAmount = fruitTransaction.getQuantity();
Integer storageAmount = Storage.getFruitsNumber(fruit);
Storage.storeFruit(fruit, storageAmount + transactionAmount);
}

}
Loading
Loading