-
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
create fruit shop #1229
base: master
Are you sure you want to change the base?
create fruit shop #1229
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,60 @@ | ||
package core.basesyntax; | ||
|
||
import core.basesyntax.file.DataConverter; | ||
import core.basesyntax.file.DataConverterImpl; | ||
import core.basesyntax.file.FileReader; | ||
import core.basesyntax.file.FileReaderImpl; | ||
import core.basesyntax.file.FileWriter; | ||
import core.basesyntax.file.FileWriterImpl; | ||
import core.basesyntax.service.FruitTransaction; | ||
import core.basesyntax.service.ReportGenerator; | ||
import core.basesyntax.service.ReportGeneratorImpl; | ||
import core.basesyntax.service.ShopService; | ||
import core.basesyntax.service.ShopServiceImpl; | ||
import core.basesyntax.strategy.BalanceOperation; | ||
import core.basesyntax.strategy.OperationHandler; | ||
import core.basesyntax.strategy.OperationStrategy; | ||
import core.basesyntax.strategy.OperationStrategyImpl; | ||
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 { | ||
public static void main(String[] args) { | ||
// 1. Read the data from the input CSV file | ||
FileReader fileReader = new FileReaderImpl(); | ||
List<String> inputReport = fileReader.read( | ||
"src/main/java/core/basesyntax/reportToRead.csv"); | ||
Comment on lines
+29
to
+30
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. Verify that the file path 'src/main/java/core/basesyntax/reportToRead.csv' is correct and that the file exists at this location. Otherwise, the |
||
|
||
// 2. Convert the incoming data into FruitTransactions list | ||
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. It seems like the step to convert the incoming data into a list of |
||
|
||
// 3. Create and feel the map with all OperationHandler implementations | ||
Map<FruitTransaction.Operation, OperationHandler> operationHandlers = new HashMap<>(); | ||
OperationStrategy operationStrategy = new OperationStrategyImpl(operationHandlers); | ||
ShopService shopService = new ShopServiceImpl(operationStrategy); | ||
operationHandlers.put(FruitTransaction.Operation.BALANCE, | ||
new BalanceOperation(shopService)); | ||
operationHandlers.put(FruitTransaction.Operation.PURCHASE, | ||
new PurchaseOperation(shopService)); | ||
operationHandlers.put(FruitTransaction.Operation.RETURN, | ||
new ReturnOperation(shopService)); | ||
operationHandlers.put(FruitTransaction.Operation.SUPPLY, | ||
new SupplyOperation(shopService)); | ||
|
||
// 4. Process the incoming transactions with applicable OperationHandler implementations | ||
DataConverter dataConverter = new DataConverterImpl(); | ||
List<FruitTransaction> transactions = dataConverter.convertToTransaction(inputReport); | ||
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 |
||
shopService.process(transactions); | ||
|
||
// 5.Generate report based on the current Storage state | ||
ReportGenerator reportGenerator = new ReportGeneratorImpl(); | ||
String resultingReport = reportGenerator.getReport(shopService); | ||
|
||
// 6. Write the received report into the destination file | ||
FileWriter fileWriter = new FileWriterImpl(); | ||
fileWriter.write(resultingReport, "src/main/java/core/basesyntax/finalReport.csv"); | ||
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. Verify that the file path 'src/main/java/core/basesyntax/finalReport.csv' is correct and that you have write permissions to this location. Otherwise, the |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package core.basesyntax.file; | ||
|
||
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,34 @@ | ||
package core.basesyntax.file; | ||
|
||
import core.basesyntax.service.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<>(); | ||
for (String line : inputReport) { | ||
if (line.startsWith("type")) { | ||
continue; | ||
} | ||
String[] parts = 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. Consider adding validation to check if the |
||
String operationCode = parts[0]; | ||
String fruit = parts[1]; | ||
int quantity = Integer.parseInt(parts[2]); | ||
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. Add 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 |
||
FruitTransaction.Operation operation = getOperationFromCode(operationCode); | ||
transactions.add(new FruitTransaction(operation, fruit, quantity)); | ||
} | ||
return transactions; | ||
} | ||
|
||
private FruitTransaction.Operation getOperationFromCode(String code) { | ||
for (FruitTransaction.Operation operation : FruitTransaction.Operation.values()) { | ||
if (operation.getCode().equals(code)) { | ||
return operation; | ||
} | ||
} | ||
throw new IllegalArgumentException("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. The method 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. Throwing an |
||
|
||
} | ||
} |
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,17 @@ | ||
package core.basesyntax.file; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
|
||
public class FileReaderImpl implements FileReader { | ||
@Override | ||
public List<String> read(String filePath) { | ||
try { | ||
return Files.readAllLines(Paths.get(filePath)); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Can't read file at " + filePath,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. Throwing a |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax.file; | ||
|
||
public interface FileWriter { | ||
void write(String resultingReport, String filePath); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package core.basesyntax.file; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
|
||
public class FileWriterImpl implements FileWriter { | ||
@Override | ||
public void write(String resultingReport, String filePath) { | ||
try (BufferedWriter bufferedWriter = | ||
new BufferedWriter(new java.io.FileWriter(filePath, false))) { | ||
bufferedWriter.write(resultingReport); | ||
bufferedWriter.newLine(); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Can't write data to file", 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. Throwing a |
||
} | ||
|
||
} | ||
} |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
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 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,5 @@ | ||
package core.basesyntax.service; | ||
|
||
public interface ReportGenerator { | ||
String getReport(ShopService shopService); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package core.basesyntax.service; | ||
|
||
import java.util.Map; | ||
|
||
public class ReportGeneratorImpl implements ReportGenerator { | ||
|
||
@Override | ||
public String getReport(ShopService shopService) { | ||
StringBuilder stringBuilder = new StringBuilder("fruit,quantity \n"); | ||
for (Map.Entry<String, Integer> entry: shopService.getFruits().entrySet()) { | ||
stringBuilder.append(entry.getKey()) | ||
.append(",") | ||
.append(entry.getValue()) | ||
.append("\n"); | ||
} | ||
return stringBuilder.toString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package core.basesyntax.service; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public interface ShopService { | ||
void process(List<FruitTransaction> transactions); | ||
|
||
void addFruits(String fruit, int quantity); | ||
|
||
int getQuantity(String fruit); | ||
|
||
Map<String, Integer> getFruits(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.strategy.OperationStrategy; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class ShopServiceImpl implements ShopService { | ||
private OperationStrategy operationStrategy; | ||
private Map<String, Integer> fruits = new HashMap<>(); | ||
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 using a thread-safe collection like |
||
|
||
public ShopServiceImpl(OperationStrategy operationStrategy) { | ||
this.operationStrategy = operationStrategy; | ||
} | ||
|
||
@Override | ||
public void process(List<FruitTransaction> transactions) { | ||
for (FruitTransaction transaction : transactions) { | ||
operationStrategy.get(transaction.getOperation()).handle(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. Ensure that |
||
} | ||
} | ||
|
||
@Override | ||
public void addFruits(String fruit, int quantity) { | ||
fruits.put(fruit,fruits.getOrDefault(fruit,0) + quantity); | ||
} | ||
|
||
@Override | ||
public int getQuantity(String fruit) { | ||
return fruits.getOrDefault(fruit,0); | ||
} | ||
|
||
@Override | ||
public Map<String, Integer> getFruits() { | ||
return fruits; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package core.basesyntax.strategy; | ||
|
||
import core.basesyntax.service.FruitTransaction; | ||
import core.basesyntax.service.ShopService; | ||
|
||
public class BalanceOperation implements OperationHandler { | ||
private ShopService shopService; | ||
|
||
public BalanceOperation(ShopService shopService) { | ||
this.shopService = shopService; | ||
} | ||
|
||
@Override | ||
public void handle(FruitTransaction transaction) { | ||
if (transaction.getOperation() == FruitTransaction.Operation.BALANCE) { | ||
String fruit = transaction.getFruit(); | ||
int quantity = transaction.getQuantity(); | ||
shopService.addFruits(fruit, quantity); | ||
} else { | ||
throw new RuntimeException("Unsupported operation for BalanceHandler"); | ||
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. Throwing a |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax.strategy; | ||
|
||
import core.basesyntax.service.FruitTransaction; | ||
|
||
public interface OperationHandler { | ||
void handle(FruitTransaction transaction); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax.strategy; | ||
|
||
import core.basesyntax.service.FruitTransaction; | ||
|
||
public interface OperationStrategy { | ||
OperationHandler get(FruitTransaction.Operation operation); | ||
} |
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.
Ensure that the file path is correct and that the file exists to avoid runtime errors. Consider adding error handling for file reading operations.