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

jv-1-fruit-shop #1285

Open
wants to merge 10 commits into
base: master
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
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Fruit shop
Let's imagine that we have a fruit store. Every day in the store there are a number of activities,
Let's imagine that we have a fruit store. Every day in the store there are a number of activities,
information about which is recorded in a file during the day.
The current input file is sent to the program in CSV format (it is recommended to use standard libraries for parsing).

Your tasks are:
- read data from the CSV file
- process these data
- process these data
- generate a report based on processed data
- write a report to a new CSV file

Expand All @@ -19,25 +19,25 @@ There are four activities at the store:

Let's check details of all types of activities:
1. Balance. Fruit balance at the beginning of the work shift. The following line in the file will look like:

```text
b,banana,100
```
The line above means there are 100 bananas at the beginning of the work shift.
The line above means there are 100 bananas at the beginning of the work shift.
1. Supply. You are accepting new fruits from suppliers. The following line in the file will look like:

```text
s,banana,100
```
The line above means you receive 100 bananas.
1. Purchase. Buyers can visit your shop and buy some fruits. In this case, you will have the following line in the file:

```text
p,banana,13
```
The line above means someone has bought 13 bananas.
1. Return. Buyers can return you some fruits. In this case, you will have the following line in the file:

```text
r,banana,10
```
Expand All @@ -57,15 +57,15 @@ Let's check details of all types of activities:
```

### Expecting report file example
We are expecting to see how many fruits are available today after the work shift in your Fruit store.
We are expecting to see how many fruits are available today after the work shift in your Fruit store.
```text
fruit,quantity
banana,152
apple,90
```
The line above means you have 152 bananas, and 90 apples in your Fruit store after the work shift.

**Hint: Think about creating some FruitTransaction model to store info from file line for more convenient data processing
**Hint: Think about creating some FruitTransaction model to store info from file line for more convenient data processing
(this is only a recommendation, you can use other classes/approaches to solve this task at your discretion):**
```java
public class FruitTransaction {
Expand Down Expand Up @@ -134,8 +134,8 @@ public class Main {

<details>
<summary>Additional tips (IMPORTANT: before viewing create a solution architecture and check it against these tips)</summary>
![FruitShop Schema](https://mate-academy-images.s3.eu-central-1.amazonaws.com/Fruit_Shop_1_c3855912d4.png)

![FruitShop Schema](https://mate-academy-images.s3.eu-central-1.amazonaws.com/Fruit_Shop_1_c3855912d4.png)

You are presented with a diagram describing an algorithm for the creation of a project structure. Your task is to implement it.

Expand Down Expand Up @@ -192,4 +192,4 @@ For example:
3. Null parameters
4. Providing the right names for your classes, methods, and variables is important. You can find examples here: [Link](https://mate-academy.github.io/style-guides/java/java.html#s5-naming)

</details>
</details>
2 changes: 0 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
</maven.checkstyle.plugin.configLocation>
</properties>

<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
Expand Down
9 changes: 0 additions & 9 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.converter.DataConverter;
import core.basesyntax.converter.DataConverterImpl;
import core.basesyntax.io.FileReader;
import core.basesyntax.io.FileReaderImpl;
import core.basesyntax.io.FileWriter;
import core.basesyntax.io.FileWriterImpl;
import core.basesyntax.model.FruitTransaction;
import core.basesyntax.operation.BalanceOperation;
import core.basesyntax.operation.OperationHandler;
import core.basesyntax.operation.OperationStrategy;
import core.basesyntax.operation.OperationStrategyImpl;
import core.basesyntax.operation.PurchaseOperation;
import core.basesyntax.operation.ReturnOperation;
import core.basesyntax.operation.SupplyOperation;
import core.basesyntax.report.ReportGenerator;
import core.basesyntax.report.ReportGeneratorImpl;
import core.basesyntax.service.ShopService;
import core.basesyntax.service.ShopServiceImpl;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
private static final String READ_PATH = "src/main/report/reportToRead.csv";
private static final String WRITE_PATH = "src/main/report/finalReport.csv";

public static void main(String[] args) throws IOException {
FileReader fileReader = new FileReaderImpl();
DataConverter dataConverter = new DataConverterImpl();

Map<FruitTransaction.Operation, OperationHandler> operationHandlers = new HashMap<>();
operationHandlers.put(FruitTransaction.Operation.BALANCE, new BalanceOperation());
operationHandlers.put(FruitTransaction.Operation.SUPPLY, new SupplyOperation());
operationHandlers.put(FruitTransaction.Operation.PURCHASE, new PurchaseOperation());
operationHandlers.put(FruitTransaction.Operation.RETURN, new ReturnOperation());

OperationStrategy operationStrategy = new OperationStrategyImpl(operationHandlers);
ShopService shopService = new ShopServiceImpl(operationStrategy);
ReportGenerator reportGenerator = new ReportGeneratorImpl();
FileWriter fileWriter = new FileWriterImpl();

List<String> inputLines = fileReader.read(READ_PATH);
List<FruitTransaction> transactions = dataConverter.convertToTransactions(inputLines);
shopService.process(transactions);
String report = reportGenerator.generateReport();
fileWriter.write(report, WRITE_PATH);
}
}
8 changes: 8 additions & 0 deletions src/main/java/core/basesyntax/converter/DataConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core.basesyntax.converter;

import core.basesyntax.model.FruitTransaction;
import java.util.List;

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

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

public class DataConverterImpl implements DataConverter {
private static final String COMMA = ",";
private static final int ZERO = 0;
private static final int HEADER_INDEX = 1;
private static final int SECOND = 2;
private static final int EXPECTED_PARTS_COUNT = 3;

@Override
public List<FruitTransaction> convertToTransactions(List<String> lines) {
if (lines == null || lines.isEmpty()) {
throw new IllegalArgumentException("Input lines cannot be null or empty");
}

List<FruitTransaction> transactions = new ArrayList<>();
for (int i = HEADER_INDEX; i < lines.size(); i++) {
transactions.add(parseTransactionLine(lines.get(i), i + HEADER_INDEX));
}
return transactions;
}

private FruitTransaction parseTransactionLine(String line, int lineNumber) {
String[] parts = line.split(COMMA);
if (parts.length != EXPECTED_PARTS_COUNT) {
throw new IllegalArgumentException("Invalid line format at line " + lineNumber);
}

String operationCode = parts[ZERO];
String fruit = parts[HEADER_INDEX];
int quantity = parseQuantity(parts[SECOND], lineNumber);

return new FruitTransaction(
FruitTransaction.Operation.fromCode(operationCode),
fruit,
quantity
);
}

private int parseQuantity(String quantityStr, int lineNumber) {
try {
int quantity = Integer.parseInt(quantityStr);
if (quantity < ZERO) {
throw new IllegalArgumentException("Quantity cannot be negative at line "
+ lineNumber);
}
return quantity;
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid quantity format at line " + lineNumber);
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/core/basesyntax/io/FileReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.io;

import java.util.List;

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

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) {
if (filePath == null || filePath.isEmpty()) {
throw new IllegalArgumentException("File path cannot be null or empty");
}

List<String> lines = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new java.io.FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
return lines;
} catch (IOException e) {
throw new RuntimeException("An error occurred while reading the file: "
+ filePath, e);
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/core/basesyntax/io/FileWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core.basesyntax.io;

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

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

public class FileWriterImpl implements FileWriter {
@Override
public void write(String content, String filePath) {
if (filePath == null || content == null) {
throw new IllegalArgumentException("File path and content cannot be null");
}

try (BufferedWriter writer = new BufferedWriter(new java.io.FileWriter(filePath))) {
writer.write(content);
} catch (IOException e) {
throw new RuntimeException("An error occurred while writing to the file: "
+ filePath, e);
}

Choose a reason for hiding this comment

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

add catch block

}
}
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 final String code;

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

public String getCode() {
return code;
}

public static Operation fromCode(String code) {
for (Operation operation : values()) {
if (operation.getCode().equals(code)) {
return operation;
}
}
throw new IllegalArgumentException("Invalid operation code " + code);
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/core/basesyntax/operation/BalanceOperation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package core.basesyntax.operation;

import core.basesyntax.model.FruitTransaction;

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

import core.basesyntax.model.FruitTransaction;

public interface OperationHandler {
void handle(FruitTransaction transaction);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax.operation;

import core.basesyntax.model.FruitTransaction;

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

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

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

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

@Override
public OperationHandler getHandler(FruitTransaction.Operation operation) {
OperationHandler handler = handlers.get(operation);
if (handler == null) {
throw new IllegalArgumentException("No handler found for "
+ operation);
}
return handler;
}
}
Loading