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

Implementation of the task and all necessary objects for the correct … #1234

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

Conversation

Petliuk
Copy link

@Petliuk Petliuk commented Nov 3, 2024

…operation of the program.

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.

Your code is well-structured and follows many good practices, but there are a few critical issues that need to be addressed before we can approve the pull request. Specifically, the code does not fully adhere to the SOLID principles, particularly the Dependency Inversion Principle, as the Storage class is directly accessed by operation handlers. Additionally, the services are not completely independent, and the package structure is missing the strategy package. These issues impact the core requirements of the task. Please address these points to ensure your solution meets the necessary standards. Keep up the good work, and I'm confident you'll nail it with these adjustments! 😊

✨ 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.

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 effort on your submission! 🎉 While your code has been approved to help you move forward with your learning journey, there are several important areas for improvement that you should address:

  1. Instantiation Error: Ensure that FruitStorage is instantiated correctly. If Storage is an interface or abstract class, replace it with a concrete implementation.

  2. File Paths: Verify that the file paths for "reportToRead.csv" and "finalReport.csv" are correct and accessible. Consider using a configuration file or environment variable for better management.

  3. Error Handling: Add error handling for file operations, transaction processing, and potential NumberFormatException during integer parsing to prevent runtime exceptions.

  4. Null Checks: Implement null checks or default behaviors for operation handlers to avoid NullPointerException.

  5. Static Map in Storage: Review the use of a static map in Storage to ensure it aligns with your application's requirements, especially if independent storage instances are needed.

  6. Report Feedback: Consider adding checks or messages for empty storage to provide clearer output in reports.

  7. Exception Feedback: Enhance IllegalArgumentException messages with more details for better debugging.

  8. Code Readability: Extract magic strings like "type" into constants for improved readability and maintainability.

These improvements will enhance the robustness and maintainability of your code. 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! ✨

// and call their methods, but do not write any business logic in the `main` method!

public static void main(String[] args) {
FruitStorage storage = new Storage();

Choose a reason for hiding this comment

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

The instantiation of FruitStorage with new Storage() may be incorrect if Storage is an interface or abstract class. Ensure Storage is a concrete class or replace it with an appropriate implementation.

FruitStorage storage = new Storage();

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.

Ensure that the file path "reportToRead.csv" is correct and accessible. Consider using a configuration file or environment variable for file paths.

String resultingReport = reportGenerator.getReport();

FileWriter fileWriter = new FileWriterImpl();
fileWriter.write(resultingReport, "finalReport.csv");

Choose a reason for hiding this comment

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

Ensure that the file path "finalReport.csv" is correct and accessible. Consider using a configuration file or environment variable for file paths.

Comment on lines +32 to +52
FileReader fileReader = new FileReaderImpl();
List<String> inputReport = fileReader.read("reportToRead.csv");

DataConverter dataConverter = new DataConverterImpl();
final List<FruitTransaction> transactions = dataConverter.convertToTransaction(inputReport);

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

OperationStrategy operationStrategy = new OperationStrategyImpl(operationHandlers);
ShopService shopService = new ShopServiceImpl(operationStrategy);
shopService.process(transactions);

ReportGenerator reportGenerator = new ReportGeneratorImpl(storage);
String resultingReport = reportGenerator.getReport();

FileWriter fileWriter = new FileWriterImpl();
fileWriter.write(resultingReport, "finalReport.csv");

Choose a reason for hiding this comment

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

Consider adding error handling for file operations and transaction processing to prevent runtime exceptions.

String[] parts = line.split(",");
FruitTransaction.Operation operation = getOperationFromCode(parts[0].trim());
String fruit = parts[1].trim();
int 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.

Consider adding error handling for Integer.parseInt to manage potential NumberFormatException if the quantity is not a valid integer.


@Override
public OperationHandler getHandler(FruitTransaction.Operation operation) {
return operationHandlers.get(operation);

Choose a reason for hiding this comment

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

Consider adding a null check or default behavior if operationHandlers.get(operation) returns null. This will prevent potential NullPointerException if an operation does not have a corresponding handler.

if (currentQuantity >= purchaseQuantity) {
storage.updateFruitQuantity(fruit, currentQuantity - purchaseQuantity);
} else {
throw new IllegalArgumentException("Insufficient inventory to purchase " + fruit);

Choose a reason for hiding this comment

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

Consider providing more detailed feedback in the IllegalArgumentException, such as including the current and requested quantities, to aid in debugging and user understanding.

for (Map.Entry<String, Integer> entry : storage.getAllFruits().entrySet()) {
report.append(entry.getKey()).append(",").append(entry.getValue()).append("\n");
}
return report.toString();

Choose a reason for hiding this comment

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

Consider adding a check or message for when the storage is empty, to provide clearer output or feedback in the report.

public void process(List<FruitTransaction> transactions) {
for (FruitTransaction transaction : transactions) {
OperationHandler handler = operationStrategy.getHandler(transaction.getOperation());
handler.handle(transaction);

Choose a reason for hiding this comment

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

Consider adding a null check or exception handling for the handler to manage cases where operationStrategy.getHandler(transaction.getOperation()) returns null. This will prevent potential NullPointerException if an operation does not have a corresponding handler.

import java.util.Map;

public class Storage implements FruitStorage {
private static final Map<String, Integer> fruits = new HashMap<>();

Choose a reason for hiding this comment

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

The use of a static map means that the storage is shared across all instances of Storage. Ensure this design choice aligns with the application's requirements, as it may lead to unexpected behavior if multiple instances are expected to have independent storage.

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.

2 participants