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

add tests to fruitshop solution #787

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

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

import core.basesyntax.handler.BalanceOperationHandler;
import core.basesyntax.handler.OperationHandler;
import core.basesyntax.handler.PurchaseOperationHandler;
import core.basesyntax.handler.ReturnOperationHandler;
import core.basesyntax.handler.SupplyOperationHandler;
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.service.impl.OperationProcessorService;
import core.basesyntax.service.impl.ReaderServiceImpl;
import core.basesyntax.service.impl.ReportServiceImpl;
import core.basesyntax.service.impl.TransactionParserServiceImpl;
import core.basesyntax.service.impl.WriterServiceImpl;
import core.basesyntax.strategy.OperationStrategy;
import core.basesyntax.strategy.impl.OperationStrategyImpl;
import core.basesyntax.validation.impl.OutputValidatorImpl;
import core.basesyntax.validation.impl.ReaderValidationImpl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
private static final String INPUT_FILE_PATH = "src/main/resources/data.csv";
private static final String OUTPUT_FILE_PATH = "src/main/resources/report.csv";
private static Map<FruitTransaction.Operation, OperationHandler> handlerMap;

static {
handlerMap = new HashMap<>();
handlerMap.put(FruitTransaction.Operation.BALANCE, new BalanceOperationHandler());
handlerMap.put(FruitTransaction.Operation.SUPPLY, new SupplyOperationHandler());
handlerMap.put(FruitTransaction.Operation.RETURN, new ReturnOperationHandler());
handlerMap.put(FruitTransaction.Operation.PURCHASE, new PurchaseOperationHandler());
}

public static void main(String[] args) {
OperationStrategy operationStrategy = new OperationStrategyImpl(handlerMap);

List<String> data = new ReaderServiceImpl().readFromFile(INPUT_FILE_PATH);
List<String> validationData = new ReaderValidationImpl().validate(data);
List<FruitTransaction> transactions = new TransactionParserServiceImpl()
.parse(validationData);

OperationProcessorService processorService = new OperationProcessorService();
processorService.process(transactions, operationStrategy);

String report = new ReportServiceImpl().generateReport();
new WriterServiceImpl().writeToFile(report, OUTPUT_FILE_PATH);
new OutputValidatorImpl().validateFile(OUTPUT_FILE_PATH);
}
}
12 changes: 12 additions & 0 deletions src/main/java/core/basesyntax/dao/StorageDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package core.basesyntax.dao;

import core.basesyntax.model.Fruit;
import java.util.Map;

public interface StorageDao {
void add(Fruit fruit, Integer value);

void substractAmount(Fruit fruit, Integer amountToDelete);

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

import core.basesyntax.dao.StorageDao;
import core.basesyntax.db.Storage;
import core.basesyntax.model.Fruit;
import java.util.Map;

public class StorageDaoImpl implements StorageDao {
@Override
public void add(Fruit fruit, Integer value) {
Storage.storage.put(fruit, Storage.storage.getOrDefault(fruit, 0)
+ value);
}

@Override
public void substractAmount(Fruit fruit, Integer amountToDelete) {
Integer quantityOfFruit = Storage.storage.get(fruit);
if (quantityOfFruit - amountToDelete < 0) {
throw new RuntimeException(String.format("We have %d quantity of %s but you "
+ "want to buy %d", quantityOfFruit, fruit.getName(),amountToDelete));
}
Storage.storage.put(fruit, quantityOfFruit - amountToDelete);
}

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

import core.basesyntax.model.Fruit;
import java.util.HashMap;
import java.util.Map;

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

import core.basesyntax.dao.StorageDao;
import core.basesyntax.dao.impl.StorageDaoImpl;
import core.basesyntax.model.FruitTransaction;

public class BalanceOperationHandler implements OperationHandler {
private StorageDao storageDao = new StorageDaoImpl();

@Override
public void apply(FruitTransaction transaction) {
storageDao.add(transaction.getFruit(),transaction.getQuantity());
}
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/handler/OperationHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.handler;

import core.basesyntax.model.FruitTransaction;

public interface OperationHandler {
void apply(FruitTransaction fruitTransaction);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package core.basesyntax.handler;

import core.basesyntax.dao.StorageDao;
import core.basesyntax.dao.impl.StorageDaoImpl;
import core.basesyntax.model.FruitTransaction;

public class PurchaseOperationHandler implements OperationHandler {
private StorageDao storageDao = new StorageDaoImpl();

@Override
public void apply(FruitTransaction transaction) {
storageDao.substractAmount(transaction.getFruit(),transaction.getQuantity());
}
}
14 changes: 14 additions & 0 deletions src/main/java/core/basesyntax/handler/ReturnOperationHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package core.basesyntax.handler;

import core.basesyntax.dao.StorageDao;
import core.basesyntax.dao.impl.StorageDaoImpl;
import core.basesyntax.model.FruitTransaction;

public class ReturnOperationHandler implements OperationHandler {
private StorageDao storageDao = new StorageDaoImpl();

@Override
public void apply(FruitTransaction transaction) {
storageDao.add(transaction.getFruit(), transaction.getQuantity());
}
}
14 changes: 14 additions & 0 deletions src/main/java/core/basesyntax/handler/SupplyOperationHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package core.basesyntax.handler;

import core.basesyntax.dao.StorageDao;
import core.basesyntax.dao.impl.StorageDaoImpl;
import core.basesyntax.model.FruitTransaction;

public class SupplyOperationHandler implements OperationHandler {
private StorageDao storageDao = new StorageDaoImpl();

@Override
public void apply(FruitTransaction transaction) {
storageDao.add(transaction.getFruit(),transaction.getQuantity());
}
}
43 changes: 43 additions & 0 deletions src/main/java/core/basesyntax/model/Fruit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package core.basesyntax.model;

import java.util.Objects;

public class Fruit {
private String name;

public Fruit(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Fruit fruit = (Fruit) o;
return Objects.equals(name, fruit.name);
}

@Override
public int hashCode() {
return Objects.hash(name);
}

@Override
public String toString() {
return "Fruit{"
+ "name='" + name + '\''
+ '}';
}
}
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 java.util.Arrays;
import java.util.Optional;

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

public Operation getOperation() {
return operation;
}

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

public Fruit getFruit() {
return fruit;
}

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

public int getQuantity() {
return quantity;
}

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

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

private String operation;

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

public String getOperation() {
return operation;
}

public static Operation fromOperationValue(String operationValue) {
Optional<Operation> optionalOperation = Arrays.stream(values())
.filter(v -> v.getOperation().equals(operationValue))
.findFirst();
return optionalOperation.orElseThrow(() -> new IllegalArgumentException("Invalid"
+ " operation value: " + operationValue));
}
}
}
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 filePath);
}
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 generateReport();
}
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 TransactionParserService {
List<FruitTransaction> parse(List<String> dataSheet);
}
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 report, String filePath);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package core.basesyntax.service.impl;

import core.basesyntax.handler.OperationHandler;
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.strategy.OperationStrategy;
import java.util.List;

public class OperationProcessorService {
public void process(List<FruitTransaction> transactions, OperationStrategy operationStrategy) {
for (FruitTransaction transaction : transactions) {
OperationHandler handler = operationStrategy.getByOperation(transaction.getOperation());
handler.apply(transaction);
}
}
}
19 changes: 19 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,19 @@
package core.basesyntax.service.impl;

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

public class ReaderServiceImpl implements ReaderService {
@Override
public List<String> readFromFile(String filePath) {
try {
List<String> data = Files.readAllLines(Paths.get(filePath));
return data;
} catch (IOException e) {
throw new RuntimeException("Can't get data from file with path: ", e);
}
}
}
24 changes: 24 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,24 @@
package core.basesyntax.service.impl;

import core.basesyntax.db.Storage;
import core.basesyntax.model.Fruit;
import core.basesyntax.service.ReportService;
import java.util.Map;

public class ReportServiceImpl implements ReportService {
private static final String REPORT_HEADER = "fruit,quantity";
private static final String SEPARATOR = ",";

@Override
public String generateReport() {
StringBuilder reportLine = new StringBuilder(REPORT_HEADER)
.append(System.lineSeparator());
for (Map.Entry<Fruit, Integer> row : Storage.storage.entrySet()) {
reportLine.append(row.getKey().getName())
.append(SEPARATOR)
.append(row.getValue())
.append(System.lineSeparator());
}
return reportLine.toString();
}
}
Loading