Skip to content

Commit

Permalink
Added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HovorukhaBohdan committed Oct 26, 2023
1 parent 3348402 commit 7aa30d9
Show file tree
Hide file tree
Showing 28 changed files with 595 additions and 78 deletions.
3 changes: 3 additions & 0 deletions fruitsWithLessThenZeroQuantity.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type,fruit,quantity
b,apple,-10
b,banana,-10
40 changes: 25 additions & 15 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
package core.basesyntax;

import core.basesyntax.service.CreateReportService;
import core.basesyntax.service.DataConvertService;
import core.basesyntax.service.DataProcessService;
import core.basesyntax.service.ReadFromCsvFileService;
import core.basesyntax.service.WriteToCsvFileService;
import core.basesyntax.service.impl.CreateReportServiceImpl;
import core.basesyntax.service.impl.DataConvertServiceImpl;
import core.basesyntax.service.impl.DataProcessServiceImpl;
import core.basesyntax.service.impl.ReadFromCsvFileServiceImpl;
import core.basesyntax.service.impl.WriteToCsvFileServiceImpl;
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.model.Operation;
import core.basesyntax.service.*;
import core.basesyntax.service.impl.*;
import core.basesyntax.strategy.OperationHandler;
import core.basesyntax.strategy.impl.BalanceOperationHandlerImpl;
import core.basesyntax.strategy.impl.PurchaseOperationHandlerImpl;
import core.basesyntax.strategy.impl.ReturnOperationHandlerImpl;
import core.basesyntax.strategy.impl.SupplyOperationHandlerImpl;

import java.util.List;
import java.util.Map;

public class Main {
private static final String FIRST_TEST_CSV = "fruits1.csv";
private static final String SECOND_TEST_CSV = "fruits2.csv";
private static final String THIRD_TEST_CSV = "fruits3.csv";
private static final String REPORT_FILENAME = "report.csv";
private static final Map<Operation, OperationHandler> operationPicker =
Map.of(Operation.BALANCE, new BalanceOperationHandlerImpl(),
Operation.PURCHASE, new PurchaseOperationHandlerImpl(),
Operation.RETURN, new ReturnOperationHandlerImpl(),
Operation.SUPPLY, new SupplyOperationHandlerImpl());

public static void main(String[] args) {
ReadFromCsvFileService reader = new ReadFromCsvFileServiceImpl();
DataConvertService convertor = new DataConvertServiceImpl();
DataProcessService processor = new DataProcessServiceImpl();
DataProcessService processor = new DataProcessServiceImpl(operationPicker);
CreateReportService reportCreator = new CreateReportServiceImpl();
WriteToCsvFileService writer = new WriteToCsvFileServiceImpl();
WriteToCsvFileService writer = new WriteToCsvFileServiceImpl(REPORT_FILENAME);

List<FruitTransaction> convertedFruitsFromFile =
convertor.convert(reader.readFile(THIRD_TEST_CSV));

processor.processFruits(convertedFruitsFromFile);

processor.processFruits(convertor.convert(reader.readFile(THIRD_TEST_CSV)));
writer.write(reportCreator.createReport());
}
}
1 change: 1 addition & 0 deletions src/main/java/core/basesyntax/db/dao/StorageDaoImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core.basesyntax.db.dao;

import core.basesyntax.db.Storage;

import java.util.Map;

public class StorageDaoImpl implements StorageDao {
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/core/basesyntax/model/Operation.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ public enum Operation {
this.code = code;
}

public static Operation findByCode(String code) {
for (Operation operation : values()) {
if (operation.code.equals(code)) {
return operation;
}
}
public String getCode() {
return code;
}

throw new RuntimeException("Code is not exist " + code);
public static Operation findByCode(String code) {
return switch (code) {
case "b" -> BALANCE;
case "s" -> SUPPLY;
case "p" -> PURCHASE;
case "r" -> RETURN;
default -> throw new RuntimeException("Code is not exist " + code);
};
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core.basesyntax.service;

import core.basesyntax.model.FruitTransaction;

import java.util.List;

public interface DataConvertService {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core.basesyntax.service;

import core.basesyntax.model.FruitTransaction;

import java.util.List;

public interface DataProcessService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import core.basesyntax.db.dao.StorageDao;
import core.basesyntax.db.dao.StorageDaoImpl;
import core.basesyntax.service.CreateReportService;

import java.util.Map;

public class CreateReportServiceImpl implements CreateReportService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.model.Operation;
import core.basesyntax.service.DataConvertService;

import java.util.ArrayList;
import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,49 @@
package core.basesyntax.service.impl;

import core.basesyntax.db.dao.StorageDao;
import core.basesyntax.db.dao.StorageDaoImpl;
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.model.Operation;
import core.basesyntax.service.DataProcessService;
import core.basesyntax.strategy.FruitOperationStrategy;
import core.basesyntax.strategy.OperationHandler;
import core.basesyntax.strategy.impl.BalanceOperationHandlerImpl;
import core.basesyntax.strategy.impl.FruitOperationStrategyImpl;
import core.basesyntax.strategy.impl.PurchaseOperationHandlerImpl;
import core.basesyntax.strategy.impl.ReturnOperationHandlerImpl;
import core.basesyntax.strategy.impl.SupplyOperationHandlerImpl;

import java.util.List;
import java.util.Map;

public class DataProcessServiceImpl implements DataProcessService {
private final Map<Operation, OperationHandler> operationPicker;
private final StorageDao storageDao;

public DataProcessServiceImpl(Map<Operation, OperationHandler> operationPicker) {
this.operationPicker = operationPicker;
storageDao = new StorageDaoImpl();
}

@Override
public void processFruits(List<FruitTransaction> fruits) {
Map<Operation, OperationHandler> operationPicker = Map.of(Operation.BALANCE,
new BalanceOperationHandlerImpl(),
Operation.PURCHASE,
new PurchaseOperationHandlerImpl(),
Operation.RETURN,
new ReturnOperationHandlerImpl(),
Operation.SUPPLY,
new SupplyOperationHandlerImpl());
List<String> fruitNames = fruits.stream()
.map(FruitTransaction::getFruitName)
.distinct()
.toList();

FruitOperationStrategy fruitOperationStrategy =
new FruitOperationStrategyImpl(operationPicker);

fruitOperationStrategy.countFruits(fruits);
for (String fruitName : fruitNames) {
int fruitCount = 0;
for (FruitTransaction fruitTransaction : fruits) {
if (fruitTransaction.getFruitName().equals(fruitName)) {
fruitCount += fruitOperationStrategy.countFruits(fruitTransaction);
}
}

if (fruitCount < 0) {
throw new RuntimeException("Cannot be less then 0: " + fruitCount);
}

storageDao.add(fruitName, fruitCount);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package core.basesyntax.service.impl;

import core.basesyntax.service.ReadFromCsvFileService;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ReadFromCsvFileServiceImpl implements ReadFromCsvFileService {

@Override
public List<String> readFile(String fromFileName) {
List<String> fruits = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
package core.basesyntax.service.impl;

import core.basesyntax.service.WriteToCsvFileService;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class WriteToCsvFileServiceImpl implements WriteToCsvFileService {
private static final String FILE_NAME = "report.csv";
private final String fileName;

public WriteToCsvFileServiceImpl(String fileName) {
this.fileName = fileName;
}

@Override
public void write(String report) {
File file = new File(FILE_NAME);
File file = new File(fileName);
try {
file.createNewFile();
} catch (IOException e) {
throw new RuntimeException("Cannot create a file ", e);
}

try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_NAME))) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
writer.write(report);
} catch (IOException e) {
throw new RuntimeException("Cannot write data to file", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package core.basesyntax.strategy;

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

public interface FruitOperationStrategy {
void countFruits(List<FruitTransaction> fruitTransactions);
int countFruits(FruitTransaction fruitTransactions);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
public class BalanceOperationHandlerImpl implements OperationHandler {
@Override
public int count(FruitTransaction fruitTransaction) {
return fruitTransaction.getQuantity();
int quantity = fruitTransaction.getQuantity();

if (quantity < 0) {
throw new RuntimeException("Quantity cannot be less then zero: " + quantity);
}

return quantity;
}
}
Original file line number Diff line number Diff line change
@@ -1,43 +1,22 @@
package core.basesyntax.strategy.impl;

import core.basesyntax.db.dao.StorageDao;
import core.basesyntax.db.dao.StorageDaoImpl;
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.model.Operation;
import core.basesyntax.strategy.FruitOperationStrategy;
import core.basesyntax.strategy.OperationHandler;
import java.util.List;

import java.util.Map;

public class FruitOperationStrategyImpl implements FruitOperationStrategy {
private Map<Operation, OperationHandler> handlerOperationMap;
private final StorageDao storageDao = new StorageDaoImpl();
private final Map<Operation, OperationHandler> handlerOperationMap;

public FruitOperationStrategyImpl(Map<Operation, OperationHandler> handlerOperationMap) {
this.handlerOperationMap = handlerOperationMap;
}

@Override
public void countFruits(List<FruitTransaction> fruitTransactions) {
List<String> fruitNames = fruitTransactions.stream()
.map(FruitTransaction::getFruitName)
.distinct()
.toList();

for (String fruitName : fruitNames) {
int fruitCount = 0;
for (FruitTransaction fruitTransaction : fruitTransactions) {
if (fruitTransaction.getFruitName().equals(fruitName)) {
fruitCount += handlerOperationMap.get(fruitTransaction.getOperation())
.count(fruitTransaction);
}
}

if (fruitCount < 0) {
throw new RuntimeException("Cannot be less then 0: " + fruitCount);
}

storageDao.add(fruitName, fruitCount);
}
public int countFruits(FruitTransaction fruitTransaction) {
return handlerOperationMap.get(fruitTransaction.getOperation())
.count(fruitTransaction);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
public class PurchaseOperationHandlerImpl implements OperationHandler {
@Override
public int count(FruitTransaction fruitTransaction) {
return -fruitTransaction.getQuantity();
int quantity = fruitTransaction.getQuantity();

if (quantity < 0) {
throw new RuntimeException("Quantity cannot be less then zero: " + quantity);
}

return -quantity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
public class ReturnOperationHandlerImpl implements OperationHandler {
@Override
public int count(FruitTransaction fruitTransaction) {
return fruitTransaction.getQuantity();
int quantity = fruitTransaction.getQuantity();

if (quantity < 0) {
throw new RuntimeException("Quantity cannot be less then zero: " + quantity);
}

return quantity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
public class SupplyOperationHandlerImpl implements OperationHandler {
@Override
public int count(FruitTransaction fruitTransaction) {
return fruitTransaction.getQuantity();
int quantity = fruitTransaction.getQuantity();

if (quantity < 0) {
throw new RuntimeException("Quantity cannot be less then zero: " + quantity);
}

return quantity;
}
}
5 changes: 2 additions & 3 deletions src/test/java/core/basesyntax/db/dao/StorageDaoImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class StorageDaoImplTest {
private static final String BANANA_NAME = "banana";
private static final Integer APPLE_COUNT = 15;
private static final Integer BANANA_COUNT = 20;
private static final Map<String, Integer> expected = Map.of(APPLE_NAME,
APPLE_COUNT, BANANA_NAME, BANANA_COUNT);
private static StorageDao storageDao;

@BeforeAll
Expand All @@ -22,9 +24,6 @@ static void beforeAll() {

@Test
void storageDaoAdd_twoFruits_Ok() {
Map<String, Integer> expected = Map.of(APPLE_NAME, APPLE_COUNT,
BANANA_NAME, BANANA_COUNT);

storageDao.add(APPLE_NAME, APPLE_COUNT);
storageDao.add(BANANA_NAME, BANANA_COUNT);

Expand Down
Loading

0 comments on commit 7aa30d9

Please sign in to comment.