diff --git a/src/main/java/core/basesyntax/HelloWorld.java b/src/main/java/core/basesyntax/HelloWorld.java deleted file mode 100644 index e758b17650..0000000000 --- a/src/main/java/core/basesyntax/HelloWorld.java +++ /dev/null @@ -1,7 +0,0 @@ -package core.basesyntax; - -/** - * Feel free to remove this class and create your own. - */ -public class HelloWorld { -} diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java new file mode 100644 index 0000000000..142e8c7770 --- /dev/null +++ b/src/main/java/core/basesyntax/Main.java @@ -0,0 +1,31 @@ +package core.basesyntax; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.service.FileService; +import core.basesyntax.service.FileServiceImpl; +import core.basesyntax.service.FoodStoreService; +import core.basesyntax.service.FoodStoreServiceImpl; +import core.basesyntax.service.ReportService; +import core.basesyntax.service.ReportServiceImpl; +import core.basesyntax.service.TransactionParser; +import core.basesyntax.service.TransactionParserImpl; +import java.util.List; + +public class Main { + private static final String INPUT_FILE_NAME = "src/main/resources/allData.csv"; + private static final String OUTPUT_FILE_NAME = "src/main/resources/report.csv"; + private static final int TITLE_ELEMENT_INDEX = 0; + private static final FoodStoreService foodStoreService = new FoodStoreServiceImpl(); + private static final FileService fileService = new FileServiceImpl(); + private static final TransactionParser transactionParser = new TransactionParserImpl(); + private static final ReportService reportService = new ReportServiceImpl(); + + public static void main(String[] args) { + List dailyData = fileService.read(INPUT_FILE_NAME); + dailyData.remove(TITLE_ELEMENT_INDEX); + List fruitTransactions = transactionParser.parseTransactions(dailyData); + foodStoreService.processTransactions(fruitTransactions); + String report = reportService.createReport(); + fileService.write(report, OUTPUT_FILE_NAME); + } +} diff --git a/src/main/java/core/basesyntax/db/Storage.java b/src/main/java/core/basesyntax/db/Storage.java new file mode 100644 index 0000000000..c9a581d664 --- /dev/null +++ b/src/main/java/core/basesyntax/db/Storage.java @@ -0,0 +1,10 @@ +package core.basesyntax.db; + +import java.util.Comparator; +import java.util.Map; +import java.util.TreeMap; + +public class Storage { + public static final Map storage + = new TreeMap<>(Comparator.naturalOrder()); +} diff --git a/src/main/java/core/basesyntax/model/FruitTransaction.java b/src/main/java/core/basesyntax/model/FruitTransaction.java new file mode 100644 index 0000000000..629ae17c87 --- /dev/null +++ b/src/main/java/core/basesyntax/model/FruitTransaction.java @@ -0,0 +1,71 @@ +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 name, int quantity) { + this.operation = operation; + this.fruit = name; + this.quantity = quantity; + } + + public Operation getOperation() { + return operation; + } + + public String getFruit() { + return fruit; + } + + public int getQuantity() { + return quantity; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + FruitTransaction that = (FruitTransaction) o; + return quantity == that.quantity && operation + == that.operation && Objects.equals(fruit, that.fruit); + } + + @Override + public int hashCode() { + return Objects.hash(operation, fruit, quantity); + } + + public enum Operation { + BALANCE("b"), + SUPPLY("s"), + PURCHASE("p"), + RETURN("r"); + private String code; + + Operation(String code) { + this.code = code; + } + + public static Operation fromCode(String code) { + for (Operation operation : Operation.values()) { + if (operation.getCode().equals(code)) { + return operation; + } + } + throw new IllegalArgumentException("No Operation found for code: " + code); + + } + + public String getCode() { + return code; + } + } +} diff --git a/src/main/java/core/basesyntax/service/FileService.java b/src/main/java/core/basesyntax/service/FileService.java new file mode 100644 index 0000000000..c552990eda --- /dev/null +++ b/src/main/java/core/basesyntax/service/FileService.java @@ -0,0 +1,10 @@ +package core.basesyntax.service; + +import java.util.List; + +public interface FileService { + + List read(String fileName); + + void write(String content, String fileName); +} diff --git a/src/main/java/core/basesyntax/service/FileServiceImpl.java b/src/main/java/core/basesyntax/service/FileServiceImpl.java new file mode 100644 index 0000000000..19e56e74b2 --- /dev/null +++ b/src/main/java/core/basesyntax/service/FileServiceImpl.java @@ -0,0 +1,30 @@ +package core.basesyntax.service; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +public class FileServiceImpl implements FileService { + + @Override + public List read(String fileName) { + List dailyData; + try { + dailyData = Files.readAllLines(Path.of(fileName)); + } catch (IOException e) { + throw new RuntimeException("Can't get data from file: " + fileName, e); + } + return dailyData; + } + + @Override + public void write(String content, String fileName) { + try { + Files.writeString(Path.of(fileName), content); + } catch (IOException e) { + throw new RuntimeException("Can't write data to file: " + fileName, e); + } + } +} + diff --git a/src/main/java/core/basesyntax/service/FoodStoreService.java b/src/main/java/core/basesyntax/service/FoodStoreService.java new file mode 100644 index 0000000000..cbd53ffe0e --- /dev/null +++ b/src/main/java/core/basesyntax/service/FoodStoreService.java @@ -0,0 +1,8 @@ +package core.basesyntax.service; + +import core.basesyntax.model.FruitTransaction; +import java.util.List; + +public interface FoodStoreService { + void processTransactions(List fruitTransactionsList); +} diff --git a/src/main/java/core/basesyntax/service/FoodStoreServiceImpl.java b/src/main/java/core/basesyntax/service/FoodStoreServiceImpl.java new file mode 100644 index 0000000000..20fce10243 --- /dev/null +++ b/src/main/java/core/basesyntax/service/FoodStoreServiceImpl.java @@ -0,0 +1,36 @@ + +package core.basesyntax.service; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.strategy.OperationStrategy; +import core.basesyntax.strategy.OperationStrategyImpl; +import core.basesyntax.strategy.operation.BalanceTypeHandler; +import core.basesyntax.strategy.operation.OperationHandlers; +import core.basesyntax.strategy.operation.PurchaseTypeHandler; +import core.basesyntax.strategy.operation.ReturnTypeHandler; +import core.basesyntax.strategy.operation.SupplyTypeHandler; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class FoodStoreServiceImpl implements FoodStoreService { + private Map operationHandlersMap = + new HashMap<>() { + { + put(FruitTransaction.Operation.BALANCE, new BalanceTypeHandler()); + put(FruitTransaction.Operation.SUPPLY, new SupplyTypeHandler()); + put(FruitTransaction.Operation.PURCHASE, new PurchaseTypeHandler()); + put(FruitTransaction.Operation.RETURN, new ReturnTypeHandler()); + } + }; + private OperationStrategy operationStrategy = + new OperationStrategyImpl(operationHandlersMap); + + @Override + public void processTransactions(List fruitTransactionsList) { + for (FruitTransaction transaction : fruitTransactionsList) { + operationStrategy.getHandler(transaction.getOperation()).handleTransaction(transaction); + } + } +} + diff --git a/src/main/java/core/basesyntax/service/ReportService.java b/src/main/java/core/basesyntax/service/ReportService.java new file mode 100644 index 0000000000..8d2487b298 --- /dev/null +++ b/src/main/java/core/basesyntax/service/ReportService.java @@ -0,0 +1,5 @@ +package core.basesyntax.service; + +public interface ReportService { + String createReport(); +} diff --git a/src/main/java/core/basesyntax/service/ReportServiceImpl.java b/src/main/java/core/basesyntax/service/ReportServiceImpl.java new file mode 100644 index 0000000000..1eca1447cb --- /dev/null +++ b/src/main/java/core/basesyntax/service/ReportServiceImpl.java @@ -0,0 +1,19 @@ +package core.basesyntax.service; + +import static core.basesyntax.db.Storage.storage; + +import java.util.Map; + +public class ReportServiceImpl implements ReportService { + @Override + public String createReport() { + StringBuilder stringBuilder = new StringBuilder(); + for (Map.Entry entry : storage.entrySet()) { + stringBuilder.append(entry.getKey()) + .append(", ") + .append(entry.getValue()) + .append(System.lineSeparator()); + } + return stringBuilder.toString(); + } +} diff --git a/src/main/java/core/basesyntax/service/TransactionParser.java b/src/main/java/core/basesyntax/service/TransactionParser.java new file mode 100644 index 0000000000..d39deaa562 --- /dev/null +++ b/src/main/java/core/basesyntax/service/TransactionParser.java @@ -0,0 +1,8 @@ +package core.basesyntax.service; + +import core.basesyntax.model.FruitTransaction; +import java.util.List; + +public interface TransactionParser { + List parseTransactions(List lines); +} diff --git a/src/main/java/core/basesyntax/service/TransactionParserImpl.java b/src/main/java/core/basesyntax/service/TransactionParserImpl.java new file mode 100644 index 0000000000..d339a187aa --- /dev/null +++ b/src/main/java/core/basesyntax/service/TransactionParserImpl.java @@ -0,0 +1,43 @@ +package core.basesyntax.service; + +import core.basesyntax.model.FruitTransaction; +import java.util.ArrayList; +import java.util.List; + +public class TransactionParserImpl implements TransactionParser { + private static final int OPERATION_POSITION_AFTER_SPLIT = 0; + private static final int FRUIT_NAME_POSITION_AFTER_SPLIT = 1; + private static final int QUANTITY_POSITION_AFTER_SPLIT = 2; + private static final int EXPECTED_LENGTH_AFTER_SPLIT = 3; + private static final int EXPECTED_LENGTH_OF_TYPE = 1; + + @Override + public List parseTransactions(List lines) { + List fruitTransactions = new ArrayList<>(); + for (String line : lines) { + String[] parsedLine = parseValue(line); + FruitTransaction.Operation operation = + validOperationValue(parsedLine[OPERATION_POSITION_AFTER_SPLIT]); + String fruitName = parsedLine[FRUIT_NAME_POSITION_AFTER_SPLIT]; + int quantity = Integer.parseInt(parsedLine[QUANTITY_POSITION_AFTER_SPLIT]); + fruitTransactions.add(new FruitTransaction(operation, fruitName, quantity)); + } + return fruitTransactions; + } + + private String[] parseValue(String line) { + String[] split = line.split(","); + if (split.length != EXPECTED_LENGTH_AFTER_SPLIT) { + throw new IllegalArgumentException("Invalid input data: expected format " + + "',,'"); + } + return split; + } + + private FruitTransaction.Operation validOperationValue(String line) { + if (line.length() != EXPECTED_LENGTH_OF_TYPE) { + throw new RuntimeException("Type should be only singe character: " + line); + } + return FruitTransaction.Operation.fromCode(line); + } +} diff --git a/src/main/java/core/basesyntax/strategy/OperationStrategy.java b/src/main/java/core/basesyntax/strategy/OperationStrategy.java new file mode 100644 index 0000000000..9098602a6c --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/OperationStrategy.java @@ -0,0 +1,8 @@ +package core.basesyntax.strategy; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.strategy.operation.OperationHandlers; + +public interface OperationStrategy { + OperationHandlers getHandler(FruitTransaction.Operation operation); +} diff --git a/src/main/java/core/basesyntax/strategy/OperationStrategyImpl.java b/src/main/java/core/basesyntax/strategy/OperationStrategyImpl.java new file mode 100644 index 0000000000..1b40a17ae0 --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/OperationStrategyImpl.java @@ -0,0 +1,20 @@ +package core.basesyntax.strategy; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.strategy.operation.OperationHandlers; +import java.util.Map; + +public class OperationStrategyImpl implements OperationStrategy { + private Map typeHandlersMap; + + public OperationStrategyImpl(Map typeHandlersMap) { + this.typeHandlersMap = typeHandlersMap; + } + + @Override + public OperationHandlers getHandler(FruitTransaction.Operation operation) { + return typeHandlersMap.get(operation); + } + +} diff --git a/src/main/java/core/basesyntax/strategy/operation/BalanceTypeHandler.java b/src/main/java/core/basesyntax/strategy/operation/BalanceTypeHandler.java new file mode 100644 index 0000000000..57c81319dd --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/operation/BalanceTypeHandler.java @@ -0,0 +1,13 @@ +package core.basesyntax.strategy.operation; + +import static core.basesyntax.db.Storage.storage; + +import core.basesyntax.model.FruitTransaction; + +public class BalanceTypeHandler implements OperationHandlers { + + @Override + public void handleTransaction(FruitTransaction transaction) { + storage.put(transaction.getFruit(),transaction.getQuantity()); + } +} diff --git a/src/main/java/core/basesyntax/strategy/operation/OperationHandlers.java b/src/main/java/core/basesyntax/strategy/operation/OperationHandlers.java new file mode 100644 index 0000000000..2e6a8ec570 --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/operation/OperationHandlers.java @@ -0,0 +1,7 @@ +package core.basesyntax.strategy.operation; + +import core.basesyntax.model.FruitTransaction; + +public interface OperationHandlers { + void handleTransaction(FruitTransaction transaction); +} diff --git a/src/main/java/core/basesyntax/strategy/operation/PurchaseTypeHandler.java b/src/main/java/core/basesyntax/strategy/operation/PurchaseTypeHandler.java new file mode 100644 index 0000000000..5b42da5adb --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/operation/PurchaseTypeHandler.java @@ -0,0 +1,17 @@ +package core.basesyntax.strategy.operation; + +import static core.basesyntax.db.Storage.storage; + +import core.basesyntax.model.FruitTransaction; + +public class PurchaseTypeHandler implements OperationHandlers { + @Override + public void handleTransaction(FruitTransaction transaction) { + int currentQuantity = storage.get(transaction.getFruit()); + if (currentQuantity - transaction.getQuantity() < 0) { + throw new RuntimeException("Quantity cannot be less then 0!"); + } + storage.put(transaction.getFruit(), currentQuantity + - transaction.getQuantity()); + } +} diff --git a/src/main/java/core/basesyntax/strategy/operation/ReturnTypeHandler.java b/src/main/java/core/basesyntax/strategy/operation/ReturnTypeHandler.java new file mode 100644 index 0000000000..cde5bc6cb3 --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/operation/ReturnTypeHandler.java @@ -0,0 +1,14 @@ +package core.basesyntax.strategy.operation; + +import static core.basesyntax.db.Storage.storage; + +import core.basesyntax.model.FruitTransaction; + +public class ReturnTypeHandler implements OperationHandlers { + @Override + public void handleTransaction(FruitTransaction transaction) { + Integer currentQuantity = storage.get(transaction.getFruit()); + storage.put(transaction.getFruit(),currentQuantity + + transaction.getQuantity()); + } +} diff --git a/src/main/java/core/basesyntax/strategy/operation/SupplyTypeHandler.java b/src/main/java/core/basesyntax/strategy/operation/SupplyTypeHandler.java new file mode 100644 index 0000000000..8bb24e1a24 --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/operation/SupplyTypeHandler.java @@ -0,0 +1,15 @@ +package core.basesyntax.strategy.operation; + +import static core.basesyntax.db.Storage.storage; + +import core.basesyntax.model.FruitTransaction; + +public class SupplyTypeHandler implements OperationHandlers { + + @Override + public void handleTransaction(FruitTransaction transaction) { + int currentQuantity = storage.get(transaction.getFruit()); + storage.put(transaction.getFruit(),currentQuantity + + transaction.getQuantity()); + } +} diff --git a/src/main/resources/allData.csv b/src/main/resources/allData.csv new file mode 100644 index 0000000000..c47b339ec9 --- /dev/null +++ b/src/main/resources/allData.csv @@ -0,0 +1,13 @@ +type,fruit,quantity, +b,banana,20 +b,apple,100 +b,orange,20 +s,banana,100 +p,banana,13 +r,apple,10 +p,orange,20 +p,apple,20 +p,banana,5 +r,orange,1 +s,banana,50 +b,cucumber,10 \ No newline at end of file diff --git a/src/main/resources/report.csv b/src/main/resources/report.csv new file mode 100644 index 0000000000..f7e236fcc0 --- /dev/null +++ b/src/main/resources/report.csv @@ -0,0 +1,4 @@ +banana, 152 +orange, 1 +apple, 90 +cucumber, 10 diff --git a/src/test/java/core/basesyntax/HelloWorldTest.java b/src/test/java/core/basesyntax/HelloWorldTest.java deleted file mode 100644 index c4a0df68d7..0000000000 --- a/src/test/java/core/basesyntax/HelloWorldTest.java +++ /dev/null @@ -1,7 +0,0 @@ -package core.basesyntax; - -/** - * Feel free to remove this class and create your own. - */ -public class HelloWorldTest { -} diff --git a/src/test/java/core/basesyntax/db/StorageTest.java b/src/test/java/core/basesyntax/db/StorageTest.java new file mode 100644 index 0000000000..7f132ca6c3 --- /dev/null +++ b/src/test/java/core/basesyntax/db/StorageTest.java @@ -0,0 +1,36 @@ +package core.basesyntax.db; + +import static core.basesyntax.db.Storage.storage; +import static org.junit.Assert.assertEquals; + +import java.util.HashMap; +import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class StorageTest { + private static final String DEFAULT_KEY1 = "Key1"; + private static final String DEFAULT_KEY2 = "Key2"; + private static final String DEFAULT_KEY3 = "Key3"; + private static final int DEFAULT_VALUE1 = 1; + private static final int DEFAULT_VALUE2 = 2; + private static final int DEFAULT_VALUE3 = 3; + private static Map storageTest; + + @BeforeEach + void setUp() { + storage.clear(); + storageTest = new HashMap<>(); + storageTest.put(DEFAULT_KEY1, DEFAULT_VALUE1); + storageTest.put(DEFAULT_KEY2, DEFAULT_VALUE2); + storageTest.put(DEFAULT_KEY3, DEFAULT_VALUE3); + } + + @Test + void storageAddValidData_Ok() { + storage.put(DEFAULT_KEY1, DEFAULT_VALUE1); + storage.put(DEFAULT_KEY2, DEFAULT_VALUE2); + storage.put(DEFAULT_KEY3, DEFAULT_VALUE3); + assertEquals(storage, storageTest); + } +} diff --git a/src/test/java/core/basesyntax/model/FruitTransactionTest.java b/src/test/java/core/basesyntax/model/FruitTransactionTest.java new file mode 100644 index 0000000000..c1193c7a16 --- /dev/null +++ b/src/test/java/core/basesyntax/model/FruitTransactionTest.java @@ -0,0 +1,67 @@ +package core.basesyntax.model; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertThrows; + +import org.junit.jupiter.api.Test; + +class FruitTransactionTest { + private static final String DEFAULT_NAME = "apple"; + private static final int DEFAULT_QUANTITY = 10; + private static final FruitTransaction.Operation DEFAULT_OPERATION = + FruitTransaction.Operation.BALANCE; + private static final FruitTransaction.Operation VALID_OPERATION = + FruitTransaction.Operation.SUPPLY; + private static final FruitTransaction DEFAULT_TRANSACTION = + new FruitTransaction(DEFAULT_OPERATION, DEFAULT_NAME, DEFAULT_QUANTITY); + + private static final String DEFAULT_OPERATION_CODE = "b"; + private static final String NOT_VALID_OPERATION_CODE = "m"; + + @Test + void fruitTransactionConstructor_Ok() { + FruitTransaction validTransaction = + new FruitTransaction(DEFAULT_OPERATION, DEFAULT_NAME, DEFAULT_QUANTITY); + assertEquals(DEFAULT_TRANSACTION, validTransaction); + } + + @Test + void fruitTransactionNotEquals_Ok() { + FruitTransaction otherFruitTransaction = + new FruitTransaction(VALID_OPERATION, DEFAULT_NAME, DEFAULT_QUANTITY); + assertNotEquals(DEFAULT_TRANSACTION, otherFruitTransaction); + } + + @Test + void getValidFruitName_Ok() { + assertEquals(DEFAULT_TRANSACTION.getFruit(), DEFAULT_NAME); + } + + @Test + void getValidQuantity_Ok() { + assertEquals(DEFAULT_TRANSACTION.getQuantity(), DEFAULT_QUANTITY); + } + + @Test + void getValidOperation_Ok() { + assertEquals(DEFAULT_TRANSACTION.getOperation(), DEFAULT_OPERATION); + } + + @Test + void getOperationValidCode_Ok() { + assertEquals(FruitTransaction.Operation.fromCode(DEFAULT_OPERATION_CODE), + DEFAULT_OPERATION); + } + + @Test + void getOperationCode() { + assertEquals(DEFAULT_OPERATION_CODE, DEFAULT_OPERATION.getCode()); + } + + @Test + void getOperationNotValidCode_notOk() { + assertThrows(IllegalArgumentException.class, () -> + FruitTransaction.Operation.fromCode(NOT_VALID_OPERATION_CODE)); + } +} diff --git a/src/test/java/core/basesyntax/service/FileServiceImplTest.java b/src/test/java/core/basesyntax/service/FileServiceImplTest.java new file mode 100644 index 0000000000..6b0643921f --- /dev/null +++ b/src/test/java/core/basesyntax/service/FileServiceImplTest.java @@ -0,0 +1,51 @@ +package core.basesyntax.service; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.Test; + +class FileServiceImplTest { + private static final String DEFAULT_FILE_NAME = "src/test/resources/testData.csv"; + private static final String RESULT_FILE_NAME + = "src/test/resources/testResult.csv"; + private static final String NOT_VALID_FILE_NAME = "C:/test.csv"; + private static final List DEFAULT_DATA = new ArrayList<>(); + private static final FileService FILE_SERVICE = new FileServiceImpl(); + private static final String VALID_RESULT_DATA = """ + banana,107 + apple,100 + orange,20"""; + private static final List RESULT_LIST = new ArrayList<>(); + + @Test + void read_ValidDataRead_Ok() { + DEFAULT_DATA.add("type,fruit,quantity,"); + DEFAULT_DATA.add("b,banana,20"); + DEFAULT_DATA.add("b,apple,100"); + DEFAULT_DATA.add("b,orange,20"); + DEFAULT_DATA.add("s,banana,100"); + DEFAULT_DATA.add("p,banana,13"); + assertEquals(FILE_SERVICE.read(DEFAULT_FILE_NAME), DEFAULT_DATA); + } + + @Test + void read_NotValidFileName_notOk() { + assertThrows(RuntimeException.class, () -> FILE_SERVICE.read(NOT_VALID_FILE_NAME)); + } + + @Test + void write_ValidDataWrite_Ok() { + RESULT_LIST.addAll(List.of(VALID_RESULT_DATA.split("\n"))); + FILE_SERVICE.write(VALID_RESULT_DATA, RESULT_FILE_NAME); + assertEquals(RESULT_LIST, FILE_SERVICE.read(RESULT_FILE_NAME)); + } + + @Test + void write_NotValidFileName_notOk() { + assertThrows(RuntimeException.class, () -> FILE_SERVICE + .write(VALID_RESULT_DATA,NOT_VALID_FILE_NAME)); + } +} diff --git a/src/test/java/core/basesyntax/service/FoodStoreServiceImplTest.java b/src/test/java/core/basesyntax/service/FoodStoreServiceImplTest.java new file mode 100644 index 0000000000..568e77e045 --- /dev/null +++ b/src/test/java/core/basesyntax/service/FoodStoreServiceImplTest.java @@ -0,0 +1,40 @@ +package core.basesyntax.service; + +import static core.basesyntax.db.Storage.storage; +import static org.junit.Assert.assertEquals; + +import core.basesyntax.model.FruitTransaction; +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class FoodStoreServiceImplTest { + + private static final FoodStoreService foodStoreService = new FoodStoreServiceImpl(); + private static final List fruitTransanctionList = new ArrayList<>(); + private static final Integer EXPECTED_VALUE = 90; + private static final Integer VALID_VALUE_1 = 100; + private static final Integer VALID_VALUE_2 = 10; + private static final String VALID_NAME = "apple"; + + @BeforeAll + static void beforeAll() { + fruitTransanctionList.add(new FruitTransaction(FruitTransaction + .Operation.BALANCE,VALID_NAME,VALID_VALUE_1)); + fruitTransanctionList.add(new FruitTransaction(FruitTransaction + .Operation.PURCHASE,VALID_NAME, VALID_VALUE_2)); + } + + @BeforeEach + void setUp() { + storage.clear(); + } + + @Test + void processTransactions_ValidTransactions_Ok() { + foodStoreService.processTransactions(fruitTransanctionList); + assertEquals(EXPECTED_VALUE,storage.get(VALID_NAME)); + } +} diff --git a/src/test/java/core/basesyntax/service/ReportServiceImplTest.java b/src/test/java/core/basesyntax/service/ReportServiceImplTest.java new file mode 100644 index 0000000000..8ca09df786 --- /dev/null +++ b/src/test/java/core/basesyntax/service/ReportServiceImplTest.java @@ -0,0 +1,29 @@ +package core.basesyntax.service; + +import static core.basesyntax.db.Storage.storage; +import static org.junit.Assert.assertEquals; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class ReportServiceImplTest { + private static final ReportService reportService = new ReportServiceImpl(); + private static final String VALID_KEY_1 = "apple"; + private static final String VALID_KEY_2 = "banana"; + private static final Integer VALID_VALUE_1 = 90; + private static final Integer VALID_VALUE_2 = 80; + private static final String EXPECTED_RESULT = "apple, 90" + System.lineSeparator() + + "banana, 80" + System.lineSeparator(); + + @BeforeEach + void setUp() { + storage.clear(); + } + + @Test + void createReport_ValidData_Ok() { + storage.put(VALID_KEY_1,VALID_VALUE_1); + storage.put(VALID_KEY_2,VALID_VALUE_2); + assertEquals(EXPECTED_RESULT, reportService.createReport()); + } +} diff --git a/src/test/java/core/basesyntax/service/TransactionParserImplTest.java b/src/test/java/core/basesyntax/service/TransactionParserImplTest.java new file mode 100644 index 0000000000..0d3c4dcd3c --- /dev/null +++ b/src/test/java/core/basesyntax/service/TransactionParserImplTest.java @@ -0,0 +1,50 @@ +package core.basesyntax.service; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + +import core.basesyntax.model.FruitTransaction; +import java.util.ArrayList; +import java.util.List; +import org.junit.jupiter.api.Test; + +class TransactionParserImplTest { + private static final TransactionParserImpl transactionParser + = new TransactionParserImpl(); + + private static final FruitTransaction VALID_TRANSACTION1 + = new FruitTransaction( + FruitTransaction.Operation.BALANCE, "apple", 100); + private static final FruitTransaction VALID_TRANSACTION2 + = new FruitTransaction( + FruitTransaction.Operation.SUPPLY, "banana", 20); + private static final List fruitTransactionList + = new ArrayList<>(); + private static final String VALID_STRING1 = "b,apple,100"; + private static final String VALID_STRING2 = "s,banana,20"; + private static final String NOT_VALID_STRING = "banana,20"; + private static final String NOT_VALID_OPERATION_STRING = "abc,banana,20"; + private static final List VALID_STRING_LIST = new ArrayList<>(); + + @Test + void parseTransactions_ValidData_Ok() { + fruitTransactionList.add(VALID_TRANSACTION1); + fruitTransactionList.add(VALID_TRANSACTION2); + VALID_STRING_LIST.add(VALID_STRING1); + VALID_STRING_LIST.add(VALID_STRING2); + assertEquals(fruitTransactionList, transactionParser + .parseTransactions(VALID_STRING_LIST)); + } + + @Test + void parseTransactions_NotValidData_notOk() { + assertThrows(IllegalArgumentException.class, () -> transactionParser + .parseTransactions(List.of(NOT_VALID_STRING))); + } + + @Test + void parseTransactions_NotValidOperation_notOk() { + assertThrows(RuntimeException.class, () -> transactionParser + .parseTransactions(List.of(NOT_VALID_OPERATION_STRING))); + } +} diff --git a/src/test/java/core/basesyntax/strategy/OperationStrategyImplTest.java b/src/test/java/core/basesyntax/strategy/OperationStrategyImplTest.java new file mode 100644 index 0000000000..a6a36b2cda --- /dev/null +++ b/src/test/java/core/basesyntax/strategy/OperationStrategyImplTest.java @@ -0,0 +1,32 @@ +package core.basesyntax.strategy; + +import static org.junit.Assert.assertEquals; + +import core.basesyntax.model.FruitTransaction; +import core.basesyntax.strategy.operation.BalanceTypeHandler; +import core.basesyntax.strategy.operation.OperationHandlers; +import java.util.HashMap; +import java.util.Map; +import org.junit.jupiter.api.Test; + +class OperationStrategyImplTest { + private static Map operationHandlersMap = + new HashMap<>() { + { + put(FruitTransaction.Operation.BALANCE, new BalanceTypeHandler()); + } + }; + + private static final OperationStrategy OPERATION_STRATEGY + = new OperationStrategyImpl(operationHandlersMap); + private static final OperationHandlers BALANCE_TYPE_HANDLER = new BalanceTypeHandler(); + + private static final FruitTransaction.Operation VALID_OPERATION + = FruitTransaction.Operation.BALANCE; + + @Test + void getHandler_ValidData_Ok() { + assertEquals(BALANCE_TYPE_HANDLER.getClass(),OPERATION_STRATEGY + .getHandler(VALID_OPERATION).getClass()); + } +} diff --git a/src/test/java/core/basesyntax/strategy/operation/BalanceTypeHandlerTest.java b/src/test/java/core/basesyntax/strategy/operation/BalanceTypeHandlerTest.java new file mode 100644 index 0000000000..e7a42dced6 --- /dev/null +++ b/src/test/java/core/basesyntax/strategy/operation/BalanceTypeHandlerTest.java @@ -0,0 +1,31 @@ +package core.basesyntax.strategy.operation; + +import static core.basesyntax.db.Storage.storage; +import static org.junit.Assert.assertEquals; + +import core.basesyntax.model.FruitTransaction; +import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class BalanceTypeHandlerTest { + private static final String VALID_NAME = "banana"; + private static final Integer VALID_QUANTITY = 100; + private static final FruitTransaction VALID_FRUIT_TRANSACTION + = new FruitTransaction(FruitTransaction.Operation.BALANCE, + VALID_NAME,VALID_QUANTITY); + private static final OperationHandlers OPERATION_HANDLERS + = new BalanceTypeHandler(); + + @BeforeEach + void setUp() { + storage.clear(); + } + + @Test + void balanceHandleTransaction_ValidData_Ok() { + OPERATION_HANDLERS.handleTransaction(VALID_FRUIT_TRANSACTION); + assertEquals(Map.of(VALID_NAME,VALID_QUANTITY).entrySet(),storage.entrySet()); + + } +} diff --git a/src/test/java/core/basesyntax/strategy/operation/PurchaseTypeHandlerTest.java b/src/test/java/core/basesyntax/strategy/operation/PurchaseTypeHandlerTest.java new file mode 100644 index 0000000000..5f70e67643 --- /dev/null +++ b/src/test/java/core/basesyntax/strategy/operation/PurchaseTypeHandlerTest.java @@ -0,0 +1,45 @@ +package core.basesyntax.strategy.operation; + +import static core.basesyntax.db.Storage.storage; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + +import core.basesyntax.model.FruitTransaction; +import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class PurchaseTypeHandlerTest { + + private static final OperationHandlers OPERATION_HANDLERS + = new PurchaseTypeHandler(); + private static final FruitTransaction.Operation VALID_OPERATION = + FruitTransaction.Operation.PURCHASE; + private static final String VALID_NAME = "banana"; + private static final Integer VALID_QUANTITY = 100; + private static final Integer NOT_VALID_QUANTITY = 200; + private static final Integer EXPECTED_QUANTITY = 0; + + private static final FruitTransaction VALID_FRUIT_TRANSACTION + = new FruitTransaction(VALID_OPERATION, VALID_NAME, VALID_QUANTITY); + private static final FruitTransaction NOT_VALID_FRUIT_TRANSACTION + = new FruitTransaction(VALID_OPERATION, VALID_NAME, NOT_VALID_QUANTITY); + + @BeforeEach + void setUp() { + storage.clear(); + } + + @Test + void purchaseHandleTransaction_ValidData_Ok() { + storage.put(VALID_NAME, VALID_QUANTITY); + OPERATION_HANDLERS.handleTransaction(VALID_FRUIT_TRANSACTION); + assertEquals(Map.of(VALID_NAME, EXPECTED_QUANTITY).entrySet(), storage.entrySet()); + } + + @Test + void handleTransaction_NotValidData_notOk() { + assertThrows(RuntimeException.class, () -> OPERATION_HANDLERS + .handleTransaction(NOT_VALID_FRUIT_TRANSACTION)); + } +} diff --git a/src/test/java/core/basesyntax/strategy/operation/ReturnTypeHandlerTest.java b/src/test/java/core/basesyntax/strategy/operation/ReturnTypeHandlerTest.java new file mode 100644 index 0000000000..3608451aa8 --- /dev/null +++ b/src/test/java/core/basesyntax/strategy/operation/ReturnTypeHandlerTest.java @@ -0,0 +1,33 @@ +package core.basesyntax.strategy.operation; + +import static core.basesyntax.db.Storage.storage; +import static org.junit.Assert.assertEquals; + +import core.basesyntax.model.FruitTransaction; +import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class ReturnTypeHandlerTest { + private static final OperationHandlers OPERATION_HANDLERS + = new ReturnTypeHandler(); + private static final FruitTransaction.Operation VALID_OPERATION = + FruitTransaction.Operation.RETURN; + private static final String VALID_NAME = "banana"; + private static final Integer VALID_QUANTITY = 100; + private static final Integer EXPECTED_QUANTITY = 200; + private static final FruitTransaction VALID_FRUIT_TRANSACTION + = new FruitTransaction(VALID_OPERATION, VALID_NAME, VALID_QUANTITY); + + @BeforeEach + void setUp() { + storage.clear(); + } + + @Test + void returnHandleTransaction_ValidData_Ok() { + storage.put(VALID_NAME, VALID_QUANTITY); + OPERATION_HANDLERS.handleTransaction(VALID_FRUIT_TRANSACTION); + assertEquals(Map.of(VALID_NAME,EXPECTED_QUANTITY).entrySet(),storage.entrySet()); + } +} diff --git a/src/test/java/core/basesyntax/strategy/operation/SupplyTypeHandlerTest.java b/src/test/java/core/basesyntax/strategy/operation/SupplyTypeHandlerTest.java new file mode 100644 index 0000000000..88c28ebf57 --- /dev/null +++ b/src/test/java/core/basesyntax/strategy/operation/SupplyTypeHandlerTest.java @@ -0,0 +1,33 @@ +package core.basesyntax.strategy.operation; + +import static core.basesyntax.db.Storage.storage; +import static org.junit.Assert.assertEquals; + +import core.basesyntax.model.FruitTransaction; +import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class SupplyTypeHandlerTest { + private static final OperationHandlers OPERATION_HANDLERS + = new SupplyTypeHandler(); + private static final FruitTransaction.Operation VALID_OPERATION = + FruitTransaction.Operation.SUPPLY; + private static final String VALID_NAME = "banana"; + private static final Integer VALID_QUANTITY = 100; + private static final Integer EXPECTED_QUANTITY = 200; + private static final FruitTransaction VALID_FRUIT_TRANSACTION + = new FruitTransaction(VALID_OPERATION, VALID_NAME, VALID_QUANTITY); + + @BeforeEach + void setUp() { + storage.clear(); + } + + @Test + void supplyHandleTransaction_ValidData_Ok() { + storage.put(VALID_NAME, VALID_QUANTITY); + OPERATION_HANDLERS.handleTransaction(VALID_FRUIT_TRANSACTION); + assertEquals(Map.of(VALID_NAME,EXPECTED_QUANTITY).entrySet(),storage.entrySet()); + } +} diff --git a/src/test/resources/testData.csv b/src/test/resources/testData.csv new file mode 100644 index 0000000000..06c47a2d1f --- /dev/null +++ b/src/test/resources/testData.csv @@ -0,0 +1,6 @@ +type,fruit,quantity, +b,banana,20 +b,apple,100 +b,orange,20 +s,banana,100 +p,banana,13 diff --git a/src/test/resources/testResult.csv b/src/test/resources/testResult.csv new file mode 100644 index 0000000000..c63e7cd27e --- /dev/null +++ b/src/test/resources/testResult.csv @@ -0,0 +1,3 @@ +banana,107 +apple,100 +orange,20 \ No newline at end of file