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

Created structure that reads data from csv file, inside created proce… #946

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

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

import core.basesyntax.db.Storage;
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.service.FruitService;
import core.basesyntax.service.ParseService;
import core.basesyntax.service.ReaderService;
import core.basesyntax.service.ReportService;
import core.basesyntax.service.WriterService;
import core.basesyntax.service.impl.FruitServiceImpl;
import core.basesyntax.service.impl.ParseServiceImpl;
import core.basesyntax.service.impl.ReaderServiceImpl;
import core.basesyntax.service.impl.ReportServiceImpl;
import core.basesyntax.service.impl.WriterServiceImpl;
import core.basesyntax.strategy.BalanceOperationStrategy;
import core.basesyntax.strategy.OperationStrategy;
import core.basesyntax.strategy.PurchaseOperationStrategy;
import core.basesyntax.strategy.ReturnOperationStrategy;
import core.basesyntax.strategy.SupplyOperationStrategy;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
private static final String INPUT_FILE = "src/main/resources/input.csv";
private static final String OUTPUT_FILE = "src/main/resources/output.csv";

public static void main(String[] args) {
ReaderService readerService = new ReaderServiceImpl();
WriterService writerService = new WriterServiceImpl();
ParseService parseService = new ParseServiceImpl();
ReportService reportService = new ReportServiceImpl();
Storage storage = Storage.getInstance();
Map<String, OperationStrategy> operationStrategies = initializeStrategies();
FruitService fruitService = new FruitServiceImpl(operationStrategies);
List<String> inputLines = readerService.readFromFile(INPUT_FILE);
List<FruitTransaction> transactions = parseService.parseTransactions(inputLines);
fruitService.processTransactions(transactions);
List<String> report = reportService.generateReport(storage);
writerService.writeToFile(OUTPUT_FILE, report);
}

private static Map<String, OperationStrategy> initializeStrategies() {
Map<String, OperationStrategy> operationStrategies = new HashMap<>();
operationStrategies.put("b", new BalanceOperationStrategy());
operationStrategies.put("s", new SupplyOperationStrategy());
operationStrategies.put("p", new PurchaseOperationStrategy());
operationStrategies.put("r", new ReturnOperationStrategy());
return operationStrategies;
}
}
47 changes: 47 additions & 0 deletions src/main/java/core/basesyntax/db/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package core.basesyntax.db;

import core.basesyntax.model.FruitTransaction;
import java.util.HashMap;
import java.util.Map;

public class Storage {
private static Storage instance;
private final Map<String, Integer> fruitInventory;

private Storage() {
this.fruitInventory = new HashMap<>();
}

public static Storage getInstance() {
if (instance == null) {
instance = new Storage();
}
return instance;
}

public Map<String, Integer> getFruitInventory() {
return new HashMap<>(fruitInventory);
}

public void updateFruitQuantity(FruitTransaction.Operation operation,
String fruitName, int quantity) {
int currentQuantity = fruitInventory.getOrDefault(fruitName, 0);
switch (operation) {
case BALANCE:
case SUPPLY:
case RETURN:
currentQuantity += quantity;
break;
case PURCHASE:
currentQuantity -= quantity;
break;
default:
throw new IllegalArgumentException("Unknown operation type: " + operation);
}
if (currentQuantity < 0) {
throw new IllegalArgumentException("Negative fruit quantity not allowed: " + fruitName);
}

fruitInventory.put(fruitName, currentQuantity);
}
}
62 changes: 62 additions & 0 deletions src/main/java/core/basesyntax/model/FruitTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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 final String code;

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

public String getCode() {
return code;
}

public static Operation fromCode(String code) {
for (Operation operation : Operation.values()) {
if (operation.getCode().equals(code)) {
return operation;
}
}
throw new IllegalArgumentException("Unknown operation code: " + code);
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/service/FruitService.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.List;

public interface FruitService {
void processTransactions(List<FruitTransaction> transaction);
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/service/ParseService.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.List;

public interface ParseService {
List<FruitTransaction> parseTransactions(List<String> inputLines);
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/service/ReaderService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.service;

import java.util.List;

public interface ReaderService {
List<String> readFromFile(String filePath);
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/service/ReportService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core.basesyntax.service;

import core.basesyntax.db.Storage;
import java.util.List;

public interface ReportService {
List<String> generateReport(Storage storage);
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/service/WriterService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.service;

import java.util.List;

public interface WriterService {
void writeToFile(String filePath, List<String> lines);
}
24 changes: 24 additions & 0 deletions src/main/java/core/basesyntax/service/impl/FruitServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package core.basesyntax.service.impl;

import core.basesyntax.model.FruitTransaction;
import core.basesyntax.service.FruitService;
import core.basesyntax.strategy.OperationStrategy;
import java.util.List;
import java.util.Map;

public class FruitServiceImpl implements FruitService {
private final Map<String, OperationStrategy> operationStrategies;

public FruitServiceImpl(Map<String, OperationStrategy> operationStrategies) {
this.operationStrategies = operationStrategies;
}

@Override
public void processTransactions(List<FruitTransaction> transaction) {
for (FruitTransaction fruitTransaction : transaction) {
FruitTransaction.Operation operationType = fruitTransaction.getOperation();
OperationStrategy strategy = operationStrategies.get(operationType.getCode());
strategy.applyStrategy(fruitTransaction);
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/core/basesyntax/service/impl/ParseServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package core.basesyntax.service.impl;

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

public class ParseServiceImpl implements ParseService {
public List<FruitTransaction> parseTransactions(List<String> inputLines) {
List<FruitTransaction> fruitList = new ArrayList<>();
for (String line : inputLines) {
line = line.trim();
String[] transaction = line.split("\\s*,\\s*");
fruitList.add(new FruitTransaction(FruitTransaction.Operation.fromCode(transaction[0]),
transaction[1],
Integer.parseInt(transaction[2])));
}
return fruitList;
}
}
24 changes: 24 additions & 0 deletions src/main/java/core/basesyntax/service/impl/ReaderServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package core.basesyntax.service.impl;

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

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

import core.basesyntax.db.Storage;
import core.basesyntax.service.ReportService;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ReportServiceImpl implements ReportService {
public List<String> generateReport(Storage storage) {
List<String> report = new ArrayList<>();
report.add("fruit,quantity");
for (Map.Entry<String, Integer> entry : storage.getFruitInventory().entrySet()) {
String fruitName = entry.getKey();
int quantity = entry.getValue();
String quantityString = (quantity >= 0) ? String.valueOf(quantity) : "-" + (-quantity);
String reportLine = fruitName + "," + quantityString;
report.add(reportLine);
}
return report;
}
}
21 changes: 21 additions & 0 deletions src/main/java/core/basesyntax/service/impl/WriterServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package core.basesyntax.service.impl;

import core.basesyntax.service.WriterService;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class WriterServiceImpl implements WriterService {
@Override
public void writeToFile(String filePath, List<String> lines) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter((filePath)))) {
for (String line : lines) {
writer.write(line);
writer.newLine();
}
} catch (IOException e) {
throw new RuntimeException("Error writing to file: " + filePath, e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package core.basesyntax.strategy;

import core.basesyntax.db.Storage;
import core.basesyntax.model.FruitTransaction;

public class BalanceOperationStrategy implements OperationStrategy {
@Override
public void applyStrategy(FruitTransaction transaction) {
String fruitName = transaction.getFruit();
int quantity = transaction.getQuantity();
Storage storage = Storage.getInstance();
storage.updateFruitQuantity(FruitTransaction.Operation.BALANCE, fruitName, quantity);
}
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/strategy/OperationStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.strategy;

import core.basesyntax.model.FruitTransaction;

public interface OperationStrategy {
void applyStrategy(FruitTransaction transaction);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package core.basesyntax.strategy;

import core.basesyntax.db.Storage;
import core.basesyntax.model.FruitTransaction;

public class PurchaseOperationStrategy implements OperationStrategy {
@Override
public void applyStrategy(FruitTransaction transaction) {
String fruitName = transaction.getFruit();
int quantity = transaction.getQuantity();
Storage storage = Storage.getInstance();
storage.updateFruitQuantity(FruitTransaction
.Operation.PURCHASE, fruitName, quantity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package core.basesyntax.strategy;

import core.basesyntax.db.Storage;
import core.basesyntax.model.FruitTransaction;

public class ReturnOperationStrategy implements OperationStrategy {
@Override
public void applyStrategy(FruitTransaction transaction) {
String fruitName = transaction.getFruit();
int quantity = transaction.getQuantity();
Storage storage = Storage.getInstance();
storage.updateFruitQuantity(FruitTransaction.Operation.RETURN, fruitName, quantity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package core.basesyntax.strategy;

import core.basesyntax.db.Storage;
import core.basesyntax.model.FruitTransaction;

public class SupplyOperationStrategy implements OperationStrategy {
@Override
public void applyStrategy(FruitTransaction transaction) {
String fruitName = transaction.getFruit();
int quantity = transaction.getQuantity();
Storage storage = Storage.getInstance();
storage.updateFruitQuantity(FruitTransaction.Operation.SUPPLY, fruitName, quantity);
}
}
Loading