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

SOLID #1256

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

SOLID #1256

Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/BalanceOperation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax;

public class BalanceOperation implements OperationHandler {
public void handle(FruitTransaction transaction) {
Storage.add(transaction.getFruit(), transaction.getQuantity());
}
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/DataConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax;

import java.util.List;

public interface DataConverter {
List<FruitTransaction> convertToTransaction(List<String> rawData);
}
19 changes: 19 additions & 0 deletions src/main/java/core/basesyntax/DataConverterImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package core.basesyntax;

import java.util.ArrayList;
import java.util.List;

public class DataConverterImpl implements DataConverter {
public List<FruitTransaction> convertToTransaction(List<String> rawData) {
List<FruitTransaction> fruitTransactions = new ArrayList<>();
for (int i = 1; i < rawData.size(); i++) {
String[] parts = rawData.get(i).split(",");
FruitTransaction.Operation operation = FruitTransaction.Operation.fromCode(parts[0]);
String fruit = parts[1];
int quantity = Integer.parseInt(parts[2]);
fruitTransactions.add(new FruitTransaction(operation, fruit, quantity));
}

return fruitTransactions;
}
}
24 changes: 24 additions & 0 deletions src/main/java/core/basesyntax/FileReaderImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package core.basesyntax;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileReaderImpl implements MyFileReader {
public List<String> read(String filePath) {
List<String> lines = new ArrayList<>();

try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
lines.add(line);
}
} catch (IOException e) {
throw new RuntimeException("Failed to read file: " + filePath, e);
}

return lines;
}
}
16 changes: 16 additions & 0 deletions src/main/java/core/basesyntax/FileWriterImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package core.basesyntax;

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

public class FileWriterImpl implements MyFileWriter {
@Override
public void write(String report, String path) {
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(path))) {
bufferedWriter.write(report);
} catch (IOException e) {
throw new RuntimeException("Failed to write file: " + path, e);
}
}
}
49 changes: 49 additions & 0 deletions src/main/java/core/basesyntax/FruitTransaction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package core.basesyntax;

public class FruitTransaction {
private final Operation operation;
private final String fruit;
private final 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 String getFruit() {
return fruit;
}

public int getQuantity() {
return quantity;
}

public enum Operation {
BALANCE("b"),
SUPPLY("s"),
PURCHASE("p"),
RETURN("r");

private final String code;

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

public static Operation fromCode(String code) {
for (Operation o : values()) {
if (o.code.equals(code)) {
return o;
}
}

throw new IllegalArgumentException("Unknown operation code: " + code);
}

}
}
9 changes: 0 additions & 9 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

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

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

/**
* Feel free to remove this class and create your own.
*/
public class Main {
public static void main(String[] args) {
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);

ShopService shopService = new ShopServiceImpl(operationStrategy);
DataConverter dataConverter = new DataConverterImpl();
MyFileReader fileReader = new FileReaderImpl();
//My IntelliJ version doesn't work with csv files, so I created a csv file locally on my PC
String path = "C:/Users/hois/Desktop/toRead.csv";
List<String> inputReport = fileReader.read(path);
shopService.process(dataConverter.convertToTransaction(inputReport));

ReportGenerator reportGenerator = new ReportGeneratorImpl();
String resultingReport = reportGenerator.getReport();

MyFileWriter fileWriter = new FileWriterImpl();
fileWriter.write(resultingReport, "finalReport.csv");
}
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/MyFileReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax;

import java.util.List;

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

public interface MyFileWriter {
void write(String report, String path);
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/OperationHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax;

public interface OperationHandler {
void handle(FruitTransaction transaction);
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/OperationStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax;

public interface OperationStrategy {
OperationHandler getOperationHandler(FruitTransaction.Operation operation);
}
16 changes: 16 additions & 0 deletions src/main/java/core/basesyntax/OperationStrategyImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package core.basesyntax;

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;
}

public OperationHandler getOperationHandler(FruitTransaction.Operation operation) {
return operationHandlers.get(operation);
}
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/PurchaseOperation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax;

public class PurchaseOperation implements OperationHandler {
public void handle(FruitTransaction transaction) {
Storage.subtract(transaction.getFruit(), transaction.getQuantity());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the resulting balance in the storage is not negative after a purchase. If it is, throw a RuntimeException as per the checklist requirement: 'Handling Purchase operation. Check result balance before saving it in the Storage - it should be positive. Throw RuntimeException in case the balance is negative.'

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

public interface ReportGenerator {
String getReport();
}
16 changes: 16 additions & 0 deletions src/main/java/core/basesyntax/ReportGeneratorImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package core.basesyntax;

public class ReportGeneratorImpl implements ReportGenerator {
public static final String REPORT_TITLE = "fruit,quantity\n";

@Override
public String getReport() {
StringBuilder reportBuilder = new StringBuilder(REPORT_TITLE);
Storage.getAll().forEach((fruit, quantity) -> reportBuilder.append(fruit)
.append(",")
.append(quantity)
.append("\n"));

return reportBuilder.toString();
}
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/ReturnOperation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax;

public class ReturnOperation implements OperationHandler {
public void handle(FruitTransaction transaction) {
Storage.add(transaction.getFruit(), transaction.getQuantity());
}
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/ShopService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core.basesyntax;

import java.util.List;

public interface ShopService {
void process(List<FruitTransaction> fruitTransactions);

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

import java.util.List;

public class ShopServiceImpl implements ShopService {
private final OperationStrategy operationStrategy;

public ShopServiceImpl(OperationStrategy operationStrategy) {
this.operationStrategy = operationStrategy;
}

@Override
public void process(List<FruitTransaction> fruitTransactions) {
for (FruitTransaction transaction : fruitTransactions) {
OperationHandler handler = operationStrategy
.getOperationHandler(transaction.getOperation());
handler.handle(transaction);
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/core/basesyntax/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package core.basesyntax;

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

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

public static void add(String fruit, int quantity) {
fruits.put(fruit, fruits.getOrDefault(fruit, 0) + quantity);
}

public static void subtract(String fruit, int quantity) {
fruits.put(fruit, fruits.getOrDefault(fruit, 0) - quantity);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the resulting quantity after subtraction is not negative. If it is, throw a RuntimeException to prevent invalid storage states. This aligns with the checklist requirement: 'Handling Purchase operation. Check result balance before saving it in the Storage - it should be positive. Throw RuntimeException in case the balance is negative.'

}

public static Map<String, Integer> getAll() {
return new HashMap<>(fruits);
}
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/SupplyOperation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax;

public class SupplyOperation implements OperationHandler {
public void handle(FruitTransaction transaction) {
Storage.add(transaction.getFruit(), transaction.getQuantity());
}
}
Loading