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

Add core services and model for fruit shop calculations #1239

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

Conversation

SnkvS
Copy link

@SnkvS SnkvS commented Nov 8, 2024

Implemented essential services along with the data model to manage fruit transactions and calculations. Introduced strategies for operations, CSV parsing, storage interaction, and report generation.

Implemented essential services along with the data model to manage fruit transactions and calculations. Introduced strategies for operations, CSV parsing, storage interaction, and report generation.
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.


Thank you for your hard work on this project! Your implementation is quite solid and meets many of the task requirements. However, there are a few critical issues that need to be addressed before we can approve the pull request:

  1. Resource Management: The readLines method in CsvReaderServiceImpl does not close the FileReader and BufferedReader, which can lead to resource leaks. Please use a try-with-resources statement to ensure these resources are closed automatically.

  2. Type Consistency: The calculate method in FruitShopCalculationServiceImpl should return a ShopStorage<T> instead of FruitShopStorage to maintain consistency with the generic type T in the ShopCalculationService interface. This change will enhance flexibility and align with the interface definition.

  3. Generic Usage: The save method in the ShopStorage interface should use the generic type T instead of Fruit to ensure the interface can handle any type T. Update the method signature to void save(T t, int value, OperationType operationType);.

  4. Spelling Error: Correct the typo in the method name validateCalcualtion to validateCalculation to avoid potential confusion.

These changes are crucial for ensuring the code is robust, flexible, and maintainable. Once these issues are addressed, your code will be in great shape! Keep up the great work! 🚀

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

🔄 Fix the issues, commit the changes, and then re-request my review using the Re-request review button in the Reviewers tab.

return fruitStorage.keySet();
}

private int validateCalcualtion(int result) {

Choose a reason for hiding this comment

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

There is a typo in the method name validateCalcualtion. It should be validateCalculation to correctly reflect its purpose and avoid potential confusion.

public interface ShopStorage<T> {
int getAmount(T t);

void save(Fruit fruit, int value, OperationType operationType);

Choose a reason for hiding this comment

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

The save method should use the generic type T instead of Fruit to maintain consistency with the rest of the interface. Change the method signature to void save(T t, int value, OperationType operationType); to ensure the interface can handle any type T.

import java.util.List;

public interface ShopCalculationService<T> {
FruitShopStorage calculate(List<AbstractTransaction<T>> transactions);

Choose a reason for hiding this comment

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

The calculate method currently returns a FruitShopStorage, which is specific to Fruit. To maintain consistency with the generic type T, consider changing the return type to ShopStorage<T>. This will allow the interface to be more flexible and handle different types of storage.

Comment on lines 26 to 33
private Stream<String> readLines(String filePath) {
try {
FileReader fileReader = new FileReader(filePath);
return new BufferedReader(fileReader).lines();
} catch (IOException e) {
throw new RuntimeException("Cannot read file: " + filePath, e);
}
}

Choose a reason for hiding this comment

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

The readLines method opens a FileReader and BufferedReader but does not close them, which can lead to resource leaks. Consider using a try-with-resources statement to ensure these resources are closed automatically.

private void writeHeader(FileWriter writer) throws IOException {
writer.write(FIRST_LINE);
writer.write(System.lineSeparator());
writer.flush();

Choose a reason for hiding this comment

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

The flush() method call after writing the header is unnecessary when using a FileWriter inside a try-with-resources statement, as the writer will be flushed and closed automatically. Consider removing this call to simplify the code.

fileWriter.write(
item.toString().toLowerCase() + "," + storage.getAmount(item));
fileWriter.write(System.lineSeparator());
fileWriter.flush();

Choose a reason for hiding this comment

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

Similar to the previous comment, the flush() method call after writing each line is unnecessary when using a FileWriter inside a try-with-resources statement. The writer will be flushed and closed automatically. Consider removing this call to simplify the code.

}

@Override
public FruitShopStorage calculate(List<AbstractTransaction<Fruit>> abstractTransactions) {

Choose a reason for hiding this comment

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

The calculate method currently returns a FruitShopStorage, which is specific to Fruit. To maintain consistency with the generic type T in the ShopCalculationService interface, consider changing the return type to ShopStorage<Fruit>. This will allow the implementation to be more flexible and align with the interface definition.

Renamed FruitShopStorage to ShopStorage to generalize the storage interface. Fixed typographical errors in method names and improved readability in several method calls across multiple classes.
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 your progress! 🎉 While your implementation is mostly well-aligned with the task requirements, there are a few critical issues that need attention:

  1. Type Consistency: The calculate method in FruitShopCalculationServiceImpl should return a ShopStorage<T> instead of FruitShopStorage to maintain consistency with the generic type T in the ShopCalculationService interface. This will enhance flexibility and align with the interface definition.

  2. Resource Management: Ensure that the FileReader is included in the try-with-resources statement to prevent potential resource leaks. This can be achieved by using try (FileReader fileReader = new FileReader(filePath); BufferedReader bufferedReader = new BufferedReader(fileReader)) { ... }.

  3. Generic Usage: The save method in the ShopStorage interface should use the generic type T instead of Fruit to ensure the interface can handle any type T. Update the method signature to void save(T t, int value, OperationType operationType);.

  4. Map Implementation: Since Fruit is not an enum, using EnumMap for fruitStorage is not suitable. Consider using a HashMap or another appropriate Map implementation.

These changes are crucial for ensuring the code's flexibility and functionality. Please review the comments and address these issues in future iterations to enhance your code quality. Keep up the good work and continue learning from these experiences! 🚀

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

Comment on lines +20 to +22
ShopCalculationService<Fruit> shopCalculationService = new FruitShopCalculationServiceImpl(
new FruitShopStorage());
ShopStorage<Fruit> fruitStorage = shopCalculationService.calculate(fruitsOperations);

Choose a reason for hiding this comment

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

The calculate method in FruitShopCalculationServiceImpl should return a ShopStorage<T> instead of FruitShopStorage to maintain consistency with the generic type T in the ShopCalculationService interface. This change will enhance flexibility and align with the interface definition.


public class FruitShopStorage implements ShopStorage<Fruit> {
private static final String STORAGE_VALUE_ERROR = "Storage value cannot be negative";
private final Map<Fruit, Integer> fruitStorage = new EnumMap<>(Fruit.class);

Choose a reason for hiding this comment

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

The fruitStorage is currently using EnumMap, which is not appropriate since Fruit is not an enum. Consider using a HashMap or another suitable Map implementation.

}

@Override
public void save(Fruit fruit, int value, OperationType operationType) {

Choose a reason for hiding this comment

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

The save method in the ShopStorage interface should use the generic type T instead of Fruit to ensure the interface can handle any type T. Update the method signature to void save(T t, int value, OperationType operationType);.

Comment on lines +26 to +27
FileReader fileReader = new FileReader(filePath);
try (BufferedReader bufferedReader = new BufferedReader(fileReader)) {

Choose a reason for hiding this comment

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

The FileReader should be included in the try-with-resources statement to ensure it is closed properly. This prevents potential resource leaks. Consider using try (FileReader fileReader = new FileReader(filePath); BufferedReader bufferedReader = new BufferedReader(fileReader)) { ... }.

Comment on lines +16 to +19
@Override
public FruitShopStorage calculate(List<AbstractTransaction<Fruit>> abstractTransactions) {
abstractTransactions.forEach(this::processFruitTransaction);
return fruitShopStorage;

Choose a reason for hiding this comment

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

The calculate method should return a ShopStorage<Fruit> instead of FruitShopStorage to maintain consistency with the generic type T in the ShopCalculationService interface. This change will enhance flexibility and align with the interface definition.

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.

3 participants