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

added test for service #836

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,4 @@
</plugins>
</pluginManagement>
</build>
</project>
</project>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't commit this change, it's redundant

7 changes: 0 additions & 7 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

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

import core.basesyntax.dao.FruitDao;
import core.basesyntax.dao.FruitDaoImp;
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.model.Operation;
import core.basesyntax.service.Parser;
import core.basesyntax.service.Producer;
import core.basesyntax.service.Reader;
import core.basesyntax.service.Writer;
import core.basesyntax.service.impl.FruitTransactionParser;
import core.basesyntax.service.impl.ProduceMapReportService;
import core.basesyntax.service.impl.ReadFromCsvService;
import core.basesyntax.service.impl.WriteCsvService;
import core.basesyntax.strategy.OperationStrategy;
import core.basesyntax.strategy.OperationStrategyImp;
import core.basesyntax.strategy.handler.BalanceHandler;
import core.basesyntax.strategy.handler.OperationHandler;
import core.basesyntax.strategy.handler.PurchaseHandler;
import core.basesyntax.strategy.handler.ReturnHandler;
import core.basesyntax.strategy.handler.SupplyHandler;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {

private static final String FILE_FROM = "src/main/resources/csvFile.csv";
private static final String FILE_TO = "src/main/resources/Report.csv";

public static void main(String[] args) {
Map<Operation, OperationHandler> operationHandlerMap = new HashMap<>();
Reader reader = new ReadFromCsvService();
FruitDao fruitDao = new FruitDaoImp();
initializeOperationHandlers(operationHandlerMap, fruitDao);
OperationStrategy operationStrategy = new OperationStrategyImp(operationHandlerMap);
List<String> fileData = reader.read(FILE_FROM);
List<FruitTransaction> fruitTransactions;
Parser parser = new FruitTransactionParser();
fruitTransactions = parser.parse(fileData);
fruitDao.add(fruitTransactions);
operationStrategy.getHandle(fruitTransactions);
Writer writer = new WriteCsvService();
Producer producer = new ProduceMapReportService();
writer.write(producer.produceReport(fruitDao.getStorage()), FILE_TO);
}

private static void initializeOperationHandlers(Map<Operation,
OperationHandler> handlerMap, FruitDao fruitDao) {
handlerMap.put(Operation.BALANCE, new BalanceHandler(fruitDao));
handlerMap.put(Operation.PURCHASE, new PurchaseHandler(fruitDao));
handlerMap.put(Operation.RETURN, new ReturnHandler(fruitDao));
handlerMap.put(Operation.SUPPLY, new SupplyHandler(fruitDao));
}
}
15 changes: 15 additions & 0 deletions src/main/java/core/basesyntax/dao/FruitDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package core.basesyntax.dao;

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

public interface FruitDao {
Map<String, Integer> getStorage();

Integer get(String key);

void add(String type, Integer quantity);

void add(List<FruitTransaction> list);
}
31 changes: 31 additions & 0 deletions src/main/java/core/basesyntax/dao/FruitDaoImp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package core.basesyntax.dao;

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

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

public class FruitDaoImp implements FruitDao {
public Map<String, Integer> getStorage() {
return STORAGE;
}

public void add(String type, Integer quantity) {
STORAGE.put(type, quantity);
}

@Override
public void add(List<FruitTransaction> list) {
for (FruitTransaction item : list) {
STORAGE.put(item.getFruit(), 0);
}
}

public Integer get(String key) {
if (STORAGE.get(key) == null) {
throw new IllegalArgumentException("Can't get non-existent key your key is " + key);
}
return STORAGE.get(key);
}
}
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 java.util.HashMap;
import java.util.Map;

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

}
55 changes: 55 additions & 0 deletions src/main/java/core/basesyntax/model/FruitTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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 fruit, int quantity) {
this.operation = operation;
this.fruit = fruit;
this.quantity = quantity;
}

public Operation getOperation() {
return operation;
}

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

FruitTransaction that = (FruitTransaction) o;

if (quantity != that.quantity) {
return false;
}
if (operation != that.operation) {
return false;
}
return Objects.equals(fruit, that.fruit);
}

@Override
public int hashCode() {
int result = operation.hashCode();
result = 31 * result + (fruit != null ? fruit.hashCode() : 0);
result = 31 * result + quantity;
return result;
}

public String getFruit() {
return fruit;
}

public int getQuantity() {
return quantity;
}
}
18 changes: 18 additions & 0 deletions src/main/java/core/basesyntax/model/Operation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package core.basesyntax.model;

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

private String code;

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

public String getCode() {
return code;
}
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/service/Parser.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 Parser {
List<FruitTransaction> parse(List<String> fileData);
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/service/Producer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.service;

import java.util.Map;

public interface Producer {
String produceReport(Map<String, Integer> map);
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/service/Reader.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 Reader {
List<String> read(String path);
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/service/Writer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax.service;

public interface Writer {
boolean write(String content, String path);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package core.basesyntax.service.impl;

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

public class FruitTransactionParser implements Parser {
private static final String SPLITER = ",";
private static final int INDEX_OF_OPERATION = 0;
private static final int INDEX_OF_FRUIT_NAME = 1;
private static final int INDEX_OF_QUANTITY = 2;

@Override
public List<FruitTransaction> parse(List<String> fileData) {
if (fileData == null) {
throw new IllegalArgumentException("Can't parse this list because list is null");
}
List<FruitTransaction> fruits = new ArrayList<>();
for (String line : fileData) {
String[] splitedLine = line.split(SPLITER);
if (splitedLine.length > 3 || splitedLine.length <= 1) {
throw new IllegalArgumentException("Can't parse this list");
}
for (Operation operation : Operation.values()) {
if (operation.getCode().equalsIgnoreCase(splitedLine[INDEX_OF_OPERATION])) {
fruits.add(new FruitTransaction(operation,
splitedLine[INDEX_OF_FRUIT_NAME],
Integer.parseInt(splitedLine[INDEX_OF_QUANTITY])));
}
}
}
return fruits;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package core.basesyntax.service.impl;

import core.basesyntax.service.Producer;
import java.util.Map;

public class ProduceMapReportService implements Producer {
private static final String NEW_LINE = System.lineSeparator();
private static final String COMA = ",";

@Override
public String produceReport(Map<String, Integer> map) {
if (map == null) {
throw new IllegalArgumentException("can't produce this map because map is null");
}
StringBuilder report = new StringBuilder();
int size = map.size();
int index = 0;
for (Map.Entry<String, Integer> entry : map.entrySet()) {
if (entry.getValue() < 0) {
throw new IllegalArgumentException("can't produce this map integer value below 0");
}
report.append(entry.getKey())
.append(COMA)
.append(entry.getValue());
if (index < size - 1) {
report.append(NEW_LINE);
}
index++;
}
return report.toString();
}
}
28 changes: 28 additions & 0 deletions src/main/java/core/basesyntax/service/impl/ReadFromCsvService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package core.basesyntax.service.impl;

import core.basesyntax.service.Reader;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ReadFromCsvService implements Reader {
private static final int INDEX_OF_TITLE = 0;
private static final String CANT_READ_EXCEPTION_MESSAGE = "can't read file";

@Override
public List<String> read(String path) {
List<String> data = new ArrayList<>();
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(path))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
data.add(line.trim());
}
} catch (IOException e) {
throw new RuntimeException(CANT_READ_EXCEPTION_MESSAGE + path,e);
}
data.remove(INDEX_OF_TITLE);
return data;
}
}
29 changes: 29 additions & 0 deletions src/main/java/core/basesyntax/service/impl/WriteCsvService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package core.basesyntax.service.impl;

import core.basesyntax.service.Writer;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class WriteCsvService implements Writer {
private static final String TITLE_TEXT = "fruit,quantity";
private static final String RUNTIME_EXCEPTION_MESSAGE = "Can't write file ";

@Override
public boolean write(String content, String path) {
if (!path.endsWith(".csv")) {
throw new IllegalArgumentException("Csv writer write only csv file");
}
try {
BufferedWriter bufferedWriter = new BufferedWriter(
new FileWriter(path));
bufferedWriter.write(TITLE_TEXT);
bufferedWriter.newLine();
bufferedWriter.write(content);
bufferedWriter.close();
} catch (IOException e) {
throw new RuntimeException(RUNTIME_EXCEPTION_MESSAGE + path, e);
}
return true;
}
}
9 changes: 9 additions & 0 deletions src/main/java/core/basesyntax/strategy/OperationStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package core.basesyntax.strategy;

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

public interface OperationStrategy {
boolean getHandle(List<FruitTransaction> fruitTransactions);

}
Loading
Loading