-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
base: master
Are you sure you want to change the base?
jv-1-fruit-shop #1285
Conversation
There was a problem hiding this 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! ✨
List<String> inputLines = fileReader.read("C:/Users/WeakRitta/IdeaProjects" | ||
+ "/jv-fruit-shop/src/main/java/result/reportToRead.csv"); |
There was a problem hiding this comment.
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.
fileWriter.write(report, "C:/Users/WeakRitta/IdeaProjects/jv-fruit-shop/src" | ||
+ "/main/java/result/finalReport.csv"); |
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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")); |
There was a problem hiding this comment.
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(",") |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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(","); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
create constant for comma
There was a problem hiding this 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 ;)
private static final String READ_PATH = "reportToRead.csv"; | ||
private static final String WRITE_PATH = "finalReport.csv"; | ||
|
There was a problem hiding this comment.
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
String operationCode = parts[0].trim(); | ||
String fruit = parts[1].trim(); | ||
int quantity; | ||
try { | ||
quantity = Integer.parseInt(parts[2].trim()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
throw exception
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
leave only one empty line
if (handler == null) { | ||
throw new IllegalArgumentException("No handler found" + transaction.getOperation()); | ||
} | ||
handler.handle(storage, transaction); |
There was a problem hiding this comment.
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.
handler.handle(storage, transaction); | |
return handler; |
|
||
public class ShopServiceImpl implements ShopService { | ||
private final OperationStrategy operationStrategy; | ||
private final Map<String, Integer> storage = new HashMap<>(); |
There was a problem hiding this comment.
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()) |
There was a problem hiding this comment.
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(",") |
There was a problem hiding this comment.
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())); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this 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 ;)
private static final String READ_PATH = "recources/reportToRead.csv"; | ||
private static final String WRITE_PATH = "recources/finalReport.csv"; |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String fruit = parts[1].trim(); | |
String fruit = parts[1]; |
|
||
public class FileReaderImpl implements FileReader { | ||
@Override | ||
public List<String> read(String filePath) throws IOException { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); |
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); | |
} |
@Override | ||
public String generateReport(Map<String, Integer> storage) { | ||
StringBuilder report = new StringBuilder(); | ||
report.append(HEADER).append(System.lineSeparator()); | ||
|
||
storage.entrySet() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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() |
There was a problem hiding this 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
String operationCode = parts[0]; | ||
String fruit = parts[1]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Magic numbers
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)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move to separate method
for (int i = 1; i < lines.size(); i++) { | ||
String[] parts = lines.get(i).split(COMMA); | ||
if (parts.length != 3) { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
void write(String content, String filePath) throws IOException; | |
void write(String content, String filePath) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not resolved
public String generateReport() { // Remove the Map parameter | ||
StringBuilder report = new StringBuilder(); | ||
report.append(HEADER).append(System.lineSeparator()); | ||
|
||
Storage.storage.entrySet() // Use Storage.storage directly |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
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(); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 don
t change readme file, it is not related to task, and changes that you made are redundant
There was a problem hiding this 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))
String operationCode = parts[0]; | ||
String fruit = parts[1]; | ||
int quantity = parseQuantity(parts[2], lineNumber); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import java.util.List; | ||
|
||
public interface FileReader { | ||
List<String> read(String filePath) throws IOException; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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
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(); |
There was a problem hiding this comment.
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 don
t change readme file, it is not related to task, and changes that you made are redundant
okay thanks a lot for your review |
@@ -0,0 +1,6 @@ | |||
fruit,quantity |
There was a problem hiding this comment.
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
No description provided.