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

done #1296

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

done #1296

wants to merge 4 commits into from

Conversation

xMaXD
Copy link

@xMaXD xMaXD commented Feb 24, 2025

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.

Thank you for your hard work on this project! 😊 There are a few critical issues that need to be addressed before we can approve the pull request. The remove method in the Storage class should update the quantity of the fruit rather than removing it from the map. It's essential to ensure that the resulting quantity is non-negative before updating the map. If the balance is negative, a RuntimeException should be thrown, as specified in the task checklist. Once these issues are resolved, 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.

public class PurchaseOperation implements OperationHandler {
@Override
public void apply(FruitTransaction transaction) {
Storage.remove(transaction.getFruit(), transaction.getQuantity());

Choose a reason for hiding this comment

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

Ensure that the Storage.remove method checks if the resulting quantity is non-negative before updating the storage. Throw a RuntimeException if the balance is negative, as specified in the checklist: "Handling Purchase operation. Check result balance before saving it in the Storage - it should be positive. Throw RuntimeException in case the balance is negative."

Comment on lines 13 to 14
public static void remove(String fruit, int quantity) {
fruits.remove(fruit, fruits.getOrDefault(fruit, 0) - quantity);

Choose a reason for hiding this comment

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

The remove method should update the quantity of the fruit instead of removing it from the map. Use fruits.put(fruit, fruits.getOrDefault(fruit, 0) - quantity) to update the quantity. Additionally, ensure that the resulting quantity is non-negative before updating the map, and throw a RuntimeException if the balance is negative, as specified in the checklist.

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 code is mostly on point, but there's a critical issue that needs to be addressed before we can approve the pull request. The remove method in the Storage class is currently using Map.remove(Object key, Object value), which is not suitable for updating quantities. This could lead to incorrect stock levels. Please ensure that the method updates the quantity correctly and checks for non-negative values, throwing a RuntimeException if the balance becomes negative. Once you address this, 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.

public class PurchaseOperation implements OperationHandler {
@Override
public void apply(FruitTransaction transaction) {
Storage.remove(transaction.getFruit(), transaction.getQuantity());

Choose a reason for hiding this comment

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

Ensure that the Storage.remove method checks if the resulting quantity is non-negative before updating the storage. If the balance becomes negative, throw a RuntimeException to handle this case. This is important to prevent negative stock levels in the storage.

Comment on lines 13 to 14
public static void remove(String fruit, int quantity) {
fruits.remove(fruit, fruits.getOrDefault(fruit, 0) - quantity);

Choose a reason for hiding this comment

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

The remove method should update the quantity of the fruit instead of using Map.remove(Object key, Object value). Ensure that the quantity does not become negative. If the resulting quantity is negative, throw a RuntimeException to handle this case.

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 demonstrates a solid understanding of the task requirements and effectively addresses the main functionality. However, there are a few areas for improvement:

  1. Package Structure: Ensure your classes are organized into the recommended package structure, such as placing the Storage class in a db package.

  2. SOLID Principles: Consider refactoring the Storage class to adhere to the Dependency Inversion Principle by avoiding direct static access.

  3. Encapsulation: Make the fruits map in the Storage class private and provide access through methods to maintain encapsulation.

  4. Exception Handling: Improve the exception messages in the PurchaseOperation class to provide more informative feedback.

  5. File Management: Ensure that files for reading from and writing to CSV files are included in the src/main/resources folder.

These improvements will enhance the maintainability and robustness of your code. Remember, addressing these issues will not only help you in this project but also in future endeavors. Keep up the good work, and continue learning from each experience! 🚀

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

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.

Review Summary

Great job on implementing the BalanceOperation, DataConverter, and other classes! Your code demonstrates a solid understanding of interfaces and the strategy pattern. 🎉

However, there are a few areas that need improvement:

  1. Exception Handling: In the PurchaseOperation class, the exception handling could be more informative. Consider providing more context in the exception message, such as including the fruit type and the attempted quantity, to make debugging easier.

  2. Package Structure: The project would benefit from a more organized package structure. Consider separating your classes into packages like db, model, service, service.impl, and strategy.

  3. SOLID Principles: Ensure that all services are independent and adhere to SOLID principles. The main() method should orchestrate the flow by calling independent services.

  4. File Placement: Input and output files should ideally be placed in the src/main/resources folder to maintain a clean project structure.

  5. Avoid Hardcoding: Try to avoid hardcoding file paths and other values outside of the Main class.

The decision to approve is made to help you move forward with your learning. Please review the comments and address these issues in future iterations to enhance the quality and maintainability of your code. Keep up the good work and continue to learn from these experiences! 🚀

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

Comment on lines 8 to 9
} catch (RuntimeException e) {
throw new RuntimeException("Balance is negative");

Choose a reason for hiding this comment

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

Consider providing more context in the exception message, such as including the fruit type and the attempted quantity, to make debugging easier.

@@ -0,0 +1,8 @@
package core.basesyntax;

Choose a reason for hiding this comment

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

Let's revise common mistakes file and check project structure

class DataConverterImpl implements DataConverter {
@Override
public List<FruitTransaction> convertToTransaction(List<String> data) {
// Пропускаем первую строку с заголовками

Choose a reason for hiding this comment

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

remove comment

int quantity = Integer.parseInt(parts[2]);
return new FruitTransaction(op, fruit, quantity);
})
.collect(Collectors.toList());

Choose a reason for hiding this comment

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

Suggested change
.collect(Collectors.toList());
.toList();

return data.stream()
.skip(1)
.map(line -> {
String[] parts = line.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

.skip(1)
.map(line -> {
String[] parts = line.split(",");
String opCode = parts[0];

Choose a reason for hiding this comment

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

create constants for all magic numbers

public static void main(String[] arg) throws IOException {
// 1. Read the data from the input CSV file
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.

input and output files should be in resources folder
create constants for files pathes

public String getReport() {
StringBuilder report = new StringBuilder();
Storage.getFruits().forEach((fruit, quantity) -> report.append(fruit)
.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

Storage.getFruits().forEach((fruit, quantity) -> report.append(fruit)
.append(",")
.append(quantity)
.append("\n"));

Choose a reason for hiding this comment

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

Use System.lineSeparator() instead of "\n"


import java.util.List;

class ShopServiceImpl implements ShopService {

Choose a reason for hiding this comment

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

Suggested change
class ShopServiceImpl implements ShopService {
public class ShopServiceImpl implements ShopService {

}
}

public static Map<String, Integer> getFruits() {

Choose a reason for hiding this comment

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

redundant method

@xMaXD xMaXD requested a review from Elena-Bruyako February 27, 2025 16:11
import java.util.Map;

public class Main {
private static final String REPORT_TO_READ_FILE_PATH = "reportToRead.csv";

Choose a reason for hiding this comment

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

files should be placed in resources folder


public String getCode() {
return code;
}

Choose a reason for hiding this comment

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

You can also check if particular code exists in public static Operation getOperation(String code)

String[] parts = line.split(SEPARATOR_SIGN);
String opCode = parts[OPERATOR_PART];
FruitTransaction.Operation op = switch (opCode) {
case "b" -> FruitTransaction.Operation.BALANCE;

Choose a reason for hiding this comment

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

let's use getOperation(code) method instead of switch case
See comment in FruitTransaction

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