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
logger #1258
Open
katrienkraska
wants to merge
3
commits into
mate-academy:master
Choose a base branch
from
katrienkraska:hw-solution
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
logger #1258
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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,8 @@ | ||
package core.basesyntax.converter; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.List; | ||
|
||
public interface DataConverter { | ||
List<FruitTransaction> convertToTransaction(List<String> inputReport); | ||
} |
30 changes: 30 additions & 0 deletions
30
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,30 @@ | ||
package core.basesyntax.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<>(); | ||
for (String line : inputReport) { | ||
String[] parts = line.split(","); | ||
if (parts.length != 3) { | ||
throw new IllegalArgumentException("Invalid line format: " + line); | ||
} | ||
String operationCode = parts[0].trim(); | ||
String fruit = parts[1].trim(); | ||
int quantity; | ||
try { | ||
quantity = Integer.parseInt(parts[2].trim()); | ||
} catch (NumberFormatException e) { | ||
throw new IllegalArgumentException("Invalid quantity in line: " + line); | ||
} | ||
FruitTransaction.Operation operation = FruitTransaction.Operation | ||
.getOperation(operationCode); | ||
transactions.add(new FruitTransaction(operation, fruit, quantity)); | ||
} | ||
return transactions; | ||
} | ||
} |
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.filereader; | ||
|
||
import java.util.List; | ||
|
||
public interface FileReader { | ||
List<String> read(String filePath); | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/core/basesyntax/filereader/FileReaderImpl.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,24 @@ | ||
package core.basesyntax.filereader; | ||
|
||
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) { | ||
|
||
// Ініціалізуємо список для збереження рядків | ||
List<String> inputReport = new ArrayList<>(); | ||
try (BufferedReader reader = new BufferedReader(new java.io.FileReader(filePath))) { | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
inputReport.add(line); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException("Error reading file: " + filePath, e); | ||
} | ||
return inputReport; | ||
} | ||
} |
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.filewriter; | ||
|
||
public interface FileWriter { | ||
void write(String content, String filePath); | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/core/basesyntax/filewriter/FileWriterImpl.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,15 @@ | ||
package core.basesyntax.filewriter; | ||
|
||
import java.io.IOException; | ||
|
||
public class FileWriterImpl implements FileWriter { | ||
@Override | ||
public void write(String content, String filePath) { | ||
try (java.io.FileWriter writer = new java.io.FileWriter(filePath)) { | ||
writer.write(content); // Записуємо текст у файл | ||
} catch (IOException e) { | ||
throw new RuntimeException( | ||
"Error writing 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,64 @@ | ||
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 String code; | ||
|
||
Operation(String code) { | ||
this.code = code; | ||
} | ||
|
||
public String getCode() { | ||
return code; | ||
} | ||
|
||
public static Operation getOperation(String code) { | ||
for (Operation operation : values()) { | ||
if (operation.getCode().equals(code)) { | ||
return operation; | ||
} | ||
} | ||
throw new IllegalArgumentException( | ||
"Unknown operation code: " + code); | ||
} | ||
} | ||
} |
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.report; | ||
|
||
import java.util.Map; | ||
|
||
public interface ReportGenerator { | ||
String getReport(Map<String, Integer> inventory); | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/core/basesyntax/report/ReportGeneratorImpl.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,17 @@ | ||
package core.basesyntax.report; | ||
|
||
import java.util.Map; | ||
|
||
public class ReportGeneratorImpl implements ReportGenerator { | ||
@Override | ||
public String getReport(Map<String, Integer> inventory) { | ||
// Додаємо заголовок | ||
StringBuilder report = new StringBuilder("fruit,quantity\n"); | ||
for (Map.Entry<String, Integer> entry : inventory.entrySet()) { | ||
// Додаємо дані | ||
report.append(entry.getKey()).append(",") | ||
.append(entry.getValue()).append("\n"); | ||
} | ||
return report.toString(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/core/basesyntax/service/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,11 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.Map; | ||
|
||
public class BalanceOperation implements OperationHandler { | ||
@Override | ||
public void handle(Map<String, Integer> inventory, FruitTransaction transaction) { | ||
inventory.put(transaction.getFruit(), transaction.getQuantity()); | ||
} | ||
} |
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.service; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.Map; | ||
|
||
public interface OperationHandler { | ||
void handle(Map<String, Integer> inventory, FruitTransaction transaction); | ||
} |
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.service; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
|
||
public interface OperationStrategy { | ||
OperationHandler getHandler(FruitTransaction.Operation operation); | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/core/basesyntax/service/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,27 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.Map; | ||
|
||
public class OperationStrategyImpl implements OperationStrategy { | ||
private final Map<FruitTransaction.Operation, OperationHandler> operationHandlers; | ||
|
||
public OperationStrategyImpl(Map<FruitTransaction.Operation, OperationHandler> | ||
operationHandlers) { | ||
if (operationHandlers == null || operationHandlers.isEmpty()) { | ||
throw new IllegalArgumentException( | ||
"OperationHandlers map cannot be null or empty"); | ||
} | ||
this.operationHandlers = operationHandlers; | ||
} | ||
|
||
@Override | ||
public OperationHandler getHandler(FruitTransaction.Operation operation) { | ||
OperationHandler handler = operationHandlers.get(operation); | ||
if (handler == null) { | ||
throw new IllegalArgumentException( | ||
"No handler found for operation: " + operation); | ||
} | ||
return handler; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/core/basesyntax/service/PurchaseOperation.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,20 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.Map; | ||
|
||
public class PurchaseOperation implements OperationHandler { | ||
@Override | ||
public void handle(Map<String, Integer> inventory, FruitTransaction transaction) { | ||
inventory.merge(transaction.getFruit(), -transaction.getQuantity(), | ||
(currentQuantity, decrement) -> { | ||
int updatedQuantity = currentQuantity + decrement; | ||
if (updatedQuantity < 0) { | ||
throw new IllegalArgumentException( | ||
"Not enough inventory for purchase: " | ||
+ transaction.getFruit()); | ||
} | ||
return updatedQuantity; | ||
}); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/core/basesyntax/service/ReturnOperation.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,11 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.Map; | ||
|
||
public class ReturnOperation implements OperationHandler { | ||
@Override | ||
public void handle(Map<String, Integer> inventory, FruitTransaction transaction) { | ||
inventory.merge(transaction.getFruit(), transaction.getQuantity(), Integer::sum); | ||
} | ||
} |
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,12 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public interface ShopService { | ||
//обробляє список транзакцій | ||
void process(List<FruitTransaction> transactions); | ||
|
||
Map<String, Integer> getInventory(); | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/core/basesyntax/service/ShopServiceImpl.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,33 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class ShopServiceImpl implements ShopService { | ||
private final OperationStrategy operationStrategy; | ||
private final Map<String, Integer> inventory = new HashMap<>(); | ||
|
||
public ShopServiceImpl(OperationStrategy operationStrategy) { | ||
this.operationStrategy = operationStrategy; | ||
} | ||
|
||
@Override | ||
public void process(List<FruitTransaction> transactions) { | ||
for (FruitTransaction transaction : transactions) { | ||
OperationHandler handler = operationStrategy.getHandler( | ||
transaction.getOperation()); | ||
if (handler == null) { | ||
throw new IllegalArgumentException( | ||
"No handler found for operation: " | ||
+ transaction.getOperation()); | ||
} | ||
handler.handle(inventory, transaction); | ||
} | ||
} | ||
|
||
public Map<String, Integer> getInventory() { | ||
return inventory; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/core/basesyntax/service/SupplyOperation.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,11 @@ | ||
package core.basesyntax.service; | ||
|
||
import core.basesyntax.model.FruitTransaction; | ||
import java.util.Map; | ||
|
||
public class SupplyOperation implements OperationHandler { | ||
@Override | ||
public void handle(Map<String, Integer> inventory, FruitTransaction transaction) { | ||
inventory.merge(transaction.getFruit(), transaction.getQuantity(), Integer::sum); | ||
} | ||
} |
Oops, something went wrong.
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.
Ensure that the
getOperation
method inFruitTransaction.Operation
is correctly implemented to handle invalid operation codes. It should throw anIllegalArgumentException
if the operation code does not match any known operation.