Skip to content
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
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

logger #1258

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/converter/DataConverter.java
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 src/main/java/core/basesyntax/converter/DataConverterImpl.java
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);
Comment on lines +24 to +25

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 in FruitTransaction.Operation is correctly implemented to handle invalid operation codes. It should throw an IllegalArgumentException if the operation code does not match any known operation.

transactions.add(new FruitTransaction(operation, fruit, quantity));
}
return transactions;
}
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/filereader/FileReader.java
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 src/main/java/core/basesyntax/filereader/FileReaderImpl.java
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;
}
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/filewriter/FileWriter.java
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 src/main/java/core/basesyntax/filewriter/FileWriterImpl.java
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);
}
}
}
64 changes: 64 additions & 0 deletions src/main/java/core/basesyntax/model/FruitTransaction.java
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);
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/report/ReportGenerator.java
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 src/main/java/core/basesyntax/report/ReportGeneratorImpl.java
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 src/main/java/core/basesyntax/service/BalanceOperation.java
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());
}
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/service/OperationHandler.java
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);
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/service/OperationStrategy.java
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 src/main/java/core/basesyntax/service/OperationStrategyImpl.java
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 src/main/java/core/basesyntax/service/PurchaseOperation.java
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 src/main/java/core/basesyntax/service/ReturnOperation.java
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);
}
}
12 changes: 12 additions & 0 deletions src/main/java/core/basesyntax/service/ShopService.java
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 src/main/java/core/basesyntax/service/ShopServiceImpl.java
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 src/main/java/core/basesyntax/service/SupplyOperation.java
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);
}
}
Loading
Loading