-
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
first_attempt #1283
base: master
Are you sure you want to change the base?
first_attempt #1283
Changes from 3 commits
81613ec
2ef4a44
792d2f3
cf778d7
7917cd7
f29914c
8024732
92aae01
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,50 @@ | ||
package core.basesyntax; | ||
|
||
import core.basesyntax.dao.FileReaderImpl; | ||
import core.basesyntax.dao.FileReaderMy; | ||
import core.basesyntax.dao.FileWriterImpl; | ||
import core.basesyntax.dao.FileWriterMy; | ||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.model.ReportGenerator; | ||
import core.basesyntax.model.ReportGeneratorImpl; | ||
import core.basesyntax.model.converter.DataConverter; | ||
import core.basesyntax.model.converter.DataConverterImpl; | ||
import core.basesyntax.model.handler.BalanceOperation; | ||
import core.basesyntax.model.handler.OperationHandler; | ||
import core.basesyntax.model.handler.PurchaseOperation; | ||
import core.basesyntax.model.handler.ReturnOperation; | ||
import core.basesyntax.model.handler.SupplyOperation; | ||
import core.basesyntax.model.strategy.OperationStrategy; | ||
import core.basesyntax.model.strategy.OperationStrategyImpl; | ||
import core.basesyntax.service.ShopService; | ||
import core.basesyntax.service.ShopServiceImpl; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
FileReaderMy fileReader = new FileReaderImpl(); | ||
List<String> inputReport = fileReader.read("src/main/resources/reportToRead.csv"); | ||
|
||
DataConverter dataConverter = new DataConverterImpl(); | ||
List<FruitTransaction> transactions = dataConverter.convertToTransaction(inputReport); | ||
|
||
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); | ||
|
||
ShopService shopService = new ShopServiceImpl(operationStrategy); | ||
Map<String, Integer> storage = shopService.process(transactions); | ||
|
||
ReportGenerator reportGenerator = new ReportGeneratorImpl(); | ||
String resultingReport = reportGenerator.getReport(storage); | ||
|
||
FileWriterMy fileWriter = new FileWriterImpl(); | ||
fileWriter.write("src/main/resources/finalReport.csv",resultingReport); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package core.basesyntax.dao; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class FileReaderImpl implements FileReaderMy { | ||
public List<String> read(String fileName) { | ||
List<String> lines = new ArrayList<>(); | ||
try (BufferedReader reader = new BufferedReader(new java.io.FileReader(fileName))) { | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
lines.add(line); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException("Error reading file: " + fileName); | ||
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 exception handling here should include the exception object |
||
} | ||
return lines; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax.dao; | ||
|
||
import java.util.List; | ||
|
||
public interface FileReaderMy { | ||
List<String> read(String fileName); | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,15 @@ | ||||||
package core.basesyntax.dao; | ||||||
|
||||||
import java.io.BufferedWriter; | ||||||
import java.io.IOException; | ||||||
|
||||||
public class FileWriterImpl implements FileWriterMy { | ||||||
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 |
||||||
@Override | ||||||
public void write(String fileName,String info) { | ||||||
try (BufferedWriter bufferedWriter = new BufferedWriter(new java.io.FileWriter(fileName))) { | ||||||
bufferedWriter.write(info); | ||||||
} catch (IOException e) { | ||||||
throw new RuntimeException("Impossible to write in this file " + fileName, e); | ||||||
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
Make the message more formal |
||||||
} | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax.dao; | ||
|
||
public interface FileWriterMy { | ||
void write(String fileName,String info); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package core.basesyntax.model; | ||
|
||
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 Operation getOperation() { | ||
return operation; | ||
} | ||
|
||
public String getFruit() { | ||
return fruit; | ||
} | ||
|
||
public int getQuantity() { | ||
return 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 fromCode(String code) { | ||
for (Operation operation : Operation.values()) { | ||
if (operation.getCode().equals(code)) { | ||
return operation; | ||
} | ||
} | ||
throw new IllegalArgumentException("No enum constant with code " + code); | ||
} | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax.model; | ||
|
||
import java.util.Map; | ||
|
||
public interface ReportGenerator { | ||
String getReport(Map<String, Integer> storage); | ||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,19 @@ | ||||
package core.basesyntax.model; | ||||
|
||||
import java.util.Map; | ||||
import java.util.StringJoiner; | ||||
|
||||
public class ReportGeneratorImpl implements ReportGenerator { | ||||
|
||||
@Override | ||||
public String getReport(Map<String, Integer> storage) { | ||||
StringJoiner report = new StringJoiner("\n"); | ||||
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. better use System.lineseparator() |
||||
report.add("fruit,quantity"); | ||||
|
||||
for (Map.Entry<String, Integer> entry : storage.entrySet()) { | ||||
report.add(entry.getKey() + "," + entry.getValue()); | ||||
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. make header and separator constant fields |
||||
} | ||||
|
||||
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 report.toString(); | ||||
} | ||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package core.basesyntax.model.converter; | ||
|
||
import core.basesyntax.model.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,25 @@ | ||
package core.basesyntax.model.converter; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class DataConverterImpl implements DataConverter { | ||
@Override | ||
public List<FruitTransaction> convertToTransaction(List<String> inputReport) { | ||
List<FruitTransaction> transactions = new ArrayList<>(); | ||
|
||
inputReport.stream() | ||
.skip(1) | ||
.map(line -> line.split(",")) | ||
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. make separator constant field |
||
.filter(parts -> parts.length == 3) | ||
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. you should throw an exception for invalid data |
||
.map(parts -> new FruitTransaction( | ||
FruitTransaction.Operation.fromCode(parts[0]), | ||
parts[1], | ||
Integer.parseInt(parts[2]) | ||
)) | ||
.forEach(transactions::add); | ||
|
||
return transactions; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,11 @@ | ||||||||||
package core.basesyntax.model.handler; | ||||||||||
|
||||||||||
import core.basesyntax.model.FruitTransaction; | ||||||||||
import java.util.Map; | ||||||||||
|
||||||||||
public class BalanceOperation implements OperationHandler { | ||||||||||
@Override | ||||||||||
public void handle(Map<String, Integer> storage, FruitTransaction transaction) { | ||||||||||
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. Storage imitates real databases. So create a Storage class with a public static map |
||||||||||
storage.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,8 @@ | ||
package core.basesyntax.model.handler; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.Map; | ||
|
||
public interface OperationHandler { | ||
void handle(Map<String, Integer> storage, FruitTransaction transaction); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package core.basesyntax.model.handler; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.Map; | ||
|
||
public class PurchaseOperation implements OperationHandler { | ||
|
||
@Override | ||
public void handle(Map<String, Integer> storage, FruitTransaction transaction) { | ||
int currentBalance = storage.getOrDefault(transaction.getFruit(), 0); | ||
int newBalance = currentBalance - transaction.getQuantity(); | ||
|
||
if (newBalance < 0) { | ||
throw new RuntimeException("Insufficient balance for fruit: " + transaction.getFruit()); | ||
} | ||
|
||
storage.put(transaction.getFruit(), newBalance); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package core.basesyntax.model.handler; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.Map; | ||
|
||
public class ReturnOperation implements OperationHandler { | ||
@Override | ||
public void handle(Map<String, Integer> storage, FruitTransaction transaction) { | ||
storage.put(transaction.getFruit(), | ||
storage.getOrDefault(transaction.getFruit(), 0) + transaction.getQuantity()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package core.basesyntax.model.handler; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.Map; | ||
|
||
public class SupplyOperation implements OperationHandler { | ||
|
||
@Override | ||
public void handle(Map<String, Integer> storage, FruitTransaction transaction) { | ||
storage.put(transaction.getFruit(), | ||
storage.getOrDefault(transaction.getFruit(), 0) + transaction.getQuantity()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package core.basesyntax.model.strategy; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.model.handler.OperationHandler; | ||
|
||
public interface OperationStrategy { | ||
OperationHandler getOperationHandler(FruitTransaction.Operation operation); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package core.basesyntax.model.strategy; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.model.handler.OperationHandler; | ||
import java.util.Map; | ||
|
||
public class OperationStrategyImpl implements OperationStrategy { | ||
private final Map<FruitTransaction.Operation, OperationHandler> operationHandlers; | ||
|
||
public OperationStrategyImpl(Map<FruitTransaction.Operation, | ||
OperationHandler> operationHandlers) { | ||
this.operationHandlers = operationHandlers; | ||
} | ||
|
||
@Override | ||
public OperationHandler getOperationHandler(FruitTransaction.Operation operation) { | ||
return operationHandlers.get(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. better check that strategy return handler |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public interface ShopService { | ||
Map<String, Integer> process(List<FruitTransaction> fruitTransactionList); | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,27 @@ | ||||||
package core.basesyntax.service; | ||||||
|
||||||
import core.basesyntax.model.FruitTransaction; | ||||||
import core.basesyntax.model.strategy.OperationStrategy; | ||||||
import java.util.HashMap; | ||||||
import java.util.List; | ||||||
import java.util.Map; | ||||||
|
||||||
public class ShopServiceImpl implements ShopService { | ||||||
private final OperationStrategy operationStrategy; | ||||||
|
||||||
public ShopServiceImpl(OperationStrategy operationStrategy) { | ||||||
this.operationStrategy = operationStrategy; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public Map<String, Integer> process(List<FruitTransaction> 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.
Suggested change
use Storage |
||||||
Map<String, Integer> storage = new HashMap<>(); | ||||||
|
||||||
for (FruitTransaction transaction : transactions) { | ||||||
operationStrategy.getOperationHandler(transaction | ||||||
.getOperation()).handle(storage, transaction); | ||||||
} | ||||||
|
||||||
return storage; | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fruit,quantity | ||
banana,152 | ||
apple,90 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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.