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

Hw frut shop test #800

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

62 changes: 62 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package core.basesyntax;

import core.basesyntax.db.Storage;
import core.basesyntax.impl.BalanceHandler;
import core.basesyntax.impl.CsvReportCreator;
import core.basesyntax.impl.CsvTransactionParser;
import core.basesyntax.impl.DefaultOperationStrategy;
import core.basesyntax.impl.FileReaderServiceImpl;
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.report.ReportCreator;
import core.basesyntax.report.WriterService;
import core.basesyntax.report.WriterServiceImpl;
import core.basesyntax.service.FileReaderService;
import core.basesyntax.service.PurchaseHandler;
import core.basesyntax.service.ReturnHandler;
import core.basesyntax.service.SupplyHandler;
import core.basesyntax.service.TransactionProcessor;
import core.basesyntax.strategy.OperationHandler;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
public static void main(String[] args) throws IOException {
String inputFilePath = "input.csv";
final String outputFilePath = "output.csv";

FileReaderService fileReaderService = new FileReaderServiceImpl();
List<String> transactionData;
try {
transactionData = fileReaderService.readData(inputFilePath);
} catch (IOException e) {
System.err.println("Error opening/reading file: " + e.getMessage());
return;
}

Map<FruitTransaction.Operation, OperationHandler> customHandlers = new HashMap<>();
customHandlers.put(FruitTransaction.Operation.BALANCE, new BalanceHandler());
customHandlers.put(FruitTransaction.Operation.SUPPLY, new SupplyHandler());
customHandlers.put(FruitTransaction.Operation.PURCHASE, new PurchaseHandler());
customHandlers.put(FruitTransaction.Operation.RETURN, new ReturnHandler());

DefaultOperationStrategy operationStrategy = new DefaultOperationStrategy(customHandlers);

List<FruitTransaction> transactions;
transactions = new CsvTransactionParser().parseTransactions(transactionData);

Storage storage = new Storage();

TransactionProcessor transactionProcessor;
transactionProcessor = new TransactionProcessor(operationStrategy, storage);
transactionProcessor.processTransactions(transactions);

WriterService writerService = new WriterServiceImpl();

ReportCreator reportCreator = new CsvReportCreator(writerService, outputFilePath);

Map<String, Integer> fruitInventory = storage.getFruitInventory();
reportCreator.createReport(fruitInventory, outputFilePath);
}
}
12 changes: 12 additions & 0 deletions src/main/java/core/basesyntax/db/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package core.basesyntax.db;

import java.util.HashMap;
import java.util.Map;

public class Storage {
private Map<String, Integer> fruitInventory = new HashMap<>();

public Map<String, Integer> getFruitInventory() {
return fruitInventory;
}
}
15 changes: 15 additions & 0 deletions src/main/java/core/basesyntax/impl/BalanceHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package core.basesyntax.impl;

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.strategy.OperationHandler;
import java.util.Map;

public class BalanceHandler implements OperationHandler {
@Override
public void handleTransaction(FruitTransaction transaction, Map<String, Integer> storage) {
String fruit = transaction.getFruit();
int quantity = transaction.getQuantity();

storage.put(fruit, quantity);
}
}
37 changes: 37 additions & 0 deletions src/main/java/core/basesyntax/impl/CsvReportCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package core.basesyntax.impl;

import core.basesyntax.report.ReportCreator;
import core.basesyntax.report.ReportService;
import core.basesyntax.report.WriterService;
import java.io.IOException;
import java.util.Map;

public class CsvReportCreator implements ReportService, ReportCreator {
private final WriterService writerService;
private final String outputPath;

public CsvReportCreator(WriterService writerService, String outputPath) {
this.writerService = writerService;
this.outputPath = outputPath;
}

@Override
public String generateReport(Map<String, Integer> fruitInventory) {
StringBuilder report = new StringBuilder("Fruit,Quantity\n");
for (Map.Entry<String, Integer> entry : fruitInventory.entrySet()) {
report.append(entry.getKey()).append(",").append(entry.getValue()).append("\n");
}
return report.toString();
}

@Override
public void createReport(Map<String, Integer> fruitInventory) throws IOException {
String report = generateReport(fruitInventory);
writerService.writeToFile(report, outputPath);
}

@Override
public void createReport(Map<String, Integer> fruitInventory,
String filePath) throws IOException {
}
}
31 changes: 31 additions & 0 deletions src/main/java/core/basesyntax/impl/CsvTransactionParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package core.basesyntax.impl;

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.service.TransactionParser;
import java.util.ArrayList;
import java.util.List;

public class CsvTransactionParser implements TransactionParser {
private static final String SEPARATOR = ",";
private static final int OPERATION_CODE_INDEX = 0;
private static final int FRUIT_INDEX = 1;

@Override
public List<FruitTransaction> parseTransactions(List<String> data) {
List<FruitTransaction> transactions = new ArrayList<>();
for (String line : data) {
String[] parts = line.split(SEPARATOR);
if (parts.length == 3) {
String operationCode = parts[OPERATION_CODE_INDEX];
String fruit = parts[FRUIT_INDEX];
int quantity = Integer.parseInt(parts[2]);
transactions.add(new FruitTransaction(
FruitTransaction.Operation.valueOf(operationCode),
fruit,
quantity
));
}
}
return transactions;
}
}
19 changes: 19 additions & 0 deletions src/main/java/core/basesyntax/impl/DefaultOperationStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package core.basesyntax.impl;

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.strategy.OperationHandler;
import core.basesyntax.strategy.OperationStrategy;
import java.util.Map;

public class DefaultOperationStrategy implements OperationStrategy {
private Map<FruitTransaction.Operation, OperationHandler> handlers;

public DefaultOperationStrategy(Map<FruitTransaction.Operation, OperationHandler> handlers) {
this.handlers = handlers;
}

@Override
public OperationHandler getHandler(FruitTransaction.Operation operation) {
return handlers.get(operation);
}
}
24 changes: 24 additions & 0 deletions src/main/java/core/basesyntax/impl/FileReaderServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package core.basesyntax.impl;

import core.basesyntax.service.FileReaderService;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileReaderServiceImpl implements FileReaderService {
@Override
public List<String> readData(String filePath) {
List<String> data = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
data.add(line);
}
} catch (IOException e) {
throw new RuntimeException("Error reading data from file: " + e.getMessage(), e);
}
return data;
}
}
15 changes: 15 additions & 0 deletions src/main/java/core/basesyntax/impl/TextFileWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package core.basesyntax.impl;

import core.basesyntax.service.TextFileWriterInterface;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class TextFileWriter implements TextFileWriterInterface {
@Override
public void writeText(String text, String filePath) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
writer.write(text);
}
}
}
63 changes: 63 additions & 0 deletions src/main/java/core/basesyntax/model/FruitTransaction.java
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 String code;

Operation(String code) {
this.code = code;
}

public String getCode() {
return code;
}
}

public static Operation getByCode(String character) {
for (Operation operation : Operation.values()) {
if (operation.getCode().equals(character)) {
return operation;
}
}
throw new RuntimeException("Unknown operation");
}
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/report/ReportCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core.basesyntax.report;

import java.io.IOException;
import java.util.Map;

public interface ReportCreator {
void createReport(Map<String, Integer> fruitInventory, String filePath) throws IOException;
}
10 changes: 10 additions & 0 deletions src/main/java/core/basesyntax/report/ReportService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package core.basesyntax.report;

import java.io.IOException;
import java.util.Map;

public interface ReportService {
String generateReport(Map<String, Integer> fruitInventory);

void createReport(Map<String, Integer> fruitInventory) throws IOException;
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/report/WriterService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.report;

import java.io.IOException;

public interface WriterService {
void writeToFile(String data, String outputPath) throws IOException;
}
13 changes: 13 additions & 0 deletions src/main/java/core/basesyntax/report/WriterServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package core.basesyntax.report;

import java.io.FileWriter;
import java.io.IOException;

public class WriterServiceImpl implements WriterService {
@Override
public void writeToFile(String data, String outputPath) throws IOException {
try (FileWriter writer = new FileWriter(outputPath)) {
writer.write(data);
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/service/FileReaderService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core.basesyntax.service;

import java.io.IOException;
import java.util.List;

public interface FileReaderService {
List<String> readData(String filePath) throws IOException;
}
16 changes: 16 additions & 0 deletions src/main/java/core/basesyntax/service/PurchaseHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package core.basesyntax.service;

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.strategy.OperationHandler;
import java.util.Map;

public class PurchaseHandler implements OperationHandler {
@Override
public void handleTransaction(FruitTransaction transaction, Map<String, Integer> storage) {
String fruit = transaction.getFruit();
int quantity = transaction.getQuantity();

int currentQuantity = storage.getOrDefault(fruit, 0);
storage.put(fruit, currentQuantity - quantity);
}
}
16 changes: 16 additions & 0 deletions src/main/java/core/basesyntax/service/ReturnHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package core.basesyntax.service;

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.strategy.OperationHandler;
import java.util.Map;

public class ReturnHandler implements OperationHandler {
@Override
public void handleTransaction(FruitTransaction transaction, Map<String, Integer> storage) {
String fruit = transaction.getFruit();
int quantity = transaction.getQuantity();

int currentQuantity = storage.getOrDefault(fruit, 0);
storage.put(fruit, currentQuantity + quantity);
}
}
Loading