Skip to content

Commit

Permalink
logger
Browse files Browse the repository at this point in the history
  • Loading branch information
katrienkraska committed Dec 13, 2024
1 parent 2d980d8 commit e506e65
Show file tree
Hide file tree
Showing 20 changed files with 347 additions and 9 deletions.
9 changes: 0 additions & 9 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

9 changes: 9 additions & 0 deletions src/main/java/core/basesyntax/converter/DataConverter.java
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);
}
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);
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 value : values()) {
if (value.getCode().equals(code)) {
return value;
}
}
throw new IllegalArgumentException(
code + " operation doesn't exist.");
}
}
}
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);
}
18 changes: 18 additions & 0 deletions src/main/java/core/basesyntax/service/OperationStrategyImpl.java
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);
}
}
11 changes: 11 additions & 0 deletions src/main/java/core/basesyntax/service/PurchaseOperation.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 PurchaseOperation implements OperationHandler {
@Override
public void handle(Map<String, Integer> inventory, FruitTransaction transaction) {
inventory.merge(transaction.getFruit(), transaction.getQuantity(), Integer::sum);
}
}
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();
}
32 changes: 32 additions & 0 deletions src/main/java/core/basesyntax/service/ShopServiceImpl.java
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());
}
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);
}
}
48 changes: 48 additions & 0 deletions src/main/java/core/basesyntax/strategy/Main.java
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());

// 6. Записати отриманий звіт у файл призначення
FileWriter fileWriter = new FileWriterImpl();
fileWriter.write(resultingReport, "finalReport.csv");
}
}

0 comments on commit e506e65

Please sign in to comment.