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 solution #1242

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

add solution #1242

wants to merge 6 commits into from

Conversation

IhorRome
Copy link

No description provided.

Copy link

@sarakhmen sarakhmen left a comment

Choose a reason for hiding this comment

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

let's improve your solution :)

Comment on lines 12 to 14
Integer newQuantity = db.containsKey(fruit)
? db.get(fruit) + quantity
: quantity;

Choose a reason for hiding this comment

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

map has native merge method to handle cases when key exists and doesn't exist

@Override
public void remove(String fruit, int quantity) {
if (db.get(fruit) - quantity < 0) {
throw new RuntimeException("Not enough " + fruit + " in storage to remove");

Choose a reason for hiding this comment

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

let's create a custom exception for such cases

return operation;
}
}
return null;

Choose a reason for hiding this comment

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

let's throw an exception instead

import java.util.List;

public interface DataConverter {
List<FruitTransaction> convertStringsDataToFruitTransactions(List<String> stringsData);

Choose a reason for hiding this comment

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

let's make the method name concise and clear enough

Comment on lines 7 to 9
if (transaction.getQuantity() < 0) {
throw new RuntimeException("Quantity can't be less than 0");
}

Choose a reason for hiding this comment

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

this isn't a good place for transaction validations. It's better to validate quantity before or during parsing phase

if (repository.hasFruit(transaction.getFruit())) {
repository.remove(transaction.getFruit(), transaction.getQuantity());
} else {
throw new RuntimeException("Can't return fruits that are/were not in storage");

Choose a reason for hiding this comment

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

do not use the most general RuntimeException. Create your own one for such a case. Fix the same issue in all places

Comment on lines 12 to 14
private static final String PATH_TO_INPUT_FILE = "transactions.csv";
private static final String PATH_TO_OUTPUT_FILE = "report.csv";

Choose a reason for hiding this comment

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

these constants seem to be completely redundant


@Override
public List<String> read(String filePath) {
File file = new File(filePath);

Choose a reason for hiding this comment

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

do you really need this variable?

Choose a reason for hiding this comment

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

don't need to create new File

Comment on lines 27 to 28
File file = new File(filePath);
file.delete();

Choose a reason for hiding this comment

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

same here


public class ReportGeneratorImpl implements ReportGenerator {
private static final String DATA_SPLITTER = ",";
private static final String REPORT_FIRST_LINE = "fruit,quantity\n";

Choose a reason for hiding this comment

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

use System.lineSeparator(), fix in all places

Comment on lines 7 to 15
private static final Map<String, Integer> storage;

static {
storage = new HashMap<>();
}

public static Map<String, Integer> getStorage() {
return storage;
}

Choose a reason for hiding this comment

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

Suggested change
private static final Map<String, Integer> storage;
static {
storage = new HashMap<>();
}
public static Map<String, Integer> getStorage() {
return storage;
}
public static final Map<String, Integer> storage = new HashMap<>();

import java.util.Map;

public class OperationStrategyImpl implements OperationStrategy {

Choose a reason for hiding this comment

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

remove empty line

Suggested change


@Override
public List<String> read(String filePath) {
File file = new File(filePath);

Choose a reason for hiding this comment

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

don't need to create new File

StandardOpenOption.APPEND);

} catch (IOException e) {
throw new RuntimeException(e);

Choose a reason for hiding this comment

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

add informative message with parameters

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 few minor comments

@Override
public List<String> read(String filePath) {
try {
return Files.readAllLines(new File(filePath).toPath());

Choose a reason for hiding this comment

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

What is the reason to create a file, it is possible just read by path?

Comment on lines 30 to 35
if (arr.length != EXPECTED_ARRAY_LENGTH) {
throw new InvalidInputException("Invalid input");
}
if (Integer.parseInt(arr[QUANTITY_INDEX]) < 0) {
throw new InvalidInputException("Quantity can't be less than 0");
}

Choose a reason for hiding this comment

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

Better to move this logic to separate method to make code more clear

Comment on lines 23 to 27
int index = START_ROW_INDEX;
if (stringsData.get(index).equals(INPUT_POTENTIAL_FIRST_ROW)) {
index++;
}
for (; index < stringsData.size(); index++) {

Choose a reason for hiding this comment

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

Better to not overcomplicate, assume that input file is correct, and first row will be with index 0, so no reason to calculate index of header, just start iteration with index 1

Copy link

@okuzan okuzan left a comment

Choose a reason for hiding this comment

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

image
be careful, always check the build locally

@IhorRome IhorRome requested a review from okuzan November 17, 2024 11:45
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! A minor code style recommendation ;)

Comment on lines +17 to +23
if (repository.hasFruit(transaction.getFruit())) {
repository.remove(transaction.getFruit(), transaction.getQuantity());
} else {
throw new InvalidInputException("There are no "
+ transaction.getFruit()
+ " in storage");
}

Choose a reason for hiding this comment

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

better avoid if-else construction

Suggested change
if (repository.hasFruit(transaction.getFruit())) {
repository.remove(transaction.getFruit(), transaction.getQuantity());
} else {
throw new InvalidInputException("There are no "
+ transaction.getFruit()
+ " in storage");
}
if (repository.hasFruit(transaction.getFruit())) {
repository.remove(transaction.getFruit(), transaction.getQuantity());
return;
}
throw new InvalidInputException("There are no "
+ transaction.getFruit()
+ " in storage");


@Override
public void handle(FruitTransaction transaction) {
if (fruitRepository.hasFruit(transaction.getFruit())) {

Choose a reason for hiding this comment

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

the same

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.

6 participants