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

Write tests with more than 80% coverage #834

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

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

import core.basesyntax.db.dao.StorageDao;
import core.basesyntax.db.dao.StorageDaoImp;
import core.basesyntax.model.GoodsOperation;
import core.basesyntax.service.CsvParseService;
import core.basesyntax.service.FileReadService;
import core.basesyntax.service.FileWriteService;
import core.basesyntax.service.ReportService;
import core.basesyntax.service.imp.CsvParser;
import core.basesyntax.service.imp.FileReadServiceImp;
import core.basesyntax.service.imp.FileWriteServiceImp;
import core.basesyntax.service.imp.Reporter;
import core.basesyntax.strategy.OperationHandler;
import core.basesyntax.strategy.OperationsStrategy;
import core.basesyntax.strategy.handlers.BalanceOperationHandler;
import core.basesyntax.strategy.handlers.PurchaseOperationHandler;
import core.basesyntax.strategy.handlers.ReturnOperationHandler;
import core.basesyntax.strategy.handlers.SupplyOperationHandler;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
private static final String NAME_SOURCE_FILE = "SourceFile.txt";
private static final String PATH_SOURCE_FILE = "src/main/resources/" + NAME_SOURCE_FILE;
private static final String NAME_CONSUMER_FILE = "ConsumerFile.txt";
private static final String PATH_CONSUMER_FILE = "src/main/resources/" + NAME_CONSUMER_FILE;
private static final Map<String, Integer> storage;
private static final FileReadService fileReadService;
private static final CsvParseService csvParseService;
private static final Map<GoodsOperation.TransactionType, OperationHandler> strategies;
private static final StorageDao storageDao;
private static final OperationsStrategy operationsStrategy;
private static final ReportService reportService;
private static final FileWriteService fileWriteService;

static {
storage = new HashMap<>();
storageDao = new StorageDaoImp(storage);
fileReadService = new FileReadServiceImp();
csvParseService = new CsvParser();
strategies = Map.of(GoodsOperation.TransactionType.BALANCE,
new BalanceOperationHandler(storageDao),
GoodsOperation.TransactionType.PURCHASE,
new PurchaseOperationHandler(storageDao),
GoodsOperation.TransactionType.RETURN,
new ReturnOperationHandler(storageDao),
GoodsOperation.TransactionType.SUPPLY,
new SupplyOperationHandler(storageDao));
operationsStrategy = new OperationsStrategy(strategies);
reportService = new Reporter(storageDao);
fileWriteService = new FileWriteServiceImp();
}

public static void main(String[] args) {
List<String> parsedLines = fileReadService.readFilesLines(PATH_SOURCE_FILE);
List<GoodsOperation> operationsList = csvParseService
.listOperationsFromCsv(parsedLines);
for (GoodsOperation operation : operationsList) {
operationsStrategy.handleOperation(operation);
}
List<String> goodsStockCsvReport = reportService.getGoodsStockCsv();
fileWriteService.writeCsvToFile(goodsStockCsvReport, PATH_CONSUMER_FILE);
}
}
11 changes: 11 additions & 0 deletions src/main/java/core/basesyntax/db/dao/StorageDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package core.basesyntax.db.dao;

import java.util.Map;

public interface StorageDao {
Integer getQuantityGoods(String goods);

void setQuantityGoods(String goods, Integer newQuantity);

Map<String, Integer> getStock();
}
27 changes: 27 additions & 0 deletions src/main/java/core/basesyntax/db/dao/StorageDaoImp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package core.basesyntax.db.dao;

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

public class StorageDaoImp implements StorageDao {
private final Map<String, Integer> storage;

public StorageDaoImp(Map<String, Integer> storage) {
this.storage = storage;
}

@Override
public Integer getQuantityGoods(String goods) {
return storage.get(goods);
}

@Override
public void setQuantityGoods(String goods, Integer newQuantity) {
storage.put(goods, newQuantity);
}

@Override
public Map<String, Integer> getStock() {
return new HashMap<>(storage);
}
}
73 changes: 73 additions & 0 deletions src/main/java/core/basesyntax/model/GoodsOperation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package core.basesyntax.model;

import java.util.Objects;

public class GoodsOperation {
private final TransactionType transactionType;
private final String item;
private final int quantity;

public GoodsOperation(TransactionType transactionType, String item, int quantity) {
this.transactionType = transactionType;
this.item = item;
this.quantity = quantity;
}

public String getItem() {
return item;
}

public TransactionType getTransactionType() {
return transactionType;
}

public int getQuantity() {
return quantity;
}

public enum TransactionType {
BALANCE("b"),
SUPPLY("s"),
PURCHASE("p"),
RETURN("r");
private final String typesCode;

TransactionType(String code) {
this.typesCode = code;
}

public static TransactionType getByCode(String code) {
for (TransactionType type : TransactionType.values()) {
if (type.typesCode.equals(code)) {
return type;
}
}
throw new RuntimeException("Can't find operation type: " + code);
}
}

@Override
public boolean equals(Object operation) {
if (operation == null) {
return false;
}
if (operation == this) {
return true;
}
if (operation.getClass().equals(GoodsOperation.class)) {
GoodsOperation current = (GoodsOperation) operation;
return (transactionType == current.transactionType)
&& (Objects.equals(item, current.item))
&& (quantity == current.quantity);
}
return false;
}

@Override
public int hashCode() {
int result = transactionType != null ? transactionType.hashCode() : 0;
result = 31 * result + (item != null ? item.hashCode() : 0);
result = 31 * result + quantity;
return result;
}
}
10 changes: 10 additions & 0 deletions src/main/java/core/basesyntax/service/CsvParseService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package core.basesyntax.service;

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

public interface CsvParseService {
List<GoodsOperation> listOperationsFromCsv(List<String> csvOperations);

GoodsOperation convertToOperationFromCsv(String operation);
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/service/FileReadService.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 FileReadService {
List<String> readFilesLines(String path);
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/service/FileWriteService.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 FileWriteService {
void writeCsvToFile(List<String> text, String path);
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/service/ReportService.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 ReportService {
List<String> getGoodsStockCsv();
}
40 changes: 40 additions & 0 deletions src/main/java/core/basesyntax/service/imp/CsvParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package core.basesyntax.service.imp;

import core.basesyntax.model.GoodsOperation;
import core.basesyntax.service.CsvParseService;
import java.util.ArrayList;
import java.util.List;

public class CsvParser implements CsvParseService {
private static final String LINE_DIVIDER = ",";
private static final int OPERATION_TYPE_INDEX = 0;
private static final int OPERATION_ITEM_INDEX = 1;
private static final int OPERATION_QUANTITY_INDEX = 2;
private static final String CSV_COLUMN_NAMES_PATTERN = "type,fruit,quantity";

@Override
public List<GoodsOperation> listOperationsFromCsv(List<String> csvOperationsList) {
List<GoodsOperation> operationsList = new ArrayList<>();
int startLine = 0;
String firstLine = csvOperationsList.get(0);
if (firstLine.contains(CSV_COLUMN_NAMES_PATTERN)) {
startLine = 1;
}
for (int i = startLine; i < csvOperationsList.size(); i++) {
String csvOperation = csvOperationsList.get(i);
GoodsOperation operation = convertToOperationFromCsv(csvOperation);
operationsList.add(operation);
}
return operationsList;
}

@Override
public GoodsOperation convertToOperationFromCsv(String operation) {
String [] line = operation.split(LINE_DIVIDER);
GoodsOperation.TransactionType type = GoodsOperation.TransactionType
.getByCode(line[OPERATION_TYPE_INDEX]);
String item = line[OPERATION_ITEM_INDEX];
int quantity = Integer.parseInt(line[OPERATION_QUANTITY_INDEX]);
return new GoodsOperation(type, item, quantity);
}
}
27 changes: 27 additions & 0 deletions src/main/java/core/basesyntax/service/imp/FileReadServiceImp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package core.basesyntax.service.imp;

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

public class FileReadServiceImp implements FileReadService {
@Override
public List<String> readFilesLines(String path) {
File sourseFile = new File(path);
List<String> filesLines = new ArrayList<>();
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(sourseFile))) {
String line = bufferedReader.readLine();
while (line != null) {
filesLines.add(line);
line = bufferedReader.readLine();
}
} catch (IOException e) {
throw new RuntimeException("Can't read file " + sourseFile, e);
}
return filesLines;
}
}
29 changes: 29 additions & 0 deletions src/main/java/core/basesyntax/service/imp/FileWriteServiceImp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package core.basesyntax.service.imp;

import core.basesyntax.service.FileWriteService;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class FileWriteServiceImp implements FileWriteService {
@Override
public void writeCsvToFile(List<String> text, String path) {
checkValidText(text);
File file = new File(path);
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file))) {
for (String str : text) {
bufferedWriter.write(str);
}
} catch (IOException e) {
throw new RuntimeException("Can't write file" + path, e);
}
}

private void checkValidText(List<String> text) {
if (text == null) {
throw new NullPointerException("Can't write null data to file");
}
}
}
35 changes: 35 additions & 0 deletions src/main/java/core/basesyntax/service/imp/Reporter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package core.basesyntax.service.imp;

import core.basesyntax.db.dao.StorageDao;
import core.basesyntax.service.ReportService;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Reporter implements ReportService {
private static final String REPORT_HEAD = "fruit,quantity";
private static final String SEPARATE_SYMBOL = ",";
private static final String LINE_SEPARATOR = System.lineSeparator();
private final StorageDao storageDao;

public Reporter(StorageDao storageDao) {
this.storageDao = storageDao;
}

@Override
public List<String> getGoodsStockCsv() {
Set<Map.Entry<String, Integer>> goodsStock = storageDao.getStock().entrySet();
List<String> report = new ArrayList<>();
report.add(REPORT_HEAD + LINE_SEPARATOR);
for (Map.Entry<String, Integer> stock : goodsStock) {
StringBuilder line = new StringBuilder()
.append(stock.getKey())
.append(SEPARATE_SYMBOL)
.append(stock.getValue())
.append(LINE_SEPARATOR);
report.add(line.toString());
}
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.GoodsOperation;

public interface OperationHandler {
void handleOperation(GoodsOperation operation);
}
16 changes: 16 additions & 0 deletions src/main/java/core/basesyntax/strategy/OperationsStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package core.basesyntax.strategy;

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

public class OperationsStrategy {
private final Map<GoodsOperation.TransactionType, OperationHandler> strategies;

public OperationsStrategy(Map<GoodsOperation.TransactionType, OperationHandler> strategies) {
this.strategies = strategies;
}

public void handleOperation(GoodsOperation operation) {
strategies.get(operation.getTransactionType()).handleOperation(operation);
}
}
Loading
Loading