-
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
base: master
Are you sure you want to change the base?
logger #1258
Changes from 1 commit
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,9 @@ | ||
package core.basesyntax.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,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; | ||
} | ||
} |
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); | ||
} |
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; | ||
} | ||
} |
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); | ||
} |
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); | ||
} | ||
} | ||
} |
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 value : values()) { | ||
if (value.getCode().equals(code)) { | ||
return value; | ||
} | ||
} | ||
throw new IllegalArgumentException( | ||
code + " operation doesn't exist."); | ||
} | ||
} | ||
} |
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); | ||
} |
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(); | ||
} | ||
} |
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()); | ||
} | ||
} |
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); | ||
} |
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); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
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) { | ||
this.operationHandlers = operationHandlers; | ||
} | ||
|
||
@Override | ||
public OperationHandler getHandler(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. Ensure that the |
||
} | ||
} |
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 PurchaseOperation implements OperationHandler { | ||
@Override | ||
public void handle(Map<String, Integer> inventory, FruitTransaction transaction) { | ||
inventory.merge(transaction.getFruit(), transaction.getQuantity(), Integer::sum); | ||
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 use of |
||
} | ||
} |
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); | ||
} | ||
} |
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(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
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()); | ||
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 |
||
} | ||
handler.handle(inventory, transaction); | ||
} | ||
} | ||
|
||
public Map<String, Integer> getInventory() { | ||
return inventory; | ||
} | ||
} |
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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package core.basesyntax.strategy; | ||
|
||
import core.basesyntax.converter.DataConverter; | ||
import core.basesyntax.converter.DataConverterImpl; | ||
import core.basesyntax.fileReader.FileReaderImpl; | ||
import core.basesyntax.fileWriter.FileWriter; | ||
import core.basesyntax.fileWriter.FileWriterImpl; | ||
import core.basesyntax.model.FruitTransaction; | ||
import core.basesyntax.report.ReportGenerator; | ||
import core.basesyntax.report.ReportGeneratorImpl; | ||
import core.basesyntax.service.*; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class Main { | ||
public static void main(String[] arg) { | ||
// 1. Зчитування даних із вхідного файлу CSV | ||
FileReaderImpl fileReader = new FileReaderImpl(); | ||
List<String> inputReport = fileReader.read("reportToRead.csv"); | ||
|
||
// 2. Перетворення вхідних даних у список FruitTransactions | ||
DataConverter dataConverter = new DataConverterImpl(); | ||
List<FruitTransaction> transactions = dataConverter.convertToTransaction(inputReport); | ||
|
||
// 3. Створюйте та відчувайте карту з усіма реалізаціями | ||
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); | ||
|
||
// 4. Обробляйте вхідні транзакції за допомогою відповідних реалізацій OperationHandler | ||
ShopService shopService = new ShopServiceImpl(operationStrategy); | ||
shopService.process(transactions); | ||
|
||
// 5. Створення звіту на основі поточного стану зберігання | ||
ReportGenerator reportGenerator = new ReportGeneratorImpl(); | ||
String resultingReport = reportGenerator.getReport((( | ||
ShopServiceImpl) shopService).getInventory()); | ||
Comment on lines
+48
to
+49
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. Casting
Comment on lines
+48
to
+49
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. Avoid casting |
||
|
||
// 6. Записати отриманий звіт у файл призначення | ||
FileWriter fileWriter = new FileWriterImpl(); | ||
fileWriter.write(resultingReport, "finalReport.csv"); | ||
} | ||
} |
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.