Repository navigation
Первый ПР для первого задания #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?
Changes from all commits
e1f6893
c521234
cf62a41
5ae261d
9f3b33c
557c906
8076552
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Goods { | ||
| StringBuffer goodList = new StringBuffer(); | ||
| float priceSum = 0.0f; | ||
| public void collectNamesAndPrices() { | ||
| String name, stopWord, userChoseToStop; | ||
| float price = 0.0f; | ||
| boolean stopper = false; | ||
| stopWord = "завершить"; | ||
| Scanner goodScanner = new Scanner(System.in); | ||
| while(!stopper) { | ||
| System.out.println("Введите название товара:"); | ||
| name = goodScanner.nextLine(); | ||
| System.out.println("Введите цену товара в формате: рубли.копейки:"); | ||
| while(true) { | ||
| try { | ||
| price = Float.parseFloat(goodScanner.nextLine()); | ||
| if (price > 0) { | ||
| break; | ||
| } | ||
| System.out.println("Цена должна быть больше нуля. Попробуйте ещё раз:"); | ||
| } catch (NumberFormatException exception) { | ||
| System.out.println("Цена должна быть в формате: рубли.копейки. Попробуйте ещё раз:"); | ||
| } | ||
| } | ||
| goodList.append(name + "\n"); | ||
| priceSum = priceSum + price; | ||
| System.out.println("Товар успешно добавлен. Добавить ещё товар или завершить?"); | ||
| userChoseToStop = goodScanner.nextLine(); | ||
| stopper = userChoseToStop.equalsIgnoreCase(stopWord); | ||
| } | ||
| goodScanner.close(); | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,49 +1,45 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| int friendsNumber; | ||
| System.out.println("На скольких человек необходимо разделить счёт?"); | ||
|
|
||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| int friendCount; | ||
| while (true) { | ||
| System.out.println("На сколько человек необходимо разделить счет?"); | ||
| friendCount = scanner.nextInt(); | ||
|
|
||
| if (friendCount > 1) { | ||
| break; | ||
| } else if (friendCount == 1) { | ||
| System.out.println( | ||
| "Нет смысла делить сумму на одного человека. Давайте попробуем ввести другое значение, которое будет больше единицы."); | ||
| } else { | ||
| System.out.println("Неверное количество друзей. Значение должно быть болье единицы, давайте попробуем еще раз."); | ||
|
|
||
| while (true){ | ||
| try{ | ||
| friendsNumber = Integer.parseInt(scanner.nextLine()); | ||
| if (friendsNumber > 1) { | ||
| break; | ||
| } | ||
| System.out.println("Упс! Для верного расчёта количество человек должно быть больше 1. Попробуйте ещё раз:"); | ||
| } | ||
| catch (NumberFormatException exception) { | ||
| System.out.println("Нужно ввести целое число больше 1. Попробуйте ещё раз:"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Имеет смысл выносить такие большие логические фрагменты в отдельные методы и вызывать их там, где необходимо. Это делает программу более удобочитаемой, когда разнородная логика не смешивается в один сплошной код. |
||
| } | ||
| } | ||
|
|
||
| Calculator calculator = new Calculator(friendCount); | ||
|
|
||
| while (true) { | ||
| System.out.println("Введите название товара"); | ||
| String name = scanner.next(); | ||
|
|
||
| System.out.println("Введите стоимость товара в формате: 'рубли.копейки' [10.45, 11.40]"); | ||
| double price = scanner.nextDouble(); | ||
|
|
||
| calculator.addItem(new Item(name, price)); | ||
|
|
||
| System.out.println( | ||
| "Хотите добавить еще один товар? Введите любой символ для продолжения, либо 'Завершить' если больше нет товаров для добавления"); | ||
| String answer = scanner.next(); | ||
|
|
||
| if (answer.equalsIgnoreCase("Завершить")) { | ||
| break; | ||
| Goods goods = new Goods(); | ||
| goods.collectNamesAndPrices(); | ||
| float debtPerFriend = goods.priceSum / friendsNumber; | ||
| String rubleForm = getWordForm((int)debtPerFriend); | ||
| String debtPhraseTemplate = "С каждого %.2f %s"; | ||
| System.out.print("Добавленные товары:\n" + goods.goodList); | ||
| System.out.print(String.format(debtPhraseTemplate, debtPerFriend, rubleForm)); | ||
| } | ||
| public static String getWordForm(int num){ | ||
| int num100 = num % 100; | ||
| if (num100 <= 4 || num100 >= 21) { | ||
| int num10 = num100 % 10; | ||
| if (num10 == 1) { | ||
| return " рубль"; | ||
| } else if (num10 > 1 && num10 < 5) { | ||
| return " рубля"; | ||
| } | ||
| } | ||
|
|
||
| double result = calculator.divideSum(); | ||
| Formatter formatter = new Formatter(); | ||
|
|
||
| System.out.println(calculator.cart); | ||
| System.out.println("Каждому человеку к оплате: " + formatter.roundResult(result) + " " + formatter.formatValue(result)); | ||
| return " рублей"; | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
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.
Также можно использовать сокращенную конструкцию
priceSum += price;