-
Notifications
You must be signed in to change notification settings - Fork 0
Console APP pr #1
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
base: main
Are you sure you want to change the base?
Conversation
src/main/java/Main.java
Outdated
| public class Main { | ||
|
|
||
| private static int numberOfPersons = 0; | ||
| public static final Scanner SCANNER = new Scanner(System.in); |
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.
Лучше не иметь больше одного сканера в проекте.
src/main/java/Calculator.java
Outdated
|
|
||
| public class Calculator { | ||
|
|
||
| private final Scanner SCANNER = new Scanner(System.in); |
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.
С заглавных букв принято называть константы, сканер это класс, он не может быть константой по определению, лучше назвать с маленькой.
Константами считается только то, что можем быть известно на этапе компиляции и просто подставлено в код, т.е. строки, числа, etc.
src/main/java/Calculator.java
Outdated
| if (command.equalsIgnoreCase("завершить")) { | ||
| break; | ||
| } | ||
| System.out.println("Введите стоимость товара например: '10,45'"); |
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.
Тут должна быть точка, не заяпятая.
src/main/java/Calculator.java
Outdated
| while (true) { | ||
| String command; | ||
| System.out.println("Добавить товар? Для окончания введите 'завершить'"); | ||
| command = SCANNER.next(); |
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.
Scanner.next() читает слово до следующего пробела, то есть если я захочу добавить например "Чипсы Лейс", то ввод будет прочитан неккоректно. Подумай как это исправить.
| System.out.printf("Товар %s успешно добавлен%n", name); | ||
| } | ||
|
|
||
| public void printGoods() { | ||
| System.out.println("Добавленные товары:"); | ||
| goods.forEach((k, v) -> System.out.printf("%s %.2f%n", k, v)); | ||
| } | ||
|
|
||
| public double getFinalSum(int numberOfPersons) { | ||
| return goods.values().stream().mapToDouble(Double::doubleValue).sum() / numberOfPersons; | ||
| } |
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.
Очень круто, что используешь форматтинг в printf и стримы!
|
|
||
| private static int numberOfPersons = 0; | ||
| public static final Scanner SCANNER = new Scanner(System.in); | ||
| private static final String GREETING_MESSAGE = "Введите число на сколько человек разделить счет:"; |
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.
Здорово, что вынес текст в константу.
| String command; | ||
| System.out.println("Добавить товар? Для окончания введите 'завершить'"); | ||
| command = SCANNER.next(); | ||
| command = Main.scanner.nextLine(); |
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.
Попытка засчитана, но так тоже лучше не делать. Если прям нужно где-то считывать, то лучше передавать сканнер в параметрах.
| } | ||
| } | ||
| SCANNER.close(); | ||
| Main.scanner.close(); |
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.
Я это забыл удалить просто xD
No description provided.