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

Wrote tests for the completed task #1084

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.

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

import java.util.List;

public interface FileReader {
List<String> readFile(String fileName);
}
21 changes: 21 additions & 0 deletions src/main/java/core/basesyntax/data/dao/FileReaderImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package core.basesyntax.data.dao;

import core.basesyntax.data.exeption.FileException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

public class FileReaderImpl implements FileReader {

@Override
public List<String> readFile(String filePath) {
List<String> reportList;
try {
reportList = Files.readAllLines(Path.of(filePath));
} catch (IOException e) {
throw new FileException("Can't read data from file: " + filePath, e);
}
return reportList;
}
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/data/dao/FileWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.data.dao;

import java.util.List;

public interface FileWriter {
List<String> writeFile(String filePath, String report);
}
19 changes: 19 additions & 0 deletions src/main/java/core/basesyntax/data/dao/FileWriterImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package core.basesyntax.data.dao;

import core.basesyntax.data.exeption.FileException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

public class FileWriterImpl implements FileWriter {
@Override
public List<String> writeFile(String filePath, String report) {
try {
Files.writeString(Path.of(filePath), report);
} catch (IOException e) {
throw new FileException("Can't write data to file: " + filePath, e);
}
return null;
}
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/data/db/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core.basesyntax.data.db;

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

public class Storage {
public static final Map<String, Integer> fruitsStorage = new HashMap<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.data.exeption;

public class FileException extends RuntimeException {
public FileException(String message, Throwable cause) {
super(message,cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.data.exeption;

public class InvalidArraySplitLength extends IllegalArgumentException {
public InvalidArraySplitLength(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.data.exeption;

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

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

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

public OperationType getOperation() {
return operation;
}

public void setOperation(OperationType 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;
}
}
26 changes: 26 additions & 0 deletions src/main/java/core/basesyntax/data/model/OperationType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package core.basesyntax.data.model;

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

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

public String getCode() {
return code;
}

public static OperationType fromCode(String code) {
for (OperationType operation : OperationType.values()) {
if (operation.getCode().equals(code)) {
return operation;
}
}
throw new IllegalArgumentException("No enum constant for code: " + code);
}
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/data/servise/DataConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core.basesyntax.data.servise;

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

public interface DataConverter {
List<FruitTransaction> convertToOperation(List<String> reportList);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax.data.servise;

public interface ReportGenerator {
String getReport();
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/data/servise/ShopService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core.basesyntax.data.servise;

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

public interface ShopService {
void process(List<FruitTransaction> operations);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core.basesyntax.data.servise;

import core.basesyntax.data.model.OperationType;
import core.basesyntax.data.strategy.OperationHandler;

public interface StrategyOperationService {
OperationHandler get(OperationType operationType);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package core.basesyntax.data.servise.ipl;

import core.basesyntax.data.model.FruitTransaction;
import core.basesyntax.data.model.OperationType;
import core.basesyntax.data.servise.DataConverter;
import java.util.ArrayList;
import java.util.List;

public class DataConverterImpl implements DataConverter {
private static final int LIST_START = 1;
private static final int OPERATION_INDEX = 0;
private static final int FRUIT_INDEX = 1;
private static final int QUANTITY_INDEX = 2;
private static final String COMMA = ",";

@Override
public List<FruitTransaction> convertToOperation(List<String> reportList) {
List<FruitTransaction> shopOperationList = new ArrayList<>();
for (String line : reportList.subList(LIST_START, reportList.size())) {
String[] parts = line.split(COMMA);
String codeOperation = parts[OPERATION_INDEX];
String fruit = parts[FRUIT_INDEX];
int quantity = Integer.parseInt(parts[QUANTITY_INDEX]);
shopOperationList.add(
new FruitTransaction(OperationType. fromCode(codeOperation),
fruit, quantity));
}
return shopOperationList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package core.basesyntax.data.servise.ipl;

import core.basesyntax.data.db.Storage;
import core.basesyntax.data.servise.ReportGenerator;
import java.util.Map;

public class ReportGeneratorImpl implements ReportGenerator {
private static final String LINE_SEPARATOR = System.lineSeparator();
private static final String COMMA = ",";
private static final String HEADER = "fruit,quantity";

@Override
public String getReport() {
StringBuilder stringBuilder = new StringBuilder(HEADER + LINE_SEPARATOR);
for (Map.Entry<String, Integer> entry : Storage.fruitsStorage.entrySet()) {
stringBuilder.append(entry.getKey())
.append(COMMA)
.append(entry.getValue())
.append(LINE_SEPARATOR);
}
return stringBuilder.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package core.basesyntax.data.servise.ipl;

import core.basesyntax.data.model.FruitTransaction;
import core.basesyntax.data.servise.ShopService;
import core.basesyntax.data.servise.StrategyOperationService;
import java.util.List;

public class ShopServiceImpl implements ShopService {
private final StrategyOperationService strategyOperationService;

public ShopServiceImpl(StrategyOperationService strategyOperationService) {
this.strategyOperationService = strategyOperationService;
}

@Override
public void process(List<FruitTransaction> operations) {
for (FruitTransaction operation : operations) {
strategyOperationService.get(operation.getOperation()).handle(operation);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package core.basesyntax.data.servise.ipl;

import core.basesyntax.data.exeption.OperationException;
import core.basesyntax.data.model.OperationType;
import core.basesyntax.data.servise.StrategyOperationService;
import core.basesyntax.data.strategy.OperationHandler;
import java.util.Map;

public class StrategyOperationServiceImpl implements StrategyOperationService {
private Map<OperationType, OperationHandler> operationHandlerMap;

public StrategyOperationServiceImpl(Map<OperationType, OperationHandler> operationHandlerMap) {
this.operationHandlerMap = operationHandlerMap;
}

@Override
public OperationHandler get(OperationType operationType) {
OperationHandler handler = operationHandlerMap.get(operationType);
if (handler != null) {
return handler;
}
throw new OperationException("Operation type is not correct: " + operationType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package core.basesyntax.data.strategy;

import core.basesyntax.data.db.Storage;
import core.basesyntax.data.exeption.OperationException;
import core.basesyntax.data.model.FruitTransaction;

public class BalanceOperationHandler implements OperationHandler {

@Override
public void handle(FruitTransaction operation) {
int quantity = operation.getQuantity();
if (quantity < 0) {
throw new OperationException(
"Operation is not correct, quantity can't be negative. Quantity: "
+ operation.getQuantity());
}
Storage.fruitsStorage.put(operation.getFruit(), operation.getQuantity());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core.basesyntax.data.strategy;

import core.basesyntax.data.model.FruitTransaction;

public interface OperationHandler {

void handle(FruitTransaction operation);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package core.basesyntax.data.strategy;

import core.basesyntax.data.db.Storage;
import core.basesyntax.data.exeption.OperationException;
import core.basesyntax.data.model.FruitTransaction;

public class PurchaseOperationHandler implements OperationHandler {

@Override
public void handle(FruitTransaction operation) {

String fruit = operation.getFruit();
if (!Storage.fruitsStorage.containsKey(fruit)) {
throw new OperationException("Operation is not correct, fruit doesn't exist: " + fruit);
}
int quantity = Storage.fruitsStorage.get(fruit);
int quantityToBuy = operation.getQuantity();
if (quantityToBuy < 0) {
throw new OperationException(
"Operation is not correct, quantity can't be negative. Quantity: "
+ quantityToBuy);
}
if (quantity < quantityToBuy) {
throw new OperationException("Operation is impossible, not enough quantity of: "
+ fruit);
}
Storage.fruitsStorage.put(fruit, quantity - quantityToBuy);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package core.basesyntax.data.strategy;

import core.basesyntax.data.db.Storage;
import core.basesyntax.data.exeption.OperationException;
import core.basesyntax.data.model.FruitTransaction;

public class ReturnOperationHandler implements OperationHandler {

@Override
public void handle(FruitTransaction operation) {
String fruit = operation.getFruit();
if (!Storage.fruitsStorage.containsKey(fruit)) {
throw new OperationException("Operation is not correct, fruit doesn't exist: " + fruit);
}
int quantity = Storage.fruitsStorage.get(fruit);
int returnedQuantity = operation.getQuantity();
if (returnedQuantity < 0) {
throw new OperationException(
"Operation is not correct, quantity can't be negative. Quantity: "
+ returnedQuantity);
}
Storage.fruitsStorage.put(fruit, quantity + returnedQuantity);

}
}
Loading
Loading