-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
base: master
Are you sure you want to change the base?
add solution #1242
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.
let's improve your solution :)
Integer newQuantity = db.containsKey(fruit) | ||
? db.get(fruit) + quantity | ||
: 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.
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"); |
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.
let's create a custom exception for such cases
return operation; | ||
} | ||
} | ||
return null; |
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.
let's throw an exception instead
import java.util.List; | ||
|
||
public interface DataConverter { | ||
List<FruitTransaction> convertStringsDataToFruitTransactions(List<String> stringsData); |
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.
let's make the method name concise and clear enough
if (transaction.getQuantity() < 0) { | ||
throw new RuntimeException("Quantity can't be less than 0"); | ||
} |
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.
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"); |
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.
do not use the most general RuntimeException. Create your own one for such a case. Fix the same issue in all places
private static final String PATH_TO_INPUT_FILE = "transactions.csv"; | ||
private static final String PATH_TO_OUTPUT_FILE = "report.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.
these constants seem to be completely redundant
|
||
@Override | ||
public List<String> read(String filePath) { | ||
File file = new File(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.
do you really need this variable?
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 need to create new File
File file = new File(filePath); | ||
file.delete(); |
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
|
||
public class ReportGeneratorImpl implements ReportGenerator { | ||
private static final String DATA_SPLITTER = ","; | ||
private static final String REPORT_FIRST_LINE = "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.
use System.lineSeparator(), fix in all places
private static final Map<String, Integer> storage; | ||
|
||
static { | ||
storage = new HashMap<>(); | ||
} | ||
|
||
public static Map<String, Integer> getStorage() { | ||
return 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.
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 { | ||
|
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 empty line
|
||
@Override | ||
public List<String> read(String filePath) { | ||
File file = new File(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.
don't need to create new File
StandardOpenOption.APPEND); | ||
|
||
} catch (IOException e) { | ||
throw new RuntimeException(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.
add informative message with parameters
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 few minor comments
@Override | ||
public List<String> read(String filePath) { | ||
try { | ||
return Files.readAllLines(new File(filePath).toPath()); |
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.
What is the reason to create a file, it is possible just read by path?
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"); | ||
} |
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.
Better to move this logic to separate method to make code more clear
int index = START_ROW_INDEX; | ||
if (stringsData.get(index).equals(INPUT_POTENTIAL_FIRST_ROW)) { | ||
index++; | ||
} | ||
for (; index < stringsData.size(); index++) { |
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.
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
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! A minor code style recommendation ;)
if (repository.hasFruit(transaction.getFruit())) { | ||
repository.remove(transaction.getFruit(), transaction.getQuantity()); | ||
} else { | ||
throw new InvalidInputException("There are no " | ||
+ transaction.getFruit() | ||
+ " in 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.
better avoid if-else construction
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())) { |
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 same
No description provided.