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

jv-1-fruit-shop #1285

wants to merge 10 commits into from

Conversation

aunkerrr
Copy link

No description provided.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Great job on implementing the solution! 🎉 Your code adheres to the SOLID principles and meets the task requirements effectively. While there are minor suggestions, such as moving file paths to a configuration file to avoid hardcoding, these do not impact the core functionality. Keep up the excellent work and consider these improvements for future projects! 😊

✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨

Comment on lines 38 to 39
List<String> inputLines = fileReader.read("C:/Users/WeakRitta/IdeaProjects"
+ "/jv-fruit-shop/src/main/java/result/reportToRead.csv");

Choose a reason for hiding this comment

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

Consider moving the file paths to a configuration file or using a resource folder to avoid hardcoding paths directly in the code. This aligns with the checklist recommendation to avoid hardcoding in your solution.

Comment on lines 43 to 44
fileWriter.write(report, "C:/Users/WeakRitta/IdeaProjects/jv-fruit-shop/src"
+ "/main/java/result/finalReport.csv");

Choose a reason for hiding this comment

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

Similarly, consider moving the output file path to a configuration file or using a resource folder to avoid hardcoding paths directly in the code.

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

Choose a reason for hiding this comment

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

revert changes

pom.xml Outdated
@@ -18,7 +18,14 @@
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>

Choose a reason for hiding this comment

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

you should implement tests in the next task

ReportGenerator reportGenerator = new ReportGeneratorImpl();
FileWriter fileWriter = new FileWriterImpl();

List<String> inputLines = fileReader.read("C:/Users/WeakRitta/IdeaProjects"

Choose a reason for hiding this comment

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

create constant for path, also files should be in resources folder
don't use absolute path

List<FruitTransaction> transactions = dataConverter.convertToTransactions(inputLines);
Map<String, Integer> finalStorage = shopService.process(transactions);
String report = reportGenerator.generateReport(finalStorage);
fileWriter.write(report, "C:/Users/WeakRitta/IdeaProjects/jv-fruit-shop/src"

Choose a reason for hiding this comment

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

same comment


public class Main {
public static void main(String[] args) {
try {

Choose a reason for hiding this comment

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

remove try catch

.append(entry.getKey())
.append(",")
.append(entry.getValue())
.append("\n"));

Choose a reason for hiding this comment

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

same here

.sorted(Map.Entry.comparingByKey())
.forEach(entry -> report
.append(entry.getKey())
.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

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class DataConverterImplTest {

Choose a reason for hiding this comment

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

remove all tests

@Override
public String generateReport(Map<String, Integer> storage) {
StringBuilder report = new StringBuilder();
report.append("fruit,quantity\n");

Choose a reason for hiding this comment

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

you can create constant HEADER = "fruit,quantity";

List<FruitTransaction> transactions = new ArrayList<>();
// Skip header
for (int i = 1; i < lines.size(); i++) {
String[] parts = lines.get(i).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

Copy link

@liliia-ponomarenko liliia-ponomarenko left a comment

Choose a reason for hiding this comment

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

Good job! Let’s improve your solution ;)

Comment on lines 27 to 29
private static final String READ_PATH = "reportToRead.csv";
private static final String WRITE_PATH = "finalReport.csv";

Choose a reason for hiding this comment

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

place files in the resources directory

Comment on lines 23 to 27
String operationCode = parts[0].trim();
String fruit = parts[1].trim();
int quantity;
try {
quantity = Integer.parseInt(parts[2].trim());

Choose a reason for hiding this comment

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

Suggested change
String operationCode = parts[0].trim();
String fruit = parts[1].trim();
int quantity;
try {
quantity = Integer.parseInt(parts[2].trim());
String operationCode = parts[0];
String fruit = parts[1].trim();
int quantity;
try {
quantity = Integer.parseInt(parts[2]);

public class FileReaderImpl implements FileReader {
@Override
public List<String> read(String filePath) throws IOException {
if (filePath == null || filePath.trim().isEmpty()) {

Choose a reason for hiding this comment

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

Suggested change
if (filePath == null || filePath.trim().isEmpty()) {
if (filePath == null || filePath.isEmpty()) {

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

Choose a reason for hiding this comment

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

throw exception

}
}
}

Choose a reason for hiding this comment

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

Suggested change

leave only one empty line

if (handler == null) {
throw new IllegalArgumentException("No handler found" + transaction.getOperation());
}
handler.handle(storage, transaction);

Choose a reason for hiding this comment

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

The strategy should return the relevant handler. for process data use a separate ShopService.

Suggested change
handler.handle(storage, transaction);
return handler;


public class ShopServiceImpl implements ShopService {
private final OperationStrategy operationStrategy;
private final Map<String, Integer> storage = new HashMap<>();

Choose a reason for hiding this comment

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

remove this variable. Use static map from the Storage class


storage.entrySet()
.stream()
.sorted(Map.Entry.comparingByKey())

Choose a reason for hiding this comment

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

It’s not required

.sorted(Map.Entry.comparingByKey())
.forEach(entry -> report
.append(entry.getKey())
.append(",")

Choose a reason for hiding this comment

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

move the separator to constant fields

.append(",")
.append(entry.getValue())
.append(System.lineSeparator()));

Choose a reason for hiding this comment

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

Suggested change

Copy link

@liliia-ponomarenko liliia-ponomarenko left a comment

Choose a reason for hiding this comment

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

Good job! Let’s fix a few mistakes ;)

Comment on lines 27 to 28
private static final String READ_PATH = "recources/reportToRead.csv";
private static final String WRITE_PATH = "recources/finalReport.csv";

Choose a reason for hiding this comment

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

use the system resources directory. the path should be src/main/resources/reportToRead.csv

}

String operationCode = parts[0];
String fruit = parts[1].trim();

Choose a reason for hiding this comment

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

Suggested change
String fruit = parts[1].trim();
String fruit = parts[1];


public class FileReaderImpl implements FileReader {
@Override
public List<String> read(String filePath) throws IOException {

Choose a reason for hiding this comment

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

Don’t throw exceptions in the method signature. add catch block

Choose a reason for hiding this comment

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

Not resolved

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

Choose a reason for hiding this comment

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

Suggested change
throw new IOException("An error occurred while writing to the file: " + e.getMessage());
throw new RuntimeException("An error occurred while writing to the file: " + filePath, e);

Comment on lines 19 to 24
public Map<String, Integer> process(List<FruitTransaction> transactions) {
for (FruitTransaction transaction : transactions) {
OperationHandler handler = operationStrategy.getHandler(transaction.getOperation());
handler.handle(transaction);
}
return new HashMap<>(Storage.storage);

Choose a reason for hiding this comment

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

Suggested change
public Map<String, Integer> process(List<FruitTransaction> transactions) {
for (FruitTransaction transaction : transactions) {
OperationHandler handler = operationStrategy.getHandler(transaction.getOperation());
handler.handle(transaction);
}
return new HashMap<>(Storage.storage);
public void process(List<FruitTransaction> transactions) {
for (FruitTransaction transaction : transactions) {
OperationHandler handler = operationStrategy.getHandler(transaction.getOperation());
handler.handle(transaction);
}

Comment on lines 9 to 14
@Override
public String generateReport(Map<String, Integer> storage) {
StringBuilder report = new StringBuilder();
report.append(HEADER).append(System.lineSeparator());

storage.entrySet()

Choose a reason for hiding this comment

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

Suggested change
@Override
public String generateReport(Map<String, Integer> storage) {
StringBuilder report = new StringBuilder();
report.append(HEADER).append(System.lineSeparator());
storage.entrySet()
@Override
public String generateReport() {
StringBuilder report = new StringBuilder();
report.append(HEADER).append(System.lineSeparator());
Storage.storage.entrySet()

Copy link

@ahoienko ahoienko left a comment

Choose a reason for hiding this comment

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

Nice, left some comments

Comment on lines 23 to 24
String operationCode = parts[0];
String fruit = parts[1];

Choose a reason for hiding this comment

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

Magic numbers

Comment on lines 26 to 34
try {
quantity = Integer.parseInt(parts[2]);
if (quantity < 0) {
throw new IllegalArgumentException("Quantity cannot be negative at line "
+ (i + 1));
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid quantity format at line " + (i + 1));
}

Choose a reason for hiding this comment

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

Move to separate method

Comment on lines 17 to 19
for (int i = 1; i < lines.size(); i++) {
String[] parts = lines.get(i).split(COMMA);
if (parts.length != 3) {

Choose a reason for hiding this comment

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

Magic numbers


public class FileReaderImpl implements FileReader {
@Override
public List<String> read(String filePath) throws IOException {

Choose a reason for hiding this comment

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

Not resolved

lines.add(line);
}
} catch (IOException e) {
throw new IOException("An error occurred while readint this file: " + e);

Choose a reason for hiding this comment

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

Suggested change
throw new IOException("An error occurred while readint this file: " + e);
throw new SomeRutimeException("An error occurred while readint this file: " + e);

lines.add(line);
}
} catch (IOException e) {
throw new IOException("An error occurred while readint this file: " + e);

Choose a reason for hiding this comment

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

Which file? What is 'this' file?

import java.io.IOException;

public interface FileWriter {
void write(String content, String filePath) throws IOException;

Choose a reason for hiding this comment

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

Suggested change
void write(String content, String filePath) throws IOException;
void write(String content, String filePath)

Choose a reason for hiding this comment

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

Not resolved

Comment on lines 10 to 14
public String generateReport() { // Remove the Map parameter
StringBuilder report = new StringBuilder();
report.append(HEADER).append(System.lineSeparator());

Storage.storage.entrySet() // Use Storage.storage directly

Choose a reason for hiding this comment

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

remove comments

@@ -0,0 +1,23 @@
package report;

import core.basesyntax.operation.Storage; // Import the Storage class

Choose a reason for hiding this comment

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

remove comments

README.md Outdated
Comment on lines 97 to 107
Also, here is an example of what the `main` method may look like:

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

// 2. Convert the incoming data into FruitTransactions list
DataConverter dataConverter = new DataConverterImpl();

Choose a reason for hiding this comment

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

Are you sure that you need this changes?

Copy link
Author

@aunkerrr aunkerrr Jan 29, 2025

Choose a reason for hiding this comment

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

Kinda sure cause i just suddenly changed task description and mentor noticed this before
So yeah im pretty sure

Choose a reason for hiding this comment

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

No, you actually dont, please dont change readme file, it is not related to task, and changes that you made are redundant

@aunkerrr aunkerrr requested a review from ahoienko January 29, 2025 16:53
Copy link

@ahoienko ahoienko left a comment

Choose a reason for hiding this comment

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

Please be careful and read comments carefully, and push new solution after you carefully checked all comments))

Comment on lines 31 to 33
String operationCode = parts[0];
String fruit = parts[1];
int quantity = parseQuantity(parts[2], lineNumber);

Choose a reason for hiding this comment

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

image

import java.util.List;

public interface FileReader {
List<String> read(String filePath) throws IOException;

Choose a reason for hiding this comment

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

image

Choose a reason for hiding this comment

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

Please be careful, if throws is removed from implementation it should be removed from interface as well

import java.io.IOException;

public interface FileWriter {
void write(String content, String filePath) throws IOException;

Choose a reason for hiding this comment

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

Not resolved

README.md Outdated
Comment on lines 97 to 107
Also, here is an example of what the `main` method may look like:

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

// 2. Convert the incoming data into FruitTransactions list
DataConverter dataConverter = new DataConverterImpl();

Choose a reason for hiding this comment

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

No, you actually dont, please dont change readme file, it is not related to task, and changes that you made are redundant

@aunkerrr
Copy link
Author

Please be careful and read comments carefully, and push new solution after you carefully checked all comments))

okay thanks a lot for your review
hope i did it well

@aunkerrr aunkerrr requested a review from ahoienko February 3, 2025 18:50
@@ -0,0 +1,6 @@
fruit,quantity

Choose a reason for hiding this comment

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

resources folder should be in main folder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants