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

created tests for FruitShop #785

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

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

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.service.OperationService;
import core.basesyntax.service.ParseService;
import core.basesyntax.service.ReadService;
import core.basesyntax.service.WriterService;
import core.basesyntax.service.impl.ParseServiceImpl;
import core.basesyntax.service.impl.ReadServiceImpl;
import core.basesyntax.service.impl.ReportServiceImpl;
import core.basesyntax.service.impl.WriteServiceImpl;
import core.basesyntax.strategy.OperationHandler;
import core.basesyntax.strategy.OperationStrategy;
import core.basesyntax.strategy.impl.BalanceOperationHandler;
import core.basesyntax.strategy.impl.PurchaseOperationHandler;
import core.basesyntax.strategy.impl.ReturnOperationHandler;
import core.basesyntax.strategy.impl.SupplyOperationHandler;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
private static final String INPUT_PATH = "src/main/java/resources/input.csv";
private static final String OUTPUT_PATH = "src/main/java/resources/output.csv";

public static void main(String[] args) {
Map<FruitTransaction.Operation, OperationHandler>
operationHandlerMap = initializeOperationHandlers();

ReadService readService = new ReadServiceImpl();
List<String> inputData = readService.readInputData(INPUT_PATH);

ParseService parseService = new ParseServiceImpl();
List<FruitTransaction> parsedInputData = parseService.parseInputData(inputData);

OperationStrategy strategy = new OperationStrategy(operationHandlerMap);
OperationService service = new OperationService(strategy);
service.processOperation(parsedInputData);

String report = new ReportServiceImpl().createReport();
System.out.println(report);

WriterService writerService = new WriteServiceImpl();
writerService.writeToFile(OUTPUT_PATH, report);
}

private static Map<FruitTransaction.Operation, OperationHandler> initializeOperationHandlers() {
Map<FruitTransaction.Operation, OperationHandler> operationHandlerMap = new HashMap<>();
operationHandlerMap.put(
FruitTransaction.Operation.BALANCE, new BalanceOperationHandler());
operationHandlerMap.put(
FruitTransaction.Operation.SUPPLY, new SupplyOperationHandler());
operationHandlerMap.put(
FruitTransaction.Operation.PURCHASE, new PurchaseOperationHandler());
operationHandlerMap.put(
FruitTransaction.Operation.RETURN, new ReturnOperationHandler());
return operationHandlerMap;
}
}
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> STORAGE = new HashMap<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.exceptions;

public class FruitShopException extends RuntimeException {
public FruitShopException(String message) {
super(message);
}
}
59 changes: 59 additions & 0 deletions src/main/java/core/basesyntax/model/FruitTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package core.basesyntax.model;

import core.basesyntax.exceptions.FruitShopException;

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

public Operation getOperation() {
return operation;
}

public String getFruit() {
return fruit;
}

public int getQuantity() {
return quantity;
}

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

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

public void setQuantity(int quantity) {
this.quantity = quantity;
}

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

public static Operation getOperationByCode(String code) {
for (Operation operation : Operation.values()) {
if (operation.getCode().equals(code)) {
return operation;
}
}
throw new FruitShopException("This operation is unavailable " + code);
}
}
}
19 changes: 19 additions & 0 deletions src/main/java/core/basesyntax/service/OperationService.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.strategy.OperationStrategy;
import java.util.List;

public class OperationService {
private final OperationStrategy operationStrategy;

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

public void processOperation(List<FruitTransaction> transactions) {
for (FruitTransaction transaction : transactions) {
operationStrategy.get(transaction.getOperation()).handle(transaction);
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/service/ParseService.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 ParseService {
List<FruitTransaction> parseInputData(List<String> inputData);
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/service/ReadService.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 ReadService {
List<String> readInputData(String inputFilePath);
}
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 {
String writeToFile(String filePath, String report);
}
39 changes: 39 additions & 0 deletions src/main/java/core/basesyntax/service/impl/ParseServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package core.basesyntax.service.impl;

import core.basesyntax.exceptions.FruitShopException;
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.service.ParseService;
import java.util.ArrayList;
import java.util.List;

public class ParseServiceImpl implements ParseService {
private static final String DELIMITER = ",";
private static final int OPERATION_INDEX = 0;
private static final int FRUIT_INDEX = 1;
private static final int QUANTITY_INDEX = 2;
private static final int START_POINT = 1;

@Override
public List<FruitTransaction> parseInputData(List<String> inputData) {
if (inputData.isEmpty()) {
throw new FruitShopException("Input data list is empty!");
}
List<FruitTransaction> parsedData = new ArrayList<>();
for (int i = START_POINT; i < inputData.size(); i++) {
parsedData.add(parseEachLine(inputData.get(i)));
}
return parsedData;
}

private FruitTransaction parseEachLine(String line) {
FruitTransaction fruitTransaction = new FruitTransaction();
String[] splitLines = line.split(DELIMITER);
fruitTransaction.setOperation(
FruitTransaction.Operation.getOperationByCode(splitLines[OPERATION_INDEX]));
fruitTransaction.setFruit(
splitLines[FRUIT_INDEX]);
fruitTransaction.setQuantity(
Integer.parseInt(splitLines[QUANTITY_INDEX]));
return fruitTransaction;
}
}
20 changes: 20 additions & 0 deletions src/main/java/core/basesyntax/service/impl/ReadServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package core.basesyntax.service.impl;

import core.basesyntax.exceptions.FruitShopException;
import core.basesyntax.service.ReadService;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

public class ReadServiceImpl implements ReadService {
@Override
public List<String> readInputData(String inputFilePath) {
try {
return Files.readAllLines(Path.of(inputFilePath));
} catch (IOException e) {
throw new FruitShopException("Can't read this file " + inputFilePath);
}

}
}
26 changes: 26 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,26 @@
package core.basesyntax.service.impl;

import core.basesyntax.db.Storage;
import core.basesyntax.exceptions.FruitShopException;
import core.basesyntax.service.ReportService;
import java.util.Map;

public class ReportServiceImpl implements ReportService {
private static final String FIRST_ROW = "fruit,quantity" + System.lineSeparator();
private static final String DELIMITER = ",";

@Override
public String createReport() {
if (Storage.STORAGE.isEmpty()) {
throw new FruitShopException("Can't create report");
}
StringBuilder builder = new StringBuilder(FIRST_ROW);
for (Map.Entry<String, Integer> entry : Storage.STORAGE.entrySet()) {
builder.append(entry.getKey())
.append(DELIMITER)
.append(entry.getValue())
.append(System.lineSeparator());
}
return builder.toString();
}
}
19 changes: 19 additions & 0 deletions src/main/java/core/basesyntax/service/impl/WriteServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package core.basesyntax.service.impl;

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

public class WriteServiceImpl implements WriterService {
@Override
public String writeToFile(String filePath, String report) {
try {
Files.writeString(Path.of(filePath), report);
} catch (IOException e) {
throw new FruitShopException("Can't write to file " + filePath);
}
return report;
}
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/strategy/OperationHandler.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 OperationHandler {
void handle(FruitTransaction fruitTransaction);
}
21 changes: 21 additions & 0 deletions src/main/java/core/basesyntax/strategy/OperationStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package core.basesyntax.strategy;

import core.basesyntax.exceptions.FruitShopException;
import core.basesyntax.model.FruitTransaction;
import java.util.Map;

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

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

public OperationHandler get(FruitTransaction.Operation operation) {
if (operation == null) {
throw new FruitShopException("Operation can't be null!");
}
return operationHandlerMap.get(operation);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package core.basesyntax.strategy.impl;

import core.basesyntax.db.Storage;
import core.basesyntax.exceptions.FruitShopException;
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.strategy.OperationHandler;

public class BalanceOperationHandler implements OperationHandler {
@Override
public void handle(FruitTransaction fruitTransaction) {
if (fruitTransaction.getQuantity() <= 0) {
throw new FruitShopException("Quantity of fruits must be greater than zero");
}
Storage.STORAGE.put(fruitTransaction.getFruit(), fruitTransaction.getQuantity());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package core.basesyntax.strategy.impl;

import core.basesyntax.db.Storage;
import core.basesyntax.exceptions.FruitShopException;
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.strategy.OperationHandler;

public class PurchaseOperationHandler implements OperationHandler {

@Override
public void handle(FruitTransaction fruitTransaction) {
if (fruitTransaction.getQuantity() <= 0) {
throw new FruitShopException("Quantity must be greater than zero");
}
String fruitName = fruitTransaction.getFruit();
int transactionQuantity = fruitTransaction.getQuantity();
int initialQuantity = Storage.STORAGE.get(fruitName);
if (initialQuantity < transactionQuantity) {
throw new FruitShopException("Unfortunately, we don't have this amount ");
}
Storage.STORAGE.put(
fruitTransaction.getFruit(), initialQuantity - transactionQuantity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package core.basesyntax.strategy.impl;

import core.basesyntax.db.Storage;
import core.basesyntax.exceptions.FruitShopException;
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.strategy.OperationHandler;

public class ReturnOperationHandler implements OperationHandler {
@Override
public void handle(FruitTransaction fruitTransaction) {
if (fruitTransaction.getQuantity() <= 0) {
throw new FruitShopException("Quantity of fruits must be greater than zero");
}
String fruitName = fruitTransaction.getFruit();
int transactionQuantity = fruitTransaction.getQuantity();
int initialQuantity = Storage.STORAGE.get(fruitName);
Storage.STORAGE.put(
fruitName, initialQuantity + transactionQuantity);
}
}
Loading
Loading