generated from mate-academy/jv-homework-template
-
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
jv-1-fruit-shop #1285
Open
aunkerrr
wants to merge
10
commits into
mate-academy:master
Choose a base branch
from
aunkerrr:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
jv-1-fruit-shop #1285
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8c59772
jv-1-fruit-shop
aunkerrr 5bfaf47
jv-2-fruit-shop-minor-changes
aunkerrr 48aac1f
jv-2-fruit-shop-tests-added
aunkerrr 1e9bc33
jv-3-fruit-shop-minor-changes
aunkerrr 36b602c
jv-4-fruit-shop-corrections
aunkerrr 25aed10
jv-4-fruit-shop-corrections
aunkerrr 74b4b18
jv-4-fruit-shop-corrections
aunkerrr 0733363
jv-5-fruit-shop-corrections
aunkerrr 4acd321
jv-6-fruit-shop-corrections
aunkerrr 97eb82c
jv-7-fruit-shop-corrections
aunkerrr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package core.basesyntax; | ||
|
||
import core.basesyntax.converter.DataConverter; | ||
import core.basesyntax.converter.DataConverterImpl; | ||
import core.basesyntax.io.FileReader; | ||
import core.basesyntax.io.FileReaderImpl; | ||
import core.basesyntax.io.FileWriter; | ||
import core.basesyntax.io.FileWriterImpl; | ||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.operation.BalanceOperation; | ||
import core.basesyntax.operation.OperationHandler; | ||
import core.basesyntax.operation.OperationStrategy; | ||
import core.basesyntax.operation.OperationStrategyImpl; | ||
import core.basesyntax.operation.PurchaseOperation; | ||
import core.basesyntax.operation.ReturnOperation; | ||
import core.basesyntax.operation.SupplyOperation; | ||
import core.basesyntax.report.ReportGenerator; | ||
import core.basesyntax.report.ReportGeneratorImpl; | ||
import core.basesyntax.service.ShopService; | ||
import core.basesyntax.service.ShopServiceImpl; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class Main { | ||
private static final String READ_PATH = "src/main/report/reportToRead.csv"; | ||
private static final String WRITE_PATH = "src/main/report/finalReport.csv"; | ||
|
||
public static void main(String[] args) throws IOException { | ||
FileReader fileReader = new FileReaderImpl(); | ||
DataConverter dataConverter = new DataConverterImpl(); | ||
|
||
Map<FruitTransaction.Operation, OperationHandler> operationHandlers = new HashMap<>(); | ||
operationHandlers.put(FruitTransaction.Operation.BALANCE, new BalanceOperation()); | ||
operationHandlers.put(FruitTransaction.Operation.SUPPLY, new SupplyOperation()); | ||
operationHandlers.put(FruitTransaction.Operation.PURCHASE, new PurchaseOperation()); | ||
operationHandlers.put(FruitTransaction.Operation.RETURN, new ReturnOperation()); | ||
|
||
OperationStrategy operationStrategy = new OperationStrategyImpl(operationHandlers); | ||
ShopService shopService = new ShopServiceImpl(operationStrategy); | ||
ReportGenerator reportGenerator = new ReportGeneratorImpl(); | ||
FileWriter fileWriter = new FileWriterImpl(); | ||
|
||
List<String> inputLines = fileReader.read(READ_PATH); | ||
List<FruitTransaction> transactions = dataConverter.convertToTransactions(inputLines); | ||
shopService.process(transactions); | ||
String report = reportGenerator.generateReport(); | ||
fileWriter.write(report, WRITE_PATH); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package core.basesyntax.converter; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.List; | ||
|
||
public interface DataConverter { | ||
List<FruitTransaction> convertToTransactions(List<String> lines); | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/core/basesyntax/converter/DataConverterImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package core.basesyntax.converter; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class DataConverterImpl implements DataConverter { | ||
private static final String COMMA = ","; | ||
private static final int ZERO = 0; | ||
private static final int HEADER_INDEX = 1; | ||
private static final int SECOND = 2; | ||
private static final int EXPECTED_PARTS_COUNT = 3; | ||
|
||
@Override | ||
public List<FruitTransaction> convertToTransactions(List<String> lines) { | ||
if (lines == null || lines.isEmpty()) { | ||
throw new IllegalArgumentException("Input lines cannot be null or empty"); | ||
} | ||
|
||
List<FruitTransaction> transactions = new ArrayList<>(); | ||
for (int i = HEADER_INDEX; i < lines.size(); i++) { | ||
transactions.add(parseTransactionLine(lines.get(i), i + HEADER_INDEX)); | ||
} | ||
return transactions; | ||
} | ||
|
||
private FruitTransaction parseTransactionLine(String line, int lineNumber) { | ||
String[] parts = line.split(COMMA); | ||
if (parts.length != EXPECTED_PARTS_COUNT) { | ||
throw new IllegalArgumentException("Invalid line format at line " + lineNumber); | ||
} | ||
|
||
String operationCode = parts[ZERO]; | ||
String fruit = parts[HEADER_INDEX]; | ||
int quantity = parseQuantity(parts[SECOND], lineNumber); | ||
|
||
return new FruitTransaction( | ||
FruitTransaction.Operation.fromCode(operationCode), | ||
fruit, | ||
quantity | ||
); | ||
} | ||
|
||
private int parseQuantity(String quantityStr, int lineNumber) { | ||
try { | ||
int quantity = Integer.parseInt(quantityStr); | ||
if (quantity < ZERO) { | ||
throw new IllegalArgumentException("Quantity cannot be negative at line " | ||
+ lineNumber); | ||
} | ||
return quantity; | ||
} catch (NumberFormatException e) { | ||
throw new IllegalArgumentException("Invalid quantity format at line " + lineNumber); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax.io; | ||
|
||
import java.util.List; | ||
|
||
public interface FileReader { | ||
List<String> read(String filePath); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package core.basesyntax.io; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class FileReaderImpl implements FileReader { | ||
@Override | ||
public List<String> read(String filePath) { | ||
if (filePath == null || filePath.isEmpty()) { | ||
throw new IllegalArgumentException("File path cannot be null or empty"); | ||
} | ||
|
||
List<String> lines = new ArrayList<>(); | ||
try (BufferedReader reader = new BufferedReader(new java.io.FileReader(filePath))) { | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
lines.add(line); | ||
} | ||
return lines; | ||
} catch (IOException e) { | ||
throw new RuntimeException("An error occurred while reading the file: " | ||
+ filePath, e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package core.basesyntax.io; | ||
|
||
public interface FileWriter { | ||
void write(String content, String filePath); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package core.basesyntax.io; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
|
||
public class FileWriterImpl implements FileWriter { | ||
@Override | ||
public void write(String content, String filePath) { | ||
if (filePath == null || content == null) { | ||
throw new IllegalArgumentException("File path and content cannot be null"); | ||
} | ||
|
||
try (BufferedWriter writer = new BufferedWriter(new java.io.FileWriter(filePath))) { | ||
writer.write(content); | ||
} catch (IOException e) { | ||
throw new RuntimeException("An error occurred while writing to the file: " | ||
+ filePath, e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
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 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 final String code; | ||
|
||
Operation(String code) { | ||
this.code = code; | ||
} | ||
|
||
public String getCode() { | ||
return code; | ||
} | ||
|
||
public static Operation fromCode(String code) { | ||
for (Operation operation : values()) { | ||
if (operation.getCode().equals(code)) { | ||
return operation; | ||
} | ||
} | ||
throw new IllegalArgumentException("Invalid operation code " + code); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/core/basesyntax/operation/BalanceOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package core.basesyntax.operation; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
|
||
public class BalanceOperation implements OperationHandler { | ||
@Override | ||
public void handle(FruitTransaction transaction) { | ||
Storage.storage.put(transaction.getFruit(), transaction.getQuantity()); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/core/basesyntax/operation/OperationHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax.operation; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
|
||
public interface OperationHandler { | ||
void handle(FruitTransaction transaction); | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/core/basesyntax/operation/OperationStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package core.basesyntax.operation; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
|
||
public interface OperationStrategy { | ||
OperationHandler getHandler(FruitTransaction.Operation operation); | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/core/basesyntax/operation/OperationStrategyImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package core.basesyntax.operation; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class OperationStrategyImpl implements OperationStrategy { | ||
private final Map<FruitTransaction.Operation, OperationHandler> handlers; | ||
|
||
public OperationStrategyImpl(Map<FruitTransaction.Operation, OperationHandler> handlers) { | ||
this.handlers = new HashMap<>(handlers); | ||
} | ||
|
||
@Override | ||
public OperationHandler getHandler(FruitTransaction.Operation operation) { | ||
OperationHandler handler = handlers.get(operation); | ||
if (handler == null) { | ||
throw new IllegalArgumentException("No handler found for " | ||
+ operation); | ||
} | ||
return handler; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
add catch block