Skip to content

Commit

Permalink
realized all classes
Browse files Browse the repository at this point in the history
  • Loading branch information
fedorovychh committed Nov 3, 2023
1 parent 084c6b7 commit 1054c4d
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 40 deletions.
12 changes: 7 additions & 5 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@
import java.util.List;

public class Main {
private static final String INPUT_FILE_NAME = "input.csv";
private static final String REPORT_FILE_NAME = "report.csv";
private static final String INPUT_FILE_NAME = "src\\main\\resources\\input.csv";
private static final String REPORT_FILE_NAME = "src\\main\\resources\\report.csv";

public static void main(String[] args) {
FileReaderService fileReaderService = new FileReaderServiceImpl();
String inputData = fileReaderService.readInputData(INPUT_FILE_NAME);

List<StorageUpdateHandler> storageUpdateHandlers = new ArrayList<>();
storageUpdateHandlers.add(new FruitBalanceHandler());
storageUpdateHandlers.add(new FruitSupplyHandler());
storageUpdateHandlers.add(new FruitPurchaseHandler());
storageUpdateHandlers.add(new FruitReturnHandler());

FileReaderService fileReaderService = new FileReaderServiceImpl();
ReportWriterService reportWriterService = new ReportWriterServiceImpl();
DataHandlerService dataHandlerService = new DataHandlerServiceImpl(storageUpdateHandlers);
String inputData = fileReaderService.readInputData(INPUT_FILE_NAME);
String reportData = dataHandlerService.calculateInputData(inputData);

ReportWriterService reportWriterService = new ReportWriterServiceImpl();
reportWriterService.writeReport(reportData, REPORT_FILE_NAME);
}
}
5 changes: 2 additions & 3 deletions src/main/java/core/basesyntax/dao/FruitDao.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package core.basesyntax.dao;

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

public interface FruitDao {
Expand All @@ -10,9 +9,9 @@ public interface FruitDao {

Integer get(String fruitName);

Map<Fruit, Integer> getAll();
Map<String, Integer> getAll();

void remove(String fruitName, int amount);
void reduce(String fruitName, int amount);

void removeAll();

Expand Down
25 changes: 9 additions & 16 deletions src/main/java/core/basesyntax/dao/impl/FruitDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,20 @@

import core.basesyntax.dao.FruitDao;
import core.basesyntax.db.FruitStorage;
import core.basesyntax.model.Fruit;
import java.util.Map;

public class FruitDaoImpl implements FruitDao {
private static final String NULL_INPUT_MESSAGE = "Invalid operation. "
+ "Input data should not be null!";
private static final String ADD_FIRST_FAILURE_MESSAGE = "Product {%s} "
+ "already exist in storage!";
private static final String ADDING_FAILURE_MESSAGE = "Product {%s} "
+ "does not exist in storage!";
private static final int GET_FAILURE_INDEX = -1;

@Override
public void addFirst(String fruitName, int amount) {
FruitStorage.goods.put(new Fruit(fruitName), amount);
FruitStorage.goods.put(fruitName, amount);
}

@Override
public void add(String fruitName, int amount) {
for (Map.Entry<Fruit, Integer> good : FruitStorage.goods.entrySet()) {
if (fruitName.equals(good.getKey().getProductName())) {
for (Map.Entry<String, Integer> good : FruitStorage.goods.entrySet()) {
if (fruitName.equals(good.getKey())) {
good.setValue(good.getValue() + amount);
break;
}
Expand All @@ -31,23 +24,23 @@ public void add(String fruitName, int amount) {

@Override
public Integer get(String fruitName) {
for (Map.Entry<Fruit, Integer> good : FruitStorage.goods.entrySet()) {
if (fruitName.equals(good.getKey().getProductName())) {
for (Map.Entry<String, Integer> good : FruitStorage.goods.entrySet()) {
if (fruitName.equals(good.getKey())) {
return good.getValue();
}
}
return GET_FAILURE_INDEX;
}

@Override
public Map<Fruit, Integer> getAll() {
public Map<String, Integer> getAll() {
return FruitStorage.goods;
}

@Override
public void remove(String fruitName, int amount) {
for (Map.Entry<Fruit, Integer> good : FruitStorage.goods.entrySet()) {
if (fruitName.equals(good.getKey().getProductName())) {
public void reduce(String fruitName, int amount) {
for (Map.Entry<String, Integer> good : FruitStorage.goods.entrySet()) {
if (fruitName.equals(good.getKey())) {
good.setValue(good.getValue() - amount);
break;
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/core/basesyntax/db/FruitStorage.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package core.basesyntax.db;

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

public class FruitStorage {
public static final Map<Fruit, Integer> goods = new HashMap<>();
public static final Map<String, Integer> goods = new HashMap<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ private String getFinalReport() {
StringBuilder reportBuilder = new StringBuilder(REPORT_COLUMNS);
fruitDao.getAll().forEach((key, value) ->
reportBuilder.append(System.lineSeparator())
.append(key.getProductName())
.append(key)
.append(SEPARATE_SYMBOL)
.append(value));
return reportBuilder.toString();
return reportBuilder + System.lineSeparator();
}

private void addProductTransaction(String productTransaction) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void updateStorage(String fruitName, int amount) {
throw new RuntimeException(NULL_INPUT_MESSAGE);
}
checkAbilityToUpdate(fruitName, amount);
fruitDao.remove(fruitName, amount);
fruitDao.reduce(fruitName, amount);
}

@Override
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/input.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
3 changes: 3 additions & 0 deletions src/main/resources/report.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fruit,quantity
banana,152
apple,90
2 changes: 1 addition & 1 deletion src/test/java/core/basesyntax/dao/FruitDaoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void getAll_correctInputData_Ok() {

@Test
public void remove_correctInputData_Ok() {
fruitDao.remove(DEFAULT_FRUIT_NAME, DEFAULT_FRUIT_AMOUNT);
fruitDao.reduce(DEFAULT_FRUIT_NAME, DEFAULT_FRUIT_AMOUNT);
int expected = EMPTY_SIZE;
Assertions.assertTrue(expected == fruitDao.get(DEFAULT_FRUIT_NAME));
}
Expand Down
20 changes: 12 additions & 8 deletions src/test/java/core/basesyntax/service/DataHandlerServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.junit.jupiter.api.Test;

class DataHandlerServiceTest {
private static final String EMPY_STRING = "";
private static DataHandlerService dataHandlerService;

@BeforeAll
Expand All @@ -22,11 +23,12 @@ public static void setUp() {
storageUpdateHandlers.add(new FruitSupplyHandler());
storageUpdateHandlers.add(new FruitPurchaseHandler());
storageUpdateHandlers.add(new FruitReturnHandler());

dataHandlerService = new DataHandlerServiceImpl(storageUpdateHandlers);
}

@Test
public void calculate_CorrectInputData_Ok() {
public void calculate_correctInputData_Ok() {
StringBuilder inputStringBuilder = new StringBuilder("type,fruit,quantity");
inputStringBuilder.append(System.lineSeparator()).append("b,banana,20")
.append(System.lineSeparator()).append("b,apple,100")
Expand All @@ -35,13 +37,16 @@ public void calculate_CorrectInputData_Ok() {
.append(System.lineSeparator()).append("r,apple,10")
.append(System.lineSeparator()).append("p,apple,20")
.append(System.lineSeparator()).append("p,banana,5")
.append(System.lineSeparator()).append("s,banana,50");
.append(System.lineSeparator()).append("s,banana,50")
.append(System.lineSeparator());
String inputString = inputStringBuilder.toString();
StringBuilder reportStringBuilder = new StringBuilder("fruit,quantity");
reportStringBuilder.append(System.lineSeparator()).append("apple,90")
.append(System.lineSeparator()).append("banana,152");
reportStringBuilder.append(System.lineSeparator()).append("banana,152")
.append(System.lineSeparator()).append("apple,90")
.append(System.lineSeparator());
String expected = reportStringBuilder.toString();
Assertions.assertEquals(expected, dataHandlerService.calculateInputData(inputString));
String actual = dataHandlerService.calculateInputData(inputString);
Assertions.assertEquals(expected, actual);
}

@Test
Expand All @@ -52,8 +57,7 @@ public void calculate_nullInputData_notOk() {

@Test
public void calculate_emptyInput_Ok() {
String emptyString = "";
String actual = dataHandlerService.calculateInputData(emptyString);
Assertions.assertEquals(emptyString, actual);
String actual = dataHandlerService.calculateInputData(EMPY_STRING);
Assertions.assertEquals(EMPY_STRING, actual);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.junit.jupiter.api.Test;

public class FileReaderServiceTest {
private static final String CORRECT_INPUT_FILE_NAME = "input.csv";
private static final String CORRECT_INPUT_FILE_NAME = "src\\main\\resources\\input.csv";
private static final String WRONG_INPUT_FILE_NAME = "53hsd59.csv";
private static FileReaderService fileReaderService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private String getFinalReport() {
StringBuilder reportBuilder = new StringBuilder(REPORT_COLUMNS);
fruitDao.getAll().forEach((key, value) ->
reportBuilder.append(System.lineSeparator())
.append(key.getProductName())
.append(key)
.append(SEPARATE_SYMBOL)
.append(value));
return reportBuilder + System.lineSeparator();
Expand Down

0 comments on commit 1054c4d

Please sign in to comment.