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 unit tests #837

Open
wants to merge 8 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
9 changes: 9 additions & 0 deletions data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type,fruit,quantity
b,banana,20
b,apple,100
s,banana,100
p,banana,13
r,apple,10
p,apple,20
p,banana,5
s,banana,50
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

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

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

public class Storage {
private static Map<String, Integer> storageMap = new HashMap<>();

public static void addFruit(String fruit, Integer amount) {
storageMap.put(fruit, amount + storageMap.get(fruit));
}

public static Map<String, Integer> getFruitsAndAmount() {
Map<String, Integer> newMap = new HashMap<>();
for (Map.Entry<String, Integer> entry : storageMap.entrySet()) {
newMap.put(entry.getKey(), entry.getValue());
}

return newMap;
}

public static void setAmount(String fruit, Integer number) {
storageMap.put(fruit, number);
}
}
42 changes: 42 additions & 0 deletions src/main/java/fruitshop/main/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package fruitshop.main;

import fruitshop.model.Operation;
import fruitshop.operation.OperationHandler;
import fruitshop.operation.impl.BalanceOperationHandlerImpl;
import fruitshop.operation.impl.OperationStrategyImpl;
import fruitshop.operation.impl.PurchaseOperationHandlerImpl;
import fruitshop.operation.impl.ReturnOperationHandlerImpl;
import fruitshop.operation.impl.SupplyOperationHandlerImpl;
import fruitshop.service.Parser;
import fruitshop.service.TransactionProcessor;
import fruitshop.service.impl.CsvParser;
import fruitshop.service.impl.ReaderServiceImpl;
import fruitshop.service.impl.ReportImpl;
import fruitshop.service.impl.TransactionProcessorImpl;
import fruitshop.service.impl.WriterServiceImpl;
import java.util.HashMap;
import java.util.Map;

public class Main {
private static final String READ_FILE_NAME = "data.csv";
private static final String WRITE_FILE_NAME = "newData.csv";

public static void main(String[] args) {
Map<Operation, OperationHandler> operationHandlers = new HashMap<>();
OperationStrategyImpl operationStrategy = new OperationStrategyImpl(operationHandlers);

operationHandlers.put(Operation.BALANCE, new BalanceOperationHandlerImpl());
operationHandlers.put(Operation.SUPPLY, new SupplyOperationHandlerImpl());
operationHandlers.put(Operation.PURCHASE, new PurchaseOperationHandlerImpl());
operationHandlers.put(Operation.RETURN, new ReturnOperationHandlerImpl());

ReaderServiceImpl reader = new ReaderServiceImpl();
Parser csvParser = new CsvParser();
TransactionProcessor transactionProcessorImpl =
new TransactionProcessorImpl(operationStrategy);
csvParser.parse(reader.readFromFile(READ_FILE_NAME))
.forEach(transactionProcessorImpl::process);
WriterServiceImpl writer = new WriterServiceImpl();
writer.writeToFile(new ReportImpl().create(), WRITE_FILE_NAME);
}
}
57 changes: 57 additions & 0 deletions src/main/java/fruitshop/model/FruitTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package fruitshop.model;

import java.util.Objects;

public class FruitTransaction {
private final Operation operation;
private final String fruit;
private final int amount;

private FruitTransaction(Operation operation, String fruit, int amount) {
this.operation = operation;
this.fruit = fruit;
this.amount = amount;
}

public static FruitTransaction of(Operation operation, String fruit, int amount) {
if (amount >= 0) {
return new FruitTransaction(operation, fruit, amount);
}

throw new RuntimeException("Amount cannot be negative");
}

public Operation getOperation() {
return operation;
}

public String getFruit() {
return fruit;
}

public int getAmount() {
return amount;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (o == null || getClass() != o.getClass()) {
return false;
}

FruitTransaction that = (FruitTransaction) o;
return amount == that.amount
&& operation
== that.operation
&& Objects.equals(fruit, that.fruit);
}

@Override
public int hashCode() {
return Objects.hash(operation, fruit, amount);
}
}
28 changes: 28 additions & 0 deletions src/main/java/fruitshop/model/Operation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package fruitshop.model;

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 getByCode(String character) {
for (Operation operation : values()) {
if (operation.getOperation().equals(character)) {
return operation;
}
}

throw new RuntimeException("Unknown operation");
}
}
7 changes: 7 additions & 0 deletions src/main/java/fruitshop/operation/OperationHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fruitshop.operation;

import fruitshop.model.FruitTransaction;

public interface OperationHandler {
void accept(FruitTransaction fruitTransaction);
}
7 changes: 7 additions & 0 deletions src/main/java/fruitshop/operation/OperationStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fruitshop.operation;

import fruitshop.model.Operation;

public interface OperationStrategy {
OperationHandler get(Operation operationType);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package fruitshop.operation.impl;

import fruitshop.db.Storage;
import fruitshop.model.FruitTransaction;
import fruitshop.operation.OperationHandler;

public class BalanceOperationHandlerImpl implements OperationHandler {
@Override
public void accept(FruitTransaction fruitTransaction) {
Storage.setAmount(fruitTransaction.getFruit(), fruitTransaction.getAmount());
}
}
19 changes: 19 additions & 0 deletions src/main/java/fruitshop/operation/impl/OperationStrategyImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package fruitshop.operation.impl;

import fruitshop.model.Operation;
import fruitshop.operation.OperationHandler;
import fruitshop.operation.OperationStrategy;
import java.util.Map;

public class OperationStrategyImpl implements OperationStrategy {
private final Map<Operation, OperationHandler> operations;

public OperationStrategyImpl(Map<Operation, OperationHandler> operations) {
this.operations = operations;
}

@Override
public OperationHandler get(Operation operationType) {
return operations.get(operationType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package fruitshop.operation.impl;

import fruitshop.db.Storage;
import fruitshop.model.FruitTransaction;
import fruitshop.operation.OperationHandler;

public class PurchaseOperationHandlerImpl implements OperationHandler {

@Override
public void accept(FruitTransaction fruitTransaction) {
if (Storage.getFruitsAndAmount().get(fruitTransaction.getFruit())
>= fruitTransaction.getAmount()) {
Storage.addFruit(fruitTransaction.getFruit(), -fruitTransaction.getAmount());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package fruitshop.operation.impl;

import fruitshop.db.Storage;
import fruitshop.model.FruitTransaction;
import fruitshop.operation.OperationHandler;

public class ReturnOperationHandlerImpl implements OperationHandler {
@Override
public void accept(FruitTransaction fruitTransaction) {
Storage.addFruit(fruitTransaction.getFruit(), fruitTransaction.getAmount());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package fruitshop.operation.impl;

import fruitshop.db.Storage;
import fruitshop.model.FruitTransaction;
import fruitshop.operation.OperationHandler;

public class SupplyOperationHandlerImpl implements OperationHandler {
@Override
public void accept(FruitTransaction fruitTransaction) {
Storage.addFruit(fruitTransaction.getFruit(), fruitTransaction.getAmount());
}
}
8 changes: 8 additions & 0 deletions src/main/java/fruitshop/service/Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package fruitshop.service;

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

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

import java.util.List;

public interface ReaderService {
List<String> readFromFile(String fromFilePath);
}
5 changes: 5 additions & 0 deletions src/main/java/fruitshop/service/Report.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package fruitshop.service;

public interface Report {
String create();
}
7 changes: 7 additions & 0 deletions src/main/java/fruitshop/service/TransactionProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fruitshop.service;

import fruitshop.model.FruitTransaction;

public interface TransactionProcessor {
void process(FruitTransaction fruitTransaction);
}
5 changes: 5 additions & 0 deletions src/main/java/fruitshop/service/WriterService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package fruitshop.service;

public interface WriterService {
void writeToFile(String data, String outputPath);
}
29 changes: 29 additions & 0 deletions src/main/java/fruitshop/service/impl/CsvParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package fruitshop.service.impl;

import fruitshop.model.FruitTransaction;
import fruitshop.model.Operation;
import fruitshop.service.Parser;
import java.util.ArrayList;
import java.util.List;

public class CsvParser implements Parser {
private static final int OPERATION_INDEX = 0;
private static final int FRUIT_INDEX = 1;
private static final int AMOUNT_INDEX = 2;
private static final String SEPARATOR = ",";

@Override
public List<FruitTransaction> parse(List<String> rowsList) {
List<FruitTransaction> fruitTransactions = new ArrayList<>();
for (int i = 1; i < rowsList.size(); i++) {
String[] values = rowsList.get(i).split(SEPARATOR);
Operation operation = Operation.getByCode(values[OPERATION_INDEX]);
String fruit = values[FRUIT_INDEX];
int amount = Integer.parseInt(values[AMOUNT_INDEX]);

fruitTransactions.add(FruitTransaction.of(operation, fruit, amount));
}

return fruitTransactions;
}
}
18 changes: 18 additions & 0 deletions src/main/java/fruitshop/service/impl/ReaderServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package fruitshop.service.impl;

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

public class ReaderServiceImpl implements ReaderService {
@Override
public List<String> readFromFile(String fromFilePath) {
try {
return Files.readAllLines(Path.of(fromFilePath));
} catch (IOException e) {
throw new RuntimeException("Can't read data from file: " + fromFilePath, e);
}
}
}
29 changes: 29 additions & 0 deletions src/main/java/fruitshop/service/impl/ReportImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package fruitshop.service.impl;

import fruitshop.db.Storage;
import fruitshop.service.Report;
import java.util.Map;

public class ReportImpl implements Report {
private static final String SEPARATOR = ",";
private static final String HEADER = "fruit,quantity";

@Override
public String create() {
StringBuilder reportBuilder = new StringBuilder();

reportBuilder
.append(HEADER)
.append(System.lineSeparator());

for (Map.Entry<String, Integer> entry : Storage.getFruitsAndAmount().entrySet()) {
reportBuilder
.append(entry.getKey())
.append(SEPARATOR)
.append(entry.getValue())
.append(System.lineSeparator());
}

return reportBuilder.toString();
}
}
Loading
Loading