-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
used service, strategy packages #960
base: master
Are you sure you want to change the base?
Changes from all commits
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,59 @@ | ||
package core.basesyntax; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.service.FruitShopService; | ||
import core.basesyntax.service.ParseOperationService; | ||
import core.basesyntax.service.ReaderService; | ||
import core.basesyntax.service.ReportService; | ||
import core.basesyntax.service.WriterService; | ||
import core.basesyntax.service.impl.FruitShopServiceImpl; | ||
import core.basesyntax.service.impl.ParseOperationServiceImpl; | ||
import core.basesyntax.service.impl.ReaderServiceImpl; | ||
import core.basesyntax.service.impl.ReportServiceImpl; | ||
import core.basesyntax.service.impl.WriterServiceImpl; | ||
import core.basesyntax.strategy.BalanceOperationHandlerImpl; | ||
import core.basesyntax.strategy.OperationHandler; | ||
import core.basesyntax.strategy.OperationStrategy; | ||
import core.basesyntax.strategy.OperationStrategyImpl; | ||
import core.basesyntax.strategy.PurchaseOperationHandlerImpl; | ||
import core.basesyntax.strategy.ReturnOperationHandlerImpl; | ||
import core.basesyntax.strategy.SupplyOperationHandlerImpl; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class Main { | ||
private static final String INPUT_FILE_PATH = "src/main/resources/input.csv"; | ||
private static final String OUTPUT_FILE_PATH = "src/main/resources/output.csv"; | ||
|
||
public static void main(String[] args) { | ||
Map<FruitTransaction.Operation, OperationHandler> operationHandlerMap = new HashMap<>(); | ||
operationHandlerMap.put(FruitTransaction.Operation.BALANCE, | ||
new BalanceOperationHandlerImpl()); | ||
operationHandlerMap.put(FruitTransaction.Operation.PURCHASE, | ||
new PurchaseOperationHandlerImpl()); | ||
operationHandlerMap.put(FruitTransaction.Operation.SUPPLY, | ||
new SupplyOperationHandlerImpl()); | ||
operationHandlerMap.put(FruitTransaction.Operation.RETURN, | ||
new ReturnOperationHandlerImpl()); | ||
|
||
ReaderService readerService = new ReaderServiceImpl(); | ||
List<String> inputData = readerService.readFromFile(INPUT_FILE_PATH); | ||
|
||
ParseOperationService transactionService = new ParseOperationServiceImpl(); | ||
List<FruitTransaction> fruitTransactions = | ||
transactionService.parseContentForOperations(inputData); | ||
|
||
OperationStrategy operationStrategy = new OperationStrategyImpl(operationHandlerMap); | ||
FruitShopService transactionHandler = new FruitShopServiceImpl(operationStrategy); | ||
transactionHandler.processOfOperations(fruitTransactions); | ||
|
||
ReportService reportService = new ReportServiceImpl(); | ||
String report = reportService.report(); | ||
System.out.println(report); | ||
|
||
WriterService writerService = new WriterServiceImpl(); | ||
writerService.writeToFile(OUTPUT_FILE_PATH, report); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package core.basesyntax.db; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class FruitStorage { | ||
public static final Map<String, Integer> fruitQuantities = new HashMap<>(); | ||
} | ||
|
||
|
||
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,59 @@ | ||||
package core.basesyntax.model; | ||||
|
||||
import java.util.Arrays; | ||||
|
||||
public class FruitTransaction { | ||||
|
||||
private Operation operation; | ||||
private String fruit; | ||||
private int quantity; | ||||
|
||||
public Operation getOperation() { | ||||
return operation; | ||||
} | ||||
|
||||
public void setOperation(Operation operation) { | ||||
this.operation = operation; | ||||
} | ||||
|
||||
public String getFruit() { | ||||
return fruit; | ||||
} | ||||
|
||||
public void setFruit(String fruit) { | ||||
this.fruit = fruit; | ||||
} | ||||
|
||||
public int getQuantity() { | ||||
return quantity; | ||||
} | ||||
|
||||
public void setQuantity(int quantity) { | ||||
this.quantity = quantity; | ||||
} | ||||
|
||||
public enum Operation { | ||||
BALANCE("b"), | ||||
SUPPLY("s"), | ||||
PURCHASE("p"), | ||||
RETURN("r"); | ||||
|
||||
private String code; | ||||
|
||||
Operation(String code) { | ||||
this.code = code; | ||||
} | ||||
|
||||
public String getCode() { | ||||
return code; | ||||
} | ||||
|
||||
public static Operation getOperationByCode(String code) { | ||||
return Arrays.stream(values()) | ||||
.filter(operation -> operation.getCode().equals(code)) | ||||
.findAny() | ||||
.orElseThrow(() -> new RuntimeException("Invalid operation code " + code)); | ||||
} | ||||
} | ||||
} | ||||
|
||||
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. line
Suggested change
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.List; | ||
|
||
public interface FruitShopService { | ||
void processOfOperations(List<FruitTransaction> parsedData); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.List; | ||
|
||
public interface ParseOperationService { | ||
List<FruitTransaction> parseContentForOperations(List<String> inputData); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax.service; | ||
|
||
import java.util.List; | ||
|
||
public interface ReaderService { | ||
List<String> readFromFile(String filePath); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax.service; | ||
|
||
public interface ReportService { | ||
String report(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax.service; | ||
|
||
public interface WriterService { | ||
void writeToFile(String filePath, String data); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package core.basesyntax.service.impl; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.service.FruitShopService; | ||
import core.basesyntax.strategy.OperationStrategy; | ||
import java.util.List; | ||
|
||
public class FruitShopServiceImpl implements FruitShopService { | ||
private final OperationStrategy operationStrategy; | ||
|
||
public FruitShopServiceImpl(OperationStrategy operationStrategy) { | ||
this.operationStrategy = operationStrategy; | ||
} | ||
|
||
@Override | ||
public void processOfOperations(List<FruitTransaction> parsedData) { | ||
for (FruitTransaction line : parsedData) { | ||
operationStrategy.get(line.getOperation()).process(line); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package core.basesyntax.service.impl; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.service.ParseOperationService; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ParseOperationServiceImpl implements ParseOperationService { | ||
private static final String SEPARATOR = ","; | ||
private static final int OPERATION_INDEX = 0; | ||
private static final int FRUIT_INDEX = 1; | ||
private static final int QUANTITY_INDEX = 2; | ||
private static final int POINT_TO_START_READING = 1; | ||
|
||
@Override | ||
public List<FruitTransaction> parseContentForOperations(List<String> inputData) { | ||
List<FruitTransaction> parsedData = new ArrayList<>(); | ||
for (int i = 1; i < inputData.size(); i++) { | ||
parsedData.add(parseInputData(inputData.get(i))); | ||
} | ||
return parsedData; | ||
} | ||
|
||
private FruitTransaction parseInputData(String line) { | ||
FruitTransaction fruitTransaction = new FruitTransaction( | ||
); | ||
String[] separetedLine = line.split(SEPARATOR); | ||
fruitTransaction.setOperation(FruitTransaction.Operation | ||
.getOperationByCode(separetedLine[OPERATION_INDEX])); | ||
fruitTransaction.setFruit(separetedLine[FRUIT_INDEX]); | ||
fruitTransaction.setQuantity(Integer.parseInt(separetedLine[QUANTITY_INDEX])); | ||
return fruitTransaction; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package core.basesyntax.service.impl; | ||
|
||
import core.basesyntax.service.ReaderService; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
public class ReaderServiceImpl implements ReaderService { | ||
|
||
@Override | ||
public List<String> readFromFile(String filePath) { | ||
Path path = Path.of(filePath); | ||
try { | ||
return Files.readAllLines(path); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Can't read data from your amazing file " | ||
+ path + " !", e); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,20 @@ | ||||||
package core.basesyntax.service.impl; | ||||||
|
||||||
import core.basesyntax.db.FruitStorage; | ||||||
import core.basesyntax.service.ReportService; | ||||||
import java.util.Map; | ||||||
|
||||||
public class ReportServiceImpl implements ReportService { | ||||||
@Override | ||||||
public String report() { | ||||||
StringBuilder report = new StringBuilder("fruit,quantity").append(System.lineSeparator()); | ||||||
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
|
||||||
for (Map.Entry<String, Integer> entry : FruitStorage.fruitQuantities.entrySet()) { | ||||||
report.append(entry.getKey()) | ||||||
.append(",") | ||||||
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
|
||||||
.append(entry.getValue()) | ||||||
.append(System.lineSeparator()); | ||||||
} | ||||||
return report.toString(); | ||||||
} | ||||||
} | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package core.basesyntax.service.impl; | ||
|
||
import core.basesyntax.service.WriterService; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
public class WriterServiceImpl implements WriterService { | ||
@Override | ||
public void writeToFile(String filePath, String data) { | ||
try { | ||
Files.writeString(Path.of(filePath), data); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Can't write to file " + filePath, e); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,13 @@ | ||||
package core.basesyntax.strategy; | ||||
|
||||
import core.basesyntax.db.FruitStorage; | ||||
import core.basesyntax.model.FruitTransaction; | ||||
|
||||
public class BalanceOperationHandlerImpl implements OperationHandler { | ||||
|
||||
@Override | ||||
public void process(FruitTransaction transaction) { | ||||
FruitStorage.fruitQuantities.put(transaction.getFruit(), transaction.getQuantity()); | ||||
} | ||||
} | ||||
|
||||
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
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax.strategy; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
|
||
public interface OperationHandler { | ||
void process(FruitTransaction transaction); | ||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,8 @@ | ||||
package core.basesyntax.strategy; | ||||
|
||||
import core.basesyntax.model.FruitTransaction; | ||||
|
||||
public interface OperationStrategy { | ||||
OperationHandler get(FruitTransaction.Operation operation); | ||||
} | ||||
|
||||
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
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,18 @@ | ||||||
package core.basesyntax.strategy; | ||||||
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. Move the entire implementation from the
Suggested change
|
||||||
|
||||||
import core.basesyntax.model.FruitTransaction; | ||||||
import java.util.Map; | ||||||
|
||||||
public class OperationStrategyImpl implements OperationStrategy { | ||||||
private Map<FruitTransaction.Operation, OperationHandler> operationHandlerMap; | ||||||
|
||||||
public OperationStrategyImpl(Map<FruitTransaction.Operation, | ||||||
OperationHandler> operationHandlerMap) { | ||||||
this.operationHandlerMap = operationHandlerMap; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public OperationHandler get(FruitTransaction.Operation operation) { | ||||||
return operationHandlerMap.get(operation); | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package core.basesyntax.strategy; | ||
|
||
import core.basesyntax.db.FruitStorage; | ||
import core.basesyntax.model.FruitTransaction; | ||
|
||
public class PurchaseOperationHandlerImpl implements OperationHandler { | ||
|
||
@Override | ||
public void process(FruitTransaction transaction) { | ||
int currentQuantity = FruitStorage.fruitQuantities.get(transaction.getFruit()); | ||
if (currentQuantity < transaction.getQuantity()) { | ||
throw new RuntimeException("Not enough " + transaction.getFruit() | ||
+ " in stock for purchase."); | ||
} | ||
FruitStorage.fruitQuantities | ||
.put(transaction.getFruit(), currentQuantity - transaction.getQuantity()); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,15 @@ | ||||
package core.basesyntax.strategy; | ||||
|
||||
import core.basesyntax.db.FruitStorage; | ||||
import core.basesyntax.model.FruitTransaction; | ||||
|
||||
public class ReturnOperationHandlerImpl implements OperationHandler { | ||||
|
||||
@Override | ||||
public void process(FruitTransaction transaction) { | ||||
int oldQuantity = FruitStorage.fruitQuantities.get(transaction.getFruit()); | ||||
FruitStorage.fruitQuantities | ||||
.put(transaction.getFruit(), oldQuantity + transaction.getQuantity()); | ||||
} | ||||
} | ||||
|
||||
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
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package core.basesyntax.strategy; | ||
|
||
import core.basesyntax.db.FruitStorage; | ||
import core.basesyntax.model.FruitTransaction; | ||
|
||
public class SupplyOperationHandlerImpl implements OperationHandler { | ||
|
||
@Override | ||
public void process(FruitTransaction transaction) { | ||
int oldQuantity = FruitStorage.fruitQuantities.get(transaction.getFruit()); | ||
FruitStorage.fruitQuantities | ||
.put(transaction.getFruit(), oldQuantity + transaction.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.
lines