-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hw 1 solution #1233
base: master
Are you sure you want to change the base?
Hw 1 solution #1233
Changes from 2 commits
11f9324
5fe8301
74adf83
a765ebb
7265e20
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = "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(); | ||
List<String> inputReport = fileReader.read(PATH_TO_FILE); | ||
|
||
DataConverter dataConverter = new DataConverterImpl(); | ||
|
||
Map<OperationType, OperationHandler> 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<ShopOperation> 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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package core.basesyntax.db; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Storage { | ||
public static final Map<String, Integer> fruitsStorage = new HashMap<>(); | ||
|
||
public Map<String, Integer> getFruitsStorage() { | ||
return fruitsStorage; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning a direct reference to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exposing the internal storage map directly through |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax.file; | ||
|
||
import java.util.List; | ||
|
||
public interface FileReader { | ||
List<String> read(String filePath); | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,20 @@ | ||||||
package core.basesyntax.file; | ||||||
|
||||||
import exception.FileException; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that the |
||||||
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<String> read(String filePath) { | ||||||
List<String> reportList; | ||||||
try { | ||||||
reportList = Files.readAllLines(Path.of(filePath)); | ||||||
} catch (IOException e) { | ||||||
throw new FileException("Can't read data from file: " + filePath); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider including the original exception There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
return reportList; | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax.file; | ||
|
||
public interface FileWriter { | ||
void write(String filePath, String report); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package core.basesyntax.file; | ||
|
||
import exception.FileException; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that the |
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider including the original exception There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the same |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package core.basesyntax.model; | ||
|
||
import exception.OperationException; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that the |
||
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that the |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 + '}'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.model.ShopOperation; | ||
import java.util.List; | ||
|
||
public interface DataConverter { | ||
List<ShopOperation> convertToOperation(List<String> reportList); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax.service; | ||
|
||
public interface ReportGenerator { | ||
String getReport(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.model.ShopOperation; | ||
import java.util.List; | ||
|
||
public interface ShopService { | ||
void process(List<ShopOperation> operations); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ShopOperation> convertToOperation(List<String> reportList) { | ||
List<ShopOperation> shopOperationList = new ArrayList<>(); | ||
for (String line : reportList.subList(LIST_START, reportList.size())) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method skips the first line of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method assumes that the input list |
||
String[] parts = line.split(COMMA); | ||
String codeOperation = parts[OPERATION_INDEX]; | ||
String fruit = parts[FRUIT_INDEX]; | ||
int quantity = Integer.parseInt(parts[QUANTITY_INDEX]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding error handling for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding error handling for |
||
shopOperationList.add( | ||
new ShopOperation(OperationType.getOperationType(codeOperation), | ||
fruit, quantity)); | ||
} | ||
return shopOperationList; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Integer> entry : Storage.fruitsStorage.entrySet()) { | ||
stringBuilder.append(entry.getKey()) | ||
.append(COMMA) | ||
.append(entry.getValue()) | ||
.append(LINE_SEPARATOR); | ||
} | ||
return stringBuilder.toString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ShopOperation> operations) { | ||
for (ShopOperation operation : operations) { | ||
strategyOperationService.get(operation.getType()).handle(operation); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<OperationType, OperationHandler> operationHandlerMap; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider making |
||
|
||
public StrategyOperationServiceImpl(Map<OperationType, OperationHandler> operationHandlerMap) { | ||
this.operationHandlerMap = operationHandlerMap; | ||
} | ||
|
||
@Override | ||
public OperationHandler get(OperationType operationType) { | ||
return operationHandlerMap.get(operationType); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding a check to handle cases where |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax.strategy; | ||
|
||
import core.basesyntax.model.ShopOperation; | ||
|
||
public interface OperationHandler { | ||
void handle(ShopOperation shopOperation); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding a check to handle cases where the fruit is not present in the |
||
int quantityToBuy = shopOperation.getQuantity(); | ||
if (quantity < quantityToBuy) { | ||
throw new OperationException("Operation is impossible, not enough quantity of: " | ||
+ fruit); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that the |
||
} | ||
Storage.fruitsStorage.put(fruit, quantity - quantityToBuy); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding a check to handle cases where the fruit is not present in the |
||
Storage.fruitsStorage.put(fruit, quantity + shopOperation.getQuantity()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding a check to handle cases where the fruit is not present in the |
||
Storage.fruitsStorage.put(fruit, quantity + shopOperation.getQuantity()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove this method because your map is public ;)