From 11f9324709d101bd3f1a5a6304a7eb9be958b8ce Mon Sep 17 00:00:00 2001 From: Oleh Date: Sat, 2 Nov 2024 19:50:17 +0200 Subject: [PATCH 1/5] Fruit shop solution --- finalReport.csv | 3 + reportToRead.csv | 9 +++ src/main/java/core/basesyntax/HelloWorld.java | 9 --- src/main/java/core/basesyntax/Main.java | 55 +++++++++++++++++++ src/main/java/core/basesyntax/db/Storage.java | 12 ++++ .../java/core/basesyntax/file/FileReader.java | 7 +++ .../core/basesyntax/file/FileReaderImpl.java | 20 +++++++ .../java/core/basesyntax/file/FileWriter.java | 5 ++ .../core/basesyntax/file/FileWriterImpl.java | 17 ++++++ .../core/basesyntax/model/OperationType.java | 29 ++++++++++ .../core/basesyntax/model/ShopOperation.java | 33 +++++++++++ .../basesyntax/service/DataConverter.java | 8 +++ .../basesyntax/service/ReportGenerator.java | 5 ++ .../core/basesyntax/service/ShopService.java | 8 +++ .../service/StrategyOperationService.java | 8 +++ .../service/impl/DataConverterImpl.java | 30 ++++++++++ .../service/impl/ReportGeneratorImpl.java | 23 ++++++++ .../service/impl/ShopServiceImpl.java | 21 +++++++ .../impl/StrategyOperationServiceImpl.java | 19 +++++++ .../basesyntax/strategy/BalanceOperation.java | 11 ++++ .../basesyntax/strategy/OperationHandler.java | 7 +++ .../strategy/PurchaseOperation.java | 19 +++++++ .../basesyntax/strategy/ReturnOperation.java | 13 +++++ .../basesyntax/strategy/SupplyOperation.java | 13 +++++ src/main/java/exception/FileException.java | 7 +++ .../java/exception/OperationException.java | 7 +++ 26 files changed, 389 insertions(+), 9 deletions(-) create mode 100644 finalReport.csv create mode 100644 reportToRead.csv delete mode 100644 src/main/java/core/basesyntax/HelloWorld.java create mode 100644 src/main/java/core/basesyntax/Main.java create mode 100644 src/main/java/core/basesyntax/db/Storage.java create mode 100644 src/main/java/core/basesyntax/file/FileReader.java create mode 100644 src/main/java/core/basesyntax/file/FileReaderImpl.java create mode 100644 src/main/java/core/basesyntax/file/FileWriter.java create mode 100644 src/main/java/core/basesyntax/file/FileWriterImpl.java create mode 100644 src/main/java/core/basesyntax/model/OperationType.java create mode 100644 src/main/java/core/basesyntax/model/ShopOperation.java create mode 100644 src/main/java/core/basesyntax/service/DataConverter.java create mode 100644 src/main/java/core/basesyntax/service/ReportGenerator.java create mode 100644 src/main/java/core/basesyntax/service/ShopService.java create mode 100644 src/main/java/core/basesyntax/service/StrategyOperationService.java create mode 100644 src/main/java/core/basesyntax/service/impl/DataConverterImpl.java create mode 100644 src/main/java/core/basesyntax/service/impl/ReportGeneratorImpl.java create mode 100644 src/main/java/core/basesyntax/service/impl/ShopServiceImpl.java create mode 100644 src/main/java/core/basesyntax/service/impl/StrategyOperationServiceImpl.java create mode 100644 src/main/java/core/basesyntax/strategy/BalanceOperation.java create mode 100644 src/main/java/core/basesyntax/strategy/OperationHandler.java create mode 100644 src/main/java/core/basesyntax/strategy/PurchaseOperation.java create mode 100644 src/main/java/core/basesyntax/strategy/ReturnOperation.java create mode 100644 src/main/java/core/basesyntax/strategy/SupplyOperation.java create mode 100644 src/main/java/exception/FileException.java create mode 100644 src/main/java/exception/OperationException.java diff --git a/finalReport.csv b/finalReport.csv new file mode 100644 index 0000000000..6158e4419c --- /dev/null +++ b/finalReport.csv @@ -0,0 +1,3 @@ +fruit,quantity +banana,152 +apple,90 diff --git a/reportToRead.csv b/reportToRead.csv new file mode 100644 index 0000000000..c771f259d3 --- /dev/null +++ b/reportToRead.csv @@ -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 \ No newline at end of file diff --git a/src/main/java/core/basesyntax/HelloWorld.java b/src/main/java/core/basesyntax/HelloWorld.java deleted file mode 100644 index 05f94be41b..0000000000 --- a/src/main/java/core/basesyntax/HelloWorld.java +++ /dev/null @@ -1,9 +0,0 @@ -package core.basesyntax; - -/** - * Feel free to remove this class and create your own. - */ -public class HelloWorld { - // HINT: In the `public static void main(String[] args)` it is better to create instances of your classes, - // and call their methods, but do not write any business logic in the `main` method! -} diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java new file mode 100644 index 0000000000..c7117e187a --- /dev/null +++ b/src/main/java/core/basesyntax/Main.java @@ -0,0 +1,55 @@ +package core.basesyntax; + +import core.basesyntax.file.FileReader; +import core.basesyntax.file.FileReaderImpl; +import core.basesyntax.file.FileWriter; +import core.basesyntax.file.FileWriterImpl; +import core.basesyntax.model.OperationType; +import core.basesyntax.model.ShopOperation; +import core.basesyntax.service.DataConverter; +import core.basesyntax.service.ReportGenerator; +import core.basesyntax.service.ShopService; +import core.basesyntax.service.StrategyOperationService; +import core.basesyntax.service.impl.DataConverterImpl; +import core.basesyntax.service.impl.ReportGeneratorImpl; +import core.basesyntax.service.impl.ShopServiceImpl; +import core.basesyntax.service.impl.StrategyOperationServiceImpl; +import core.basesyntax.strategy.BalanceOperation; +import core.basesyntax.strategy.OperationHandler; +import core.basesyntax.strategy.PurchaseOperation; +import core.basesyntax.strategy.ReturnOperation; +import core.basesyntax.strategy.SupplyOperation; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class Main { + private static final String PATH_TO_FILE = "reportToRead.csv"; + private static final String PATH_TO_REPORT_FILE = "finalReport.csv"; + + public static void main(String[] args) { + FileReader fileReader = new FileReaderImpl(); + List inputReport = fileReader.read(PATH_TO_FILE); + + DataConverter dataConverter = new DataConverterImpl(); + + Map operationHandlers = new HashMap<>(); + operationHandlers.put(OperationType.BALANCE, new BalanceOperation()); + operationHandlers.put(OperationType.PURCHASE, new PurchaseOperation()); + operationHandlers.put(OperationType.SUPPLY, new SupplyOperation()); + operationHandlers.put(OperationType.RETURN, new ReturnOperation()); + StrategyOperationService operationStrategy = new StrategyOperationServiceImpl( + operationHandlers); + + List operations = dataConverter.convertToOperation(inputReport); + + ShopService shopService = new ShopServiceImpl(operationStrategy); + shopService.process(operations); + + ReportGenerator reportGenerator = new ReportGeneratorImpl(); + String resultingReport = reportGenerator.getReport(); + + FileWriter fileWriter = new FileWriterImpl(); + fileWriter.write(PATH_TO_REPORT_FILE, resultingReport); + } +} 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..2bfec8101d --- /dev/null +++ b/src/main/java/core/basesyntax/db/Storage.java @@ -0,0 +1,12 @@ +package core.basesyntax.db; + +import java.util.HashMap; +import java.util.Map; + +public class Storage { + public static final Map fruitsStorage = new HashMap<>(); + + public Map getFruitsStorage() { + return fruitsStorage; + } +} diff --git a/src/main/java/core/basesyntax/file/FileReader.java b/src/main/java/core/basesyntax/file/FileReader.java new file mode 100644 index 0000000000..47bd96e502 --- /dev/null +++ b/src/main/java/core/basesyntax/file/FileReader.java @@ -0,0 +1,7 @@ +package core.basesyntax.file; + +import java.util.List; + +public interface FileReader { + List read(String filePath); +} diff --git a/src/main/java/core/basesyntax/file/FileReaderImpl.java b/src/main/java/core/basesyntax/file/FileReaderImpl.java new file mode 100644 index 0000000000..b73d77d728 --- /dev/null +++ b/src/main/java/core/basesyntax/file/FileReaderImpl.java @@ -0,0 +1,20 @@ +package core.basesyntax.file; + +import exception.FileException; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +public class FileReaderImpl implements FileReader { + @Override + public List read(String filePath) { + List reportList; + try { + reportList = Files.readAllLines(Path.of(filePath)); + } catch (IOException e) { + throw new FileException("Can't read data from file: " + filePath); + } + return reportList; + } +} diff --git a/src/main/java/core/basesyntax/file/FileWriter.java b/src/main/java/core/basesyntax/file/FileWriter.java new file mode 100644 index 0000000000..f4a570f216 --- /dev/null +++ b/src/main/java/core/basesyntax/file/FileWriter.java @@ -0,0 +1,5 @@ +package core.basesyntax.file; + +public interface FileWriter { + void write(String filePath, String report); +} diff --git a/src/main/java/core/basesyntax/file/FileWriterImpl.java b/src/main/java/core/basesyntax/file/FileWriterImpl.java new file mode 100644 index 0000000000..38eff38133 --- /dev/null +++ b/src/main/java/core/basesyntax/file/FileWriterImpl.java @@ -0,0 +1,17 @@ +package core.basesyntax.file; + +import exception.FileException; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +public class FileWriterImpl implements FileWriter { + @Override + public void write(String filePath, String report) { + try { + Files.writeString(Path.of(filePath), report); + } catch (IOException e) { + throw new FileException("Can't write data to file: " + filePath); + } + } +} diff --git a/src/main/java/core/basesyntax/model/OperationType.java b/src/main/java/core/basesyntax/model/OperationType.java new file mode 100644 index 0000000000..46369f12e5 --- /dev/null +++ b/src/main/java/core/basesyntax/model/OperationType.java @@ -0,0 +1,29 @@ +package core.basesyntax.model; + +import exception.OperationException; + +public enum OperationType { + BALANCE("b"), + SUPPLY("s"), + PURCHASE("p"), + RETURN("r"); + + private final String codeOperation; + + OperationType(String codeOperation) { + this.codeOperation = codeOperation; + } + + public String getCodeOperation() { + return codeOperation; + } + + public static OperationType getOperationType(String codeOperation) { + for (OperationType type : OperationType.values()) { + if (type.getCodeOperation().equals(codeOperation)) { + return type; + } + } + throw new OperationException("Operation not found by code: " + codeOperation); + } +} diff --git a/src/main/java/core/basesyntax/model/ShopOperation.java b/src/main/java/core/basesyntax/model/ShopOperation.java new file mode 100644 index 0000000000..adb6b17c04 --- /dev/null +++ b/src/main/java/core/basesyntax/model/ShopOperation.java @@ -0,0 +1,33 @@ +package core.basesyntax.model; + +public class ShopOperation { + private final OperationType type; + private final String fruit; + private final int quantity; + + public ShopOperation(OperationType type, String fruit, int quantity) { + this.type = type; + this.fruit = fruit; + this.quantity = quantity; + } + + public OperationType getType() { + return type; + } + + public String getFruit() { + return fruit; + } + + public int getQuantity() { + return quantity; + } + + @Override + public String toString() { + return "ShopOperation{" + + "type=" + type + + ", fruit='" + fruit + '\'' + + ", quantity=" + quantity + '}'; + } +} diff --git a/src/main/java/core/basesyntax/service/DataConverter.java b/src/main/java/core/basesyntax/service/DataConverter.java new file mode 100644 index 0000000000..56f8d06105 --- /dev/null +++ b/src/main/java/core/basesyntax/service/DataConverter.java @@ -0,0 +1,8 @@ +package core.basesyntax.service; + +import core.basesyntax.model.ShopOperation; +import java.util.List; + +public interface DataConverter { + List convertToOperation(List reportList); +} diff --git a/src/main/java/core/basesyntax/service/ReportGenerator.java b/src/main/java/core/basesyntax/service/ReportGenerator.java new file mode 100644 index 0000000000..6649635ed2 --- /dev/null +++ b/src/main/java/core/basesyntax/service/ReportGenerator.java @@ -0,0 +1,5 @@ +package core.basesyntax.service; + +public interface ReportGenerator { + String getReport(); +} diff --git a/src/main/java/core/basesyntax/service/ShopService.java b/src/main/java/core/basesyntax/service/ShopService.java new file mode 100644 index 0000000000..e0b715cf1c --- /dev/null +++ b/src/main/java/core/basesyntax/service/ShopService.java @@ -0,0 +1,8 @@ +package core.basesyntax.service; + +import core.basesyntax.model.ShopOperation; +import java.util.List; + +public interface ShopService { + void process(List operations); +} diff --git a/src/main/java/core/basesyntax/service/StrategyOperationService.java b/src/main/java/core/basesyntax/service/StrategyOperationService.java new file mode 100644 index 0000000000..7898bdd760 --- /dev/null +++ b/src/main/java/core/basesyntax/service/StrategyOperationService.java @@ -0,0 +1,8 @@ +package core.basesyntax.service; + +import core.basesyntax.model.OperationType; +import core.basesyntax.strategy.OperationHandler; + +public interface StrategyOperationService { + OperationHandler get(OperationType operationType); +} diff --git a/src/main/java/core/basesyntax/service/impl/DataConverterImpl.java b/src/main/java/core/basesyntax/service/impl/DataConverterImpl.java new file mode 100644 index 0000000000..d36d4e7bf0 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/DataConverterImpl.java @@ -0,0 +1,30 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.model.OperationType; +import core.basesyntax.model.ShopOperation; +import core.basesyntax.service.DataConverter; +import java.util.ArrayList; +import java.util.List; + +public class DataConverterImpl implements DataConverter { + private static final int LIST_START = 1; + private static final int OPERATION_INDEX = 0; + private static final int FRUIT_INDEX = 1; + private static final int QUANTITY_INDEX = 2; + private static final String COMMA = ","; + + @Override + public List convertToOperation(List reportList) { + List shopOperationList = new ArrayList<>(); + for (String line : reportList.subList(LIST_START, reportList.size())) { + String[] parts = line.split(COMMA); + String codeOperation = parts[OPERATION_INDEX]; + String fruit = parts[FRUIT_INDEX]; + int quantity = Integer.parseInt(parts[QUANTITY_INDEX]); + shopOperationList.add( + new ShopOperation(OperationType.getOperationType(codeOperation), + fruit, quantity)); + } + return shopOperationList; + } +} diff --git a/src/main/java/core/basesyntax/service/impl/ReportGeneratorImpl.java b/src/main/java/core/basesyntax/service/impl/ReportGeneratorImpl.java new file mode 100644 index 0000000000..43be152d03 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/ReportGeneratorImpl.java @@ -0,0 +1,23 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.db.Storage; +import core.basesyntax.service.ReportGenerator; +import java.util.Map; + +public class ReportGeneratorImpl implements ReportGenerator { + private static final String LINE_SEPARATOR = System.lineSeparator(); + private static final String COMMA = ","; + private static final String HEADER = "fruit,quantity"; + + @Override + public String getReport() { + StringBuilder stringBuilder = new StringBuilder(HEADER + LINE_SEPARATOR); + for (Map.Entry entry : Storage.fruitsStorage.entrySet()) { + stringBuilder.append(entry.getKey()) + .append(COMMA) + .append(entry.getValue()) + .append(LINE_SEPARATOR); + } + return stringBuilder.toString(); + } +} diff --git a/src/main/java/core/basesyntax/service/impl/ShopServiceImpl.java b/src/main/java/core/basesyntax/service/impl/ShopServiceImpl.java new file mode 100644 index 0000000000..c3cc707153 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/ShopServiceImpl.java @@ -0,0 +1,21 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.model.ShopOperation; +import core.basesyntax.service.ShopService; +import core.basesyntax.service.StrategyOperationService; +import java.util.List; + +public class ShopServiceImpl implements ShopService { + private final StrategyOperationService strategyOperationService; + + public ShopServiceImpl(StrategyOperationService strategyOperationService) { + this.strategyOperationService = strategyOperationService; + } + + @Override + public void process(List operations) { + for (ShopOperation operation : operations) { + strategyOperationService.get(operation.getType()).handle(operation); + } + } +} diff --git a/src/main/java/core/basesyntax/service/impl/StrategyOperationServiceImpl.java b/src/main/java/core/basesyntax/service/impl/StrategyOperationServiceImpl.java new file mode 100644 index 0000000000..173eb18a21 --- /dev/null +++ b/src/main/java/core/basesyntax/service/impl/StrategyOperationServiceImpl.java @@ -0,0 +1,19 @@ +package core.basesyntax.service.impl; + +import core.basesyntax.model.OperationType; +import core.basesyntax.service.StrategyOperationService; +import core.basesyntax.strategy.OperationHandler; +import java.util.Map; + +public class StrategyOperationServiceImpl implements StrategyOperationService { + private Map operationHandlerMap; + + public StrategyOperationServiceImpl(Map operationHandlerMap) { + this.operationHandlerMap = operationHandlerMap; + } + + @Override + public OperationHandler get(OperationType operationType) { + return operationHandlerMap.get(operationType); + } +} diff --git a/src/main/java/core/basesyntax/strategy/BalanceOperation.java b/src/main/java/core/basesyntax/strategy/BalanceOperation.java new file mode 100644 index 0000000000..5aa3cc471d --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/BalanceOperation.java @@ -0,0 +1,11 @@ +package core.basesyntax.strategy; + +import core.basesyntax.db.Storage; +import core.basesyntax.model.ShopOperation; + +public class BalanceOperation implements OperationHandler { + @Override + public void handle(ShopOperation shopOperation) { + Storage.fruitsStorage.put(shopOperation.getFruit(), shopOperation.getQuantity()); + } +} diff --git a/src/main/java/core/basesyntax/strategy/OperationHandler.java b/src/main/java/core/basesyntax/strategy/OperationHandler.java new file mode 100644 index 0000000000..19b70f520b --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/OperationHandler.java @@ -0,0 +1,7 @@ +package core.basesyntax.strategy; + +import core.basesyntax.model.ShopOperation; + +public interface OperationHandler { + void handle(ShopOperation shopOperation); +} diff --git a/src/main/java/core/basesyntax/strategy/PurchaseOperation.java b/src/main/java/core/basesyntax/strategy/PurchaseOperation.java new file mode 100644 index 0000000000..77fa2f3a27 --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/PurchaseOperation.java @@ -0,0 +1,19 @@ +package core.basesyntax.strategy; + +import core.basesyntax.db.Storage; +import core.basesyntax.model.ShopOperation; +import exception.OperationException; + +public class PurchaseOperation implements OperationHandler { + @Override + public void handle(ShopOperation shopOperation) { + String fruit = shopOperation.getFruit(); + int quantity = Storage.fruitsStorage.get(fruit); + int quantityToBuy = shopOperation.getQuantity(); + if (quantity < quantityToBuy) { + throw new OperationException("Operation is impossible, not enough quantity of: " + + fruit); + } + Storage.fruitsStorage.put(fruit, quantity - quantityToBuy); + } +} diff --git a/src/main/java/core/basesyntax/strategy/ReturnOperation.java b/src/main/java/core/basesyntax/strategy/ReturnOperation.java new file mode 100644 index 0000000000..6a6694ab58 --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/ReturnOperation.java @@ -0,0 +1,13 @@ +package core.basesyntax.strategy; + +import core.basesyntax.db.Storage; +import core.basesyntax.model.ShopOperation; + +public class ReturnOperation implements OperationHandler { + @Override + public void handle(ShopOperation shopOperation) { + String fruit = shopOperation.getFruit(); + int quantity = Storage.fruitsStorage.get(fruit); + Storage.fruitsStorage.put(fruit, quantity + shopOperation.getQuantity()); + } +} diff --git a/src/main/java/core/basesyntax/strategy/SupplyOperation.java b/src/main/java/core/basesyntax/strategy/SupplyOperation.java new file mode 100644 index 0000000000..6ffe92f2d5 --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/SupplyOperation.java @@ -0,0 +1,13 @@ +package core.basesyntax.strategy; + +import core.basesyntax.db.Storage; +import core.basesyntax.model.ShopOperation; + +public class SupplyOperation implements OperationHandler { + @Override + public void handle(ShopOperation shopOperation) { + String fruit = shopOperation.getFruit(); + int quantity = Storage.fruitsStorage.get(fruit); + Storage.fruitsStorage.put(fruit, quantity + shopOperation.getQuantity()); + } +} diff --git a/src/main/java/exception/FileException.java b/src/main/java/exception/FileException.java new file mode 100644 index 0000000000..b38950a16f --- /dev/null +++ b/src/main/java/exception/FileException.java @@ -0,0 +1,7 @@ +package exception; + +public class FileException extends RuntimeException { + public FileException(String message) { + super(message); + } +} diff --git a/src/main/java/exception/OperationException.java b/src/main/java/exception/OperationException.java new file mode 100644 index 0000000000..9d25751f0b --- /dev/null +++ b/src/main/java/exception/OperationException.java @@ -0,0 +1,7 @@ +package exception; + +public class OperationException extends RuntimeException { + public OperationException(String message) { + super(message); + } +} From 5fe830128878e82ccd2dbdfac5cb0eb45f6920bf Mon Sep 17 00:00:00 2001 From: Oleh Date: Sat, 2 Nov 2024 19:58:59 +0200 Subject: [PATCH 2/5] place files into src/main/resources --- src/main/java/core/basesyntax/Main.java | 4 ++-- finalReport.csv => src/main/resources/finalReport.csv | 0 reportToRead.csv => src/main/resources/reportToRead.csv | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename finalReport.csv => src/main/resources/finalReport.csv (100%) rename reportToRead.csv => src/main/resources/reportToRead.csv (100%) diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java index c7117e187a..419beca76f 100644 --- a/src/main/java/core/basesyntax/Main.java +++ b/src/main/java/core/basesyntax/Main.java @@ -24,8 +24,8 @@ import java.util.Map; public class Main { - private static final String PATH_TO_FILE = "reportToRead.csv"; - private static final String PATH_TO_REPORT_FILE = "finalReport.csv"; + private static final String PATH_TO_FILE = "src/main/resources/reportToRead.csv"; + private static final String PATH_TO_REPORT_FILE = "src/main/resources/finalReport.csv"; public static void main(String[] args) { FileReader fileReader = new FileReaderImpl(); diff --git a/finalReport.csv b/src/main/resources/finalReport.csv similarity index 100% rename from finalReport.csv rename to src/main/resources/finalReport.csv diff --git a/reportToRead.csv b/src/main/resources/reportToRead.csv similarity index 100% rename from reportToRead.csv rename to src/main/resources/reportToRead.csv From 74adf83a56f69e47ca87a15a2be4ca2089db5fee Mon Sep 17 00:00:00 2001 From: Oleh Date: Sat, 2 Nov 2024 20:55:40 +0200 Subject: [PATCH 3/5] added try/catch block to handle() methods, added if block to method get() in StrategyOperationServiceImpl --- .../impl/StrategyOperationServiceImpl.java | 7 ++++++- .../strategy/PurchaseOperation.java | 19 ++++++++++++------- .../basesyntax/strategy/ReturnOperation.java | 12 +++++++++--- .../basesyntax/strategy/SupplyOperation.java | 11 ++++++++--- 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/main/java/core/basesyntax/service/impl/StrategyOperationServiceImpl.java b/src/main/java/core/basesyntax/service/impl/StrategyOperationServiceImpl.java index 173eb18a21..1aefe1bb07 100644 --- a/src/main/java/core/basesyntax/service/impl/StrategyOperationServiceImpl.java +++ b/src/main/java/core/basesyntax/service/impl/StrategyOperationServiceImpl.java @@ -3,6 +3,8 @@ import core.basesyntax.model.OperationType; import core.basesyntax.service.StrategyOperationService; import core.basesyntax.strategy.OperationHandler; +import exception.OperationException; + import java.util.Map; public class StrategyOperationServiceImpl implements StrategyOperationService { @@ -14,6 +16,9 @@ public StrategyOperationServiceImpl(Map operati @Override public OperationHandler get(OperationType operationType) { - return operationHandlerMap.get(operationType); + if (operationHandlerMap.get(operationType) != null) { + return operationHandlerMap.get(operationType); + } + throw new OperationException("Operation type is not correct: " + operationType); } } diff --git a/src/main/java/core/basesyntax/strategy/PurchaseOperation.java b/src/main/java/core/basesyntax/strategy/PurchaseOperation.java index 77fa2f3a27..f025c4096f 100644 --- a/src/main/java/core/basesyntax/strategy/PurchaseOperation.java +++ b/src/main/java/core/basesyntax/strategy/PurchaseOperation.java @@ -7,13 +7,18 @@ public class PurchaseOperation implements OperationHandler { @Override public void handle(ShopOperation shopOperation) { - String fruit = shopOperation.getFruit(); - int quantity = Storage.fruitsStorage.get(fruit); - int quantityToBuy = shopOperation.getQuantity(); - if (quantity < quantityToBuy) { - throw new OperationException("Operation is impossible, not enough quantity of: " - + fruit); + try { + String fruit = shopOperation.getFruit(); + int quantity = Storage.fruitsStorage.get(fruit); + int quantityToBuy = shopOperation.getQuantity(); + if (quantity < quantityToBuy) { + throw new OperationException("Operation is impossible, not enough quantity of: " + + fruit); + } + Storage.fruitsStorage.put(fruit, quantity - quantityToBuy); + } catch (NullPointerException e) { + throw new OperationException("Operation is not correct: " + shopOperation); } - Storage.fruitsStorage.put(fruit, quantity - quantityToBuy); + } } diff --git a/src/main/java/core/basesyntax/strategy/ReturnOperation.java b/src/main/java/core/basesyntax/strategy/ReturnOperation.java index 6a6694ab58..02ae87bd34 100644 --- a/src/main/java/core/basesyntax/strategy/ReturnOperation.java +++ b/src/main/java/core/basesyntax/strategy/ReturnOperation.java @@ -2,12 +2,18 @@ import core.basesyntax.db.Storage; import core.basesyntax.model.ShopOperation; +import exception.OperationException; public class ReturnOperation implements OperationHandler { @Override public void handle(ShopOperation shopOperation) { - String fruit = shopOperation.getFruit(); - int quantity = Storage.fruitsStorage.get(fruit); - Storage.fruitsStorage.put(fruit, quantity + shopOperation.getQuantity()); + try { + String fruit = shopOperation.getFruit(); + int quantity = Storage.fruitsStorage.get(fruit); + Storage.fruitsStorage.put(fruit, quantity + shopOperation.getQuantity()); + } catch (NullPointerException e) { + throw new OperationException("Operation is not correct: " + shopOperation); + } + } } diff --git a/src/main/java/core/basesyntax/strategy/SupplyOperation.java b/src/main/java/core/basesyntax/strategy/SupplyOperation.java index 6ffe92f2d5..f94d524803 100644 --- a/src/main/java/core/basesyntax/strategy/SupplyOperation.java +++ b/src/main/java/core/basesyntax/strategy/SupplyOperation.java @@ -2,12 +2,17 @@ import core.basesyntax.db.Storage; import core.basesyntax.model.ShopOperation; +import exception.OperationException; public class SupplyOperation implements OperationHandler { @Override public void handle(ShopOperation shopOperation) { - String fruit = shopOperation.getFruit(); - int quantity = Storage.fruitsStorage.get(fruit); - Storage.fruitsStorage.put(fruit, quantity + shopOperation.getQuantity()); + try { + String fruit = shopOperation.getFruit(); + int quantity = Storage.fruitsStorage.get(fruit); + Storage.fruitsStorage.put(fruit, quantity + shopOperation.getQuantity()); + } catch (NullPointerException e) { + throw new OperationException("Operation is not correct: " + shopOperation); + } } } From a765ebbd2a81148e83d063322d3022225b3a01a8 Mon Sep 17 00:00:00 2001 From: Oleh Date: Sat, 2 Nov 2024 20:57:32 +0200 Subject: [PATCH 4/5] fixed checkstyle --- .../basesyntax/service/impl/StrategyOperationServiceImpl.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/core/basesyntax/service/impl/StrategyOperationServiceImpl.java b/src/main/java/core/basesyntax/service/impl/StrategyOperationServiceImpl.java index 1aefe1bb07..2e2c512329 100644 --- a/src/main/java/core/basesyntax/service/impl/StrategyOperationServiceImpl.java +++ b/src/main/java/core/basesyntax/service/impl/StrategyOperationServiceImpl.java @@ -4,7 +4,6 @@ import core.basesyntax.service.StrategyOperationService; import core.basesyntax.strategy.OperationHandler; import exception.OperationException; - import java.util.Map; public class StrategyOperationServiceImpl implements StrategyOperationService { From 7265e20c09b76e72389b1435bc5d734ca5f785c4 Mon Sep 17 00:00:00 2001 From: Oleh Date: Mon, 4 Nov 2024 12:32:10 +0200 Subject: [PATCH 5/5] added fixes in exceptions. Remove try/catch blocks but added a check if the key is in the Storage to prevent NPE. Is this check necessary or unnecessary? --- src/main/java/core/basesyntax/db/Storage.java | 4 ---- .../core/basesyntax/file/FileReaderImpl.java | 2 +- .../core/basesyntax/file/FileWriterImpl.java | 2 +- .../impl/StrategyOperationServiceImpl.java | 5 +++-- .../strategy/PurchaseOperation.java | 20 +++++++++---------- .../basesyntax/strategy/ReturnOperation.java | 10 ++++------ .../basesyntax/strategy/SupplyOperation.java | 9 ++++----- src/main/java/exception/FileException.java | 4 ++-- 8 files changed, 24 insertions(+), 32 deletions(-) diff --git a/src/main/java/core/basesyntax/db/Storage.java b/src/main/java/core/basesyntax/db/Storage.java index 2bfec8101d..9d385af657 100644 --- a/src/main/java/core/basesyntax/db/Storage.java +++ b/src/main/java/core/basesyntax/db/Storage.java @@ -5,8 +5,4 @@ public class Storage { public static final Map fruitsStorage = new HashMap<>(); - - public Map getFruitsStorage() { - return fruitsStorage; - } } diff --git a/src/main/java/core/basesyntax/file/FileReaderImpl.java b/src/main/java/core/basesyntax/file/FileReaderImpl.java index b73d77d728..17b1169f87 100644 --- a/src/main/java/core/basesyntax/file/FileReaderImpl.java +++ b/src/main/java/core/basesyntax/file/FileReaderImpl.java @@ -13,7 +13,7 @@ public List read(String filePath) { try { reportList = Files.readAllLines(Path.of(filePath)); } catch (IOException e) { - throw new FileException("Can't read data from file: " + filePath); + throw new FileException("Can't read data from file: " + filePath, e); } return reportList; } diff --git a/src/main/java/core/basesyntax/file/FileWriterImpl.java b/src/main/java/core/basesyntax/file/FileWriterImpl.java index 38eff38133..09ea9a89ae 100644 --- a/src/main/java/core/basesyntax/file/FileWriterImpl.java +++ b/src/main/java/core/basesyntax/file/FileWriterImpl.java @@ -11,7 +11,7 @@ public void write(String filePath, String report) { try { Files.writeString(Path.of(filePath), report); } catch (IOException e) { - throw new FileException("Can't write data to file: " + filePath); + throw new FileException("Can't write data to file: " + filePath, e); } } } diff --git a/src/main/java/core/basesyntax/service/impl/StrategyOperationServiceImpl.java b/src/main/java/core/basesyntax/service/impl/StrategyOperationServiceImpl.java index 2e2c512329..d7ddfdbf1a 100644 --- a/src/main/java/core/basesyntax/service/impl/StrategyOperationServiceImpl.java +++ b/src/main/java/core/basesyntax/service/impl/StrategyOperationServiceImpl.java @@ -15,8 +15,9 @@ public StrategyOperationServiceImpl(Map operati @Override public OperationHandler get(OperationType operationType) { - if (operationHandlerMap.get(operationType) != null) { - return operationHandlerMap.get(operationType); + OperationHandler handler = operationHandlerMap.get(operationType); + if (handler != null) { + return handler; } throw new OperationException("Operation type is not correct: " + operationType); } diff --git a/src/main/java/core/basesyntax/strategy/PurchaseOperation.java b/src/main/java/core/basesyntax/strategy/PurchaseOperation.java index f025c4096f..a9a52d77af 100644 --- a/src/main/java/core/basesyntax/strategy/PurchaseOperation.java +++ b/src/main/java/core/basesyntax/strategy/PurchaseOperation.java @@ -7,18 +7,16 @@ public class PurchaseOperation implements OperationHandler { @Override public void handle(ShopOperation shopOperation) { - try { - String fruit = shopOperation.getFruit(); - int quantity = Storage.fruitsStorage.get(fruit); - int quantityToBuy = shopOperation.getQuantity(); - if (quantity < quantityToBuy) { - throw new OperationException("Operation is impossible, not enough quantity of: " - + fruit); - } - Storage.fruitsStorage.put(fruit, quantity - quantityToBuy); - } catch (NullPointerException e) { + String fruit = shopOperation.getFruit(); + if (!Storage.fruitsStorage.containsKey(fruit)) { throw new OperationException("Operation is not correct: " + shopOperation); } - + int quantity = Storage.fruitsStorage.get(fruit); + int quantityToBuy = shopOperation.getQuantity(); + if (quantity < quantityToBuy) { + throw new OperationException("Operation is impossible, not enough quantity of: " + + fruit); + } + Storage.fruitsStorage.put(fruit, quantity - quantityToBuy); } } diff --git a/src/main/java/core/basesyntax/strategy/ReturnOperation.java b/src/main/java/core/basesyntax/strategy/ReturnOperation.java index 02ae87bd34..fe652503a1 100644 --- a/src/main/java/core/basesyntax/strategy/ReturnOperation.java +++ b/src/main/java/core/basesyntax/strategy/ReturnOperation.java @@ -7,13 +7,11 @@ public class ReturnOperation implements OperationHandler { @Override public void handle(ShopOperation shopOperation) { - try { - String fruit = shopOperation.getFruit(); - int quantity = Storage.fruitsStorage.get(fruit); - Storage.fruitsStorage.put(fruit, quantity + shopOperation.getQuantity()); - } catch (NullPointerException e) { + String fruit = shopOperation.getFruit(); + if (!Storage.fruitsStorage.containsKey(fruit)) { throw new OperationException("Operation is not correct: " + shopOperation); } - + int quantity = Storage.fruitsStorage.get(fruit); + Storage.fruitsStorage.put(fruit, quantity + shopOperation.getQuantity()); } } diff --git a/src/main/java/core/basesyntax/strategy/SupplyOperation.java b/src/main/java/core/basesyntax/strategy/SupplyOperation.java index f94d524803..aa17a9834d 100644 --- a/src/main/java/core/basesyntax/strategy/SupplyOperation.java +++ b/src/main/java/core/basesyntax/strategy/SupplyOperation.java @@ -7,12 +7,11 @@ public class SupplyOperation implements OperationHandler { @Override public void handle(ShopOperation shopOperation) { - try { - String fruit = shopOperation.getFruit(); - int quantity = Storage.fruitsStorage.get(fruit); - Storage.fruitsStorage.put(fruit, quantity + shopOperation.getQuantity()); - } catch (NullPointerException e) { + String fruit = shopOperation.getFruit(); + if (!Storage.fruitsStorage.containsKey(fruit)) { throw new OperationException("Operation is not correct: " + shopOperation); } + int quantity = Storage.fruitsStorage.get(fruit); + Storage.fruitsStorage.put(fruit, quantity + shopOperation.getQuantity()); } } diff --git a/src/main/java/exception/FileException.java b/src/main/java/exception/FileException.java index b38950a16f..2f0284d875 100644 --- a/src/main/java/exception/FileException.java +++ b/src/main/java/exception/FileException.java @@ -1,7 +1,7 @@ package exception; public class FileException extends RuntimeException { - public FileException(String message) { - super(message); + public FileException(String message, Throwable cause) { + super(message, cause); } }