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, cover all code by JUniy5 test. Try to do correct, now … #835

Open
wants to merge 3 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.

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

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.service.FileService;
import core.basesyntax.service.FileServiceImpl;
import core.basesyntax.service.FoodStoreService;
import core.basesyntax.service.FoodStoreServiceImpl;
import core.basesyntax.service.ReportService;
import core.basesyntax.service.ReportServiceImpl;
import core.basesyntax.service.TransactionParser;
import core.basesyntax.service.TransactionParserImpl;
import java.util.List;

public class Main {
private static final String INPUT_FILE_NAME = "src/main/resources/allData.csv";
private static final String OUTPUT_FILE_NAME = "src/main/resources/report.csv";
private static final int TITLE_ELEMENT_INDEX = 0;
private static final FoodStoreService foodStoreService = new FoodStoreServiceImpl();
private static final FileService fileService = new FileServiceImpl();
private static final TransactionParser transactionParser = new TransactionParserImpl();
private static final ReportService reportService = new ReportServiceImpl();

public static void main(String[] args) {
List<String> dailyData = fileService.read(INPUT_FILE_NAME);
dailyData.remove(TITLE_ELEMENT_INDEX);
List<FruitTransaction> fruitTransactions = transactionParser.parseTransactions(dailyData);
foodStoreService.processTransactions(fruitTransactions);
String report = reportService.createReport();
fileService.write(report, OUTPUT_FILE_NAME);
}
}
10 changes: 10 additions & 0 deletions src/main/java/core/basesyntax/db/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package core.basesyntax.db;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

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

import java.util.Objects;

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

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

public Operation getOperation() {
return operation;
}

public String getFruit() {
return fruit;
}

public int getQuantity() {
return 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 quantity == that.quantity && operation
== that.operation && Objects.equals(fruit, that.fruit);
}

@Override
public int hashCode() {
return Objects.hash(operation, fruit, quantity);
}

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

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

public static Operation fromCode(String code) {
for (Operation operation : Operation.values()) {
if (operation.getCode().equals(code)) {
return operation;
}
}
throw new IllegalArgumentException("No Operation found for code: " + code);

}

public String getCode() {
return code;
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/core/basesyntax/service/FileService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package core.basesyntax.service;

import java.util.List;

public interface FileService {

List<String> read(String fileName);

void write(String content, String fileName);
}
30 changes: 30 additions & 0 deletions src/main/java/core/basesyntax/service/FileServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package core.basesyntax.service;

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

public class FileServiceImpl implements FileService {

@Override
public List<String> read(String fileName) {
List<String> dailyData;
try {
dailyData = Files.readAllLines(Path.of(fileName));
} catch (IOException e) {
throw new RuntimeException("Can't get data from file: " + fileName, e);
}
return dailyData;
}

@Override
public void write(String content, String fileName) {
try {
Files.writeString(Path.of(fileName), content);
} catch (IOException e) {
throw new RuntimeException("Can't write data to file: " + fileName, e);
}
}
}

8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/service/FoodStoreService.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 FoodStoreService {
void processTransactions(List<FruitTransaction> fruitTransactionsList);
}
36 changes: 36 additions & 0 deletions src/main/java/core/basesyntax/service/FoodStoreServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

package core.basesyntax.service;

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.strategy.OperationStrategy;
import core.basesyntax.strategy.OperationStrategyImpl;
import core.basesyntax.strategy.operation.BalanceTypeHandler;
import core.basesyntax.strategy.operation.OperationHandlers;
import core.basesyntax.strategy.operation.PurchaseTypeHandler;
import core.basesyntax.strategy.operation.ReturnTypeHandler;
import core.basesyntax.strategy.operation.SupplyTypeHandler;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FoodStoreServiceImpl implements FoodStoreService {
private Map<FruitTransaction.Operation, OperationHandlers> operationHandlersMap =
new HashMap<>() {
{
put(FruitTransaction.Operation.BALANCE, new BalanceTypeHandler());
put(FruitTransaction.Operation.SUPPLY, new SupplyTypeHandler());
put(FruitTransaction.Operation.PURCHASE, new PurchaseTypeHandler());
put(FruitTransaction.Operation.RETURN, new ReturnTypeHandler());
}
};
private OperationStrategy operationStrategy =
new OperationStrategyImpl(operationHandlersMap);

@Override
public void processTransactions(List<FruitTransaction> fruitTransactionsList) {
for (FruitTransaction transaction : fruitTransactionsList) {
operationStrategy.getHandler(transaction.getOperation()).handleTransaction(transaction);
}
}
}

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();
}
19 changes: 19 additions & 0 deletions src/main/java/core/basesyntax/service/ReportServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package core.basesyntax.service;

import static core.basesyntax.db.Storage.storage;

import java.util.Map;

public class ReportServiceImpl implements ReportService {
@Override
public String createReport() {
StringBuilder stringBuilder = new StringBuilder();
for (Map.Entry<String, Integer> entry : storage.entrySet()) {
stringBuilder.append(entry.getKey())
.append(", ")
.append(entry.getValue())
.append(System.lineSeparator());
}
return stringBuilder.toString();
}
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/service/TransactionParser.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 TransactionParser {
List<FruitTransaction> parseTransactions(List<String> lines);
}
43 changes: 43 additions & 0 deletions src/main/java/core/basesyntax/service/TransactionParserImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package core.basesyntax.service;

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

public class TransactionParserImpl implements TransactionParser {
private static final int OPERATION_POSITION_AFTER_SPLIT = 0;
private static final int FRUIT_NAME_POSITION_AFTER_SPLIT = 1;
private static final int QUANTITY_POSITION_AFTER_SPLIT = 2;
private static final int EXPECTED_LENGTH_AFTER_SPLIT = 3;
private static final int EXPECTED_LENGTH_OF_TYPE = 1;

@Override
public List<FruitTransaction> parseTransactions(List<String> lines) {
List<FruitTransaction> fruitTransactions = new ArrayList<>();
for (String line : lines) {
String[] parsedLine = parseValue(line);
FruitTransaction.Operation operation =
validOperationValue(parsedLine[OPERATION_POSITION_AFTER_SPLIT]);
String fruitName = parsedLine[FRUIT_NAME_POSITION_AFTER_SPLIT];
int quantity = Integer.parseInt(parsedLine[QUANTITY_POSITION_AFTER_SPLIT]);
fruitTransactions.add(new FruitTransaction(operation, fruitName, quantity));
}
return fruitTransactions;
}

private String[] parseValue(String line) {
String[] split = line.split(",");
if (split.length != EXPECTED_LENGTH_AFTER_SPLIT) {
throw new IllegalArgumentException("Invalid input data: expected format "
+ "'<type>,<fruit_name>,<quantity>'");
}
return split;
}

private FruitTransaction.Operation validOperationValue(String line) {
if (line.length() != EXPECTED_LENGTH_OF_TYPE) {
throw new RuntimeException("Type should be only singe character: " + line);
}
return FruitTransaction.Operation.fromCode(line);
}
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/strategy/OperationStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core.basesyntax.strategy;

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.strategy.operation.OperationHandlers;

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

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.strategy.operation.OperationHandlers;
import java.util.Map;

public class OperationStrategyImpl implements OperationStrategy {
private Map<FruitTransaction.Operation, OperationHandlers> typeHandlersMap;

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

@Override
public OperationHandlers getHandler(FruitTransaction.Operation operation) {
return typeHandlersMap.get(operation);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package core.basesyntax.strategy.operation;

import static core.basesyntax.db.Storage.storage;

import core.basesyntax.model.FruitTransaction;

public class BalanceTypeHandler implements OperationHandlers {

@Override
public void handleTransaction(FruitTransaction transaction) {
storage.put(transaction.getFruit(),transaction.getQuantity());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.strategy.operation;

import core.basesyntax.model.FruitTransaction;

public interface OperationHandlers {
void handleTransaction(FruitTransaction transaction);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package core.basesyntax.strategy.operation;

import static core.basesyntax.db.Storage.storage;

import core.basesyntax.model.FruitTransaction;

public class PurchaseTypeHandler implements OperationHandlers {
@Override
public void handleTransaction(FruitTransaction transaction) {
int currentQuantity = storage.get(transaction.getFruit());
if (currentQuantity - transaction.getQuantity() < 0) {
throw new RuntimeException("Quantity cannot be less then 0!");
}
storage.put(transaction.getFruit(), currentQuantity
- transaction.getQuantity());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package core.basesyntax.strategy.operation;

import static core.basesyntax.db.Storage.storage;

import core.basesyntax.model.FruitTransaction;

public class ReturnTypeHandler implements OperationHandlers {
@Override
public void handleTransaction(FruitTransaction transaction) {
Integer currentQuantity = storage.get(transaction.getFruit());
storage.put(transaction.getFruit(),currentQuantity
+ transaction.getQuantity());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package core.basesyntax.strategy.operation;

import static core.basesyntax.db.Storage.storage;

import core.basesyntax.model.FruitTransaction;

public class SupplyTypeHandler implements OperationHandlers {

@Override
public void handleTransaction(FruitTransaction transaction) {
int currentQuantity = storage.get(transaction.getFruit());
storage.put(transaction.getFruit(),currentQuantity
+ transaction.getQuantity());
}
}
Loading
Loading