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

done #1296

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

done #1296

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

Choose a reason for hiding this comment

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

Let's revise common mistakes file and check project structure


public class BalanceOperation implements OperationHandler {
@Override
public void apply(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> data);
}
31 changes: 31 additions & 0 deletions src/main/java/core/basesyntax/DataConverterImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package core.basesyntax;

import java.util.List;
import java.util.stream.Collectors;

class DataConverterImpl implements DataConverter {
@Override
public List<FruitTransaction> convertToTransaction(List<String> data) {
// Пропускаем первую строку с заголовками

Choose a reason for hiding this comment

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

remove comment

return data.stream()
.skip(1)
.map(line -> {
String[] parts = line.split(",");

Choose a reason for hiding this comment

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

create constant for comma

String opCode = parts[0];

Choose a reason for hiding this comment

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

create constants for all magic numbers

FruitTransaction.Operation op = switch (opCode) {
case "b" -> FruitTransaction.Operation.BALANCE;
case "s" -> FruitTransaction.Operation.SUPPLY;
case "p" -> FruitTransaction.Operation.PURCHASE;
case "r" -> FruitTransaction.Operation.RETURN;
default -> throw new IllegalArgumentException("Invalid operation code: "
+ opCode);
};
String fruit = parts[1];
int quantity = Integer.parseInt(parts[2]);
return new FruitTransaction(op, fruit, quantity);
})
.collect(Collectors.toList());

Choose a reason for hiding this comment

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

Suggested change
.collect(Collectors.toList());
.toList();

}
}

Choose a reason for hiding this comment

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

remove redundant empty lines


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

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

public interface FileReader {
List<String> read(String filepath) throws IOException;
}
12 changes: 12 additions & 0 deletions src/main/java/core/basesyntax/FileReaderImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package core.basesyntax;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class FileReaderImpl implements FileReader {
public List<String> read(String filePath) throws IOException {
return Files.readAllLines(Paths.get(filePath));

Choose a reason for hiding this comment

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

let's use try catch here

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

import java.io.IOException;

public interface FileWriter {
void write(String content, String filePath) throws IOException;
}
12 changes: 12 additions & 0 deletions src/main/java/core/basesyntax/FileWriterImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package core.basesyntax;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

class FileWriterImpl implements FileWriter {
public void write(String content, String filePath) throws IOException {
Files.write(Paths.get(filePath), content.getBytes());

Choose a reason for hiding this comment

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

same here

}
}

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

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

Choose a reason for hiding this comment

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

you can also create method to check if particular code exists
public static Operation getOperation(String code)

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

This file was deleted.

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

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

public class Main {
public static void main(String[] arg) throws IOException {
// 1. Read the data from the input CSV file
FileReader fileReader = new FileReaderImpl();
List<String> inputReport = fileReader.read("reportToRead.csv");

Choose a reason for hiding this comment

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

input and output files should be in resources folder
create constants for files pathes


// 2. Convert the incoming data into FruitTransactions list
DataConverter dataConverter = new DataConverterImpl();
final List<FruitTransaction> transactions = dataConverter.convertToTransaction(inputReport);

// 3. Create and feel the map with all OperationHandler implementations
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. Process the incoming transactions with applicable OperationHandler implementations
ShopService shopService = new ShopServiceImpl(operationStrategy);
shopService.process(transactions);

// 5.Generate report based on the current Storage state
ReportGenerator reportGenerator = new ReportGeneratorImpl();
String resultingReport = reportGenerator.getReport();

// 6. Write the received report into the destination file
FileWriter fileWriter = new FileWriterImpl();
fileWriter.write(resultingReport, "finalReport.csv");
}
}
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 apply(FruitTransaction transaction);
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/OperationStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax;

import java.util.List;

public interface OperationStrategy {
void process(List<FruitTransaction> transactions);
}
27 changes: 27 additions & 0 deletions src/main/java/core/basesyntax/OperationStrategyImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package core.basesyntax;

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

class OperationStrategyImpl implements OperationStrategy {
private final Map<FruitTransaction.Operation, OperationHandler> operationHandlers;

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

@Override
public void process(List<FruitTransaction> transactions) {
for (FruitTransaction transaction : transactions) {
OperationHandler handler = operationHandlers.get(transaction.getOperation());
if (handler != null) {
handler.apply(transaction);
} else {
throw new IllegalArgumentException("No handler for operation: "
+ transaction.getOperation());
}
}
}
}

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

public class PurchaseOperation implements OperationHandler {
@Override
public void apply(FruitTransaction transaction) {
try {
Storage.remove(transaction.getFruit(), transaction.getQuantity());
} catch (RuntimeException e) {
throw new RuntimeException("Balance is negative");

Choose a reason for hiding this comment

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

Consider providing more context in the exception message, such as including the fruit type and the attempted quantity, to make debugging easier.

}
}
}
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();
}
13 changes: 13 additions & 0 deletions src/main/java/core/basesyntax/ReportGeneratorImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package core.basesyntax;

public class ReportGeneratorImpl implements ReportGenerator {
@Override
public String getReport() {
StringBuilder report = new StringBuilder();
Storage.getFruits().forEach((fruit, quantity) -> report.append(fruit)
.append(",")

Choose a reason for hiding this comment

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

create constant for comma

.append(quantity)
.append("\n"));

Choose a reason for hiding this comment

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

Use System.lineSeparator() instead of "\n"

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

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

import java.util.List;

public interface ShopService {
public void process(List<FruitTransaction> transactions);
}
15 changes: 15 additions & 0 deletions src/main/java/core/basesyntax/ShopServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package core.basesyntax;

import java.util.List;

class ShopServiceImpl implements ShopService {

Choose a reason for hiding this comment

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

Suggested change
class ShopServiceImpl implements ShopService {
public class ShopServiceImpl implements ShopService {

private final OperationStrategy operationStrategy;

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

public void process(List<FruitTransaction> transactions) {
operationStrategy.process(transactions);
}
}
33 changes: 33 additions & 0 deletions src/main/java/core/basesyntax/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package core.basesyntax;

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

public class Storage {
public 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 remove(String fruit, int quantity) {
int currentQuantity = fruits.getOrDefault(fruit, 0);
if (currentQuantity < quantity) {
throw new IllegalArgumentException("Not enough "
+ fruit
+ " in stock to remove "
+ quantity);
}
int newQuantity = currentQuantity - quantity;
if (newQuantity == 0) {
fruits.remove(fruit);
} else {
fruits.put(fruit, newQuantity);
}
}

public static Map<String, Integer> getFruits() {

Choose a reason for hiding this comment

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

redundant method

return fruits;
}
}

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

public class SupplyOperation implements OperationHandler {
@Override
public void apply(FruitTransaction transaction) {
Storage.add(transaction.getFruit(), transaction.getQuantity());
}
}