-
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
Develop #1289
base: master
Are you sure you want to change the base?
Develop #1289
Changes from 9 commits
c109bec
376f28d
40d5531
13dfcc5
5acef2f
8721977
6ff2aa5
22cac8c
411e32d
ad9e05b
3df4ac3
e794b88
f386dad
db24169
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.infrastructure.DataConverter; | ||
import core.basesyntax.infrastructure.DataConverterImpl; | ||
import core.basesyntax.infrastructure.db.FileReader; | ||
import core.basesyntax.infrastructure.db.FileReaderImpl; | ||
import core.basesyntax.infrastructure.db.FileWriter; | ||
import core.basesyntax.infrastructure.db.FileWriterImpl; | ||
import core.basesyntax.service.FruitTransaction; | ||
import core.basesyntax.service.OperationStrategy; | ||
import core.basesyntax.service.OperationStrategyImpl; | ||
import core.basesyntax.service.ReportGenerator; | ||
import core.basesyntax.service.ReportGeneratorImpl; | ||
import core.basesyntax.service.ShopService; | ||
import core.basesyntax.service.ShopServiceImpl; | ||
import core.basesyntax.service.operations.BalanceOperation; | ||
import core.basesyntax.service.operations.OperationHandler; | ||
import core.basesyntax.service.operations.PurchaseOperation; | ||
import core.basesyntax.service.operations.ReturnOperation; | ||
import core.basesyntax.service.operations.SupplyOperation; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class Main { | ||
private static final String OPERATION_LIST_FILE_PATH | ||
= "src/main/java/core/basesyntax/resources/operationslist.csv"; | ||
private static final String DB_FILE_PATH | ||
= "src/main/java/core/basesyntax/resources/database.csv"; | ||
|
||
public static void main(String[] arg) { | ||
FileReader fileReader = new FileReaderImpl(); | ||
List<String> inputReport = fileReader.read(OPERATION_LIST_FILE_PATH); | ||
|
||
DataConverter dataConverter = new DataConverterImpl(); | ||
|
||
Map<FruitTransaction.Operation, OperationHandler> operationHandlers = new HashMap<>(); | ||
operationHandlers.put(FruitTransaction.Operation.BALANCE, new BalanceOperation()); | ||
operationHandlers.put(FruitTransaction.Operation.PURCHASE, new PurchaseOperation()); | ||
operationHandlers.put(FruitTransaction.Operation.RETURN, new ReturnOperation()); | ||
operationHandlers.put(FruitTransaction.Operation.SUPPLY, new SupplyOperation()); | ||
OperationStrategy operationStrategy = new OperationStrategyImpl(operationHandlers); | ||
|
||
List<FruitTransaction> transactions = dataConverter.convertToTransaction(inputReport); | ||
ShopService shopService = new ShopServiceImpl(operationStrategy); | ||
shopService.process(transactions); | ||
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 variable 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 variable |
||
|
||
ReportGenerator reportGenerator = new ReportGeneratorImpl(); | ||
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 class |
||
String resultingReport = reportGenerator.getReport(); | ||
System.out.println(resultingReport); | ||
|
||
FileWriter fileWriter = new FileWriterImpl(); | ||
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 class |
||
fileWriter.write(resultingReport, DB_FILE_PATH); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package core.basesyntax.infrastructure; | ||
|
||
import core.basesyntax.service.FruitTransaction; | ||
import java.util.List; | ||
|
||
public interface DataConverter { | ||
List<FruitTransaction> convertToTransaction(List<String> inputReport); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package core.basesyntax.infrastructure; | ||
|
||
import core.basesyntax.service.FruitTransaction; | ||
import java.util.List; | ||
|
||
public class DataConverterImpl implements DataConverter { | ||
private static final String SEPARATOR = ","; | ||
private static final int OPERATION_INDEX = 0; | ||
private static final int FRUIT_NAME_INDEX = 1; | ||
private static final int CAPACITY_INDEX = 2; | ||
|
||
@Override | ||
public List<FruitTransaction> convertToTransaction(List<String> inputReport) { | ||
return inputReport.stream() | ||
.map(s -> s.split(SEPARATOR)) | ||
.filter(s -> s.length == 3 && isNumeric(s[2])) | ||
.map(string -> new FruitTransaction( | ||
FruitTransaction.getOperation(string[OPERATION_INDEX]), | ||
string[FRUIT_NAME_INDEX], Integer.parseInt(string[CAPACITY_INDEX]))) | ||
.toList(); | ||
MykolaTurak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private boolean isNumeric(String str) { | ||
try { | ||
Integer.parseInt(str); | ||
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. What happens if quantity is negative?) |
||
return true; | ||
} catch (NumberFormatException e) { | ||
return false; | ||
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. let's throw an exception with message in this case |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax.infrastructure.db; | ||
|
||
import java.util.List; | ||
|
||
public interface FileReader { | ||
List<String> read(String fileName); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package core.basesyntax.infrastructure.db; | ||
|
||
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 fileName) { | ||
try { | ||
return Files.readAllLines(Path.of(fileName)); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Can't read from file"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax.infrastructure.db; | ||
|
||
public interface FileWriter { | ||
void write(String resultingReport, String fileName); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package core.basesyntax.infrastructure.db; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
|
||
public class FileWriterImpl implements FileWriter { | ||
@Override | ||
public void write(String resultingReport, String fileName) { | ||
try (BufferedWriter br = new BufferedWriter(new java.io.FileWriter(fileName))) { | ||
br.write(resultingReport); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Can't open the file: " + fileName, e); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package core.basesyntax.infrastructure.db; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Storage { | ||
public static final Map<String, Integer> STORAGE = new HashMap<>(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
banana,152 | ||
apple,90 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
b,banana,20 | ||
b,apple,100 | ||
s,banana,100 | ||
p,banana,13 | ||
r,apple,10 | ||
p,apple,20 | ||
p,banana,5 | ||
s,banana,50 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package core.basesyntax.service; | ||
|
||
public class FruitTransaction { | ||
private Operation operation; | ||
private String fruit; | ||
private int quantity; | ||
|
||
public FruitTransaction(Operation operation, String fruit, int quantity) { | ||
this.operation = operation; | ||
this.fruit = fruit; | ||
this.quantity = quantity; | ||
} | ||
|
||
public static Operation getOperation(String code) { | ||
for (Operation value : Operation.values()) { | ||
if (value.getCode().equals(code)) { | ||
return value; | ||
} | ||
} | ||
throw new IllegalArgumentException(code + " operation doesn't exist."); | ||
} | ||
|
||
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; | ||
} | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.service.operations.OperationHandler; | ||
|
||
public interface OperationStrategy { | ||
OperationHandler execute(FruitTransaction fruitTransaction); | ||
MykolaTurak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.service.operations.OperationHandler; | ||
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 execute(FruitTransaction fruitTransaction) { | ||
return operationHandlerMap.get(fruitTransaction.getOperation()); | ||
} | ||
} |
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,16 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.infrastructure.db.Storage; | ||
import java.util.stream.Collectors; | ||
|
||
public class ReportGeneratorImpl implements ReportGenerator { | ||
private static final String SEPARATOR = ","; | ||
|
||
@Override | ||
public String getReport() { | ||
return Storage.STORAGE.entrySet().stream() | ||
.map(f -> f.getKey() + SEPARATOR + f.getValue()) | ||
MykolaTurak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.collect(Collectors.joining(System.lineSeparator())); | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax.service; | ||
|
||
import java.util.List; | ||
|
||
public interface ShopService { | ||
void process(List<FruitTransaction> fruitTransactions); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.service.operations.OperationHandler; | ||
import java.util.List; | ||
|
||
public class ShopServiceImpl implements ShopService { | ||
private final OperationStrategy operationStrategy; | ||
|
||
public ShopServiceImpl(OperationStrategy operationStrategy) { | ||
this.operationStrategy = operationStrategy; | ||
} | ||
|
||
@Override | ||
public void process(List<FruitTransaction> fruitTransactions) { | ||
for (FruitTransaction transaction : fruitTransactions) { | ||
OperationHandler operation = operationStrategy.execute(transaction); | ||
operation.run(transaction); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package core.basesyntax.service.operations; | ||
|
||
import core.basesyntax.infrastructure.db.Storage; | ||
import core.basesyntax.service.FruitTransaction; | ||
|
||
public class BalanceOperation implements OperationHandler { | ||
|
||
@Override | ||
public void run(FruitTransaction fruitTransaction) { | ||
Storage.STORAGE.put(fruitTransaction.getFruit(), fruitTransaction.getQuantity()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax.service.operations; | ||
|
||
import core.basesyntax.service.FruitTransaction; | ||
|
||
public interface OperationHandler { | ||
void run(FruitTransaction fruitTransaction); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package core.basesyntax.service.operations; | ||
|
||
import core.basesyntax.infrastructure.db.Storage; | ||
import core.basesyntax.service.FruitTransaction; | ||
import java.util.NoSuchElementException; | ||
|
||
public class PurchaseOperation implements OperationHandler { | ||
|
||
@Override | ||
public void run(FruitTransaction fruitTransaction) { | ||
if (Storage.STORAGE.get(fruitTransaction.getFruit()) == null) { | ||
throw new NoSuchElementException("Can't find fruit: " + fruitTransaction.getFruit()); | ||
} | ||
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. Let's move it to the default or static method in OperationHandler to avoid code duplication |
||
if (Storage.STORAGE.get(fruitTransaction.getFruit()) < fruitTransaction.getQuantity()) { | ||
throw new RuntimeException("Too little of product: " + fruitTransaction.getFruit()); | ||
} | ||
Storage.STORAGE.replace(fruitTransaction.getFruit(), | ||
Storage.STORAGE.get(fruitTransaction.getFruit()) - fruitTransaction.getQuantity()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package core.basesyntax.service.operations; | ||
|
||
import core.basesyntax.infrastructure.db.Storage; | ||
import core.basesyntax.service.FruitTransaction; | ||
import java.util.NoSuchElementException; | ||
|
||
public class ReturnOperation implements OperationHandler { | ||
@Override | ||
public void run(FruitTransaction fruitTransaction) { | ||
if (Storage.STORAGE.get(fruitTransaction.getFruit()) == null) { | ||
throw new NoSuchElementException("Can't find fruit: " + fruitTransaction.getFruit()); | ||
} | ||
Storage.STORAGE.replace(fruitTransaction.getFruit(), | ||
Storage.STORAGE.get(fruitTransaction.getFruit()) + fruitTransaction.getQuantity()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package core.basesyntax.service.operations; | ||
|
||
import core.basesyntax.infrastructure.db.Storage; | ||
import core.basesyntax.service.FruitTransaction; | ||
import java.util.NoSuchElementException; | ||
|
||
public class SupplyOperation implements OperationHandler { | ||
@Override | ||
public void run(FruitTransaction fruitTransaction) { | ||
if (Storage.STORAGE.get(fruitTransaction.getFruit()) == null) { | ||
throw new NoSuchElementException("Can't find fruit: " + fruitTransaction.getFruit()); | ||
} | ||
Storage.STORAGE.replace(fruitTransaction.getFruit(), | ||
Storage.STORAGE.get(fruitTransaction.getFruit()) + fruitTransaction.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. Let's use |
||
} | ||
} |
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.
resources folder should be placed in
src/main/resources