-
Notifications
You must be signed in to change notification settings - Fork 220
первое домашнее задание #96
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Calculate { | ||
| static String toComplete = "завершить"; | ||
|
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. static можно убрать |
||
|
|
||
| List<String> menuList = new LinkedList<>(); | ||
| List<Double> priceList = new ArrayList<>(); | ||
| String menu; | ||
| double price = 0; | ||
| double sum; | ||
|
|
||
| public void product() { | ||
|
|
||
| Scanner scanner = new Scanner(System.in); | ||
| while (true) { | ||
| System.out.println("Введите название товара : "); | ||
| menu = scanner.next(); | ||
| menuList.add(menu); | ||
| System.out.println("Введите цену товара в формате 'рубли.копейки' [10.45, 11.40] :"); | ||
|
|
||
| while (!scanner.hasNextDouble()) { | ||
| System.out.println("Введите корректное значение :"); | ||
| scanner.next(); | ||
| } | ||
|
|
||
| price = scanner.nextDouble(); | ||
| if (price > 0) { | ||
| priceList.add(price); | ||
| sum = sum + price; | ||
| System.out.println("Товар успешно добавлен! "); | ||
| } | ||
|
|
||
| while (price <= 0) { | ||
| if (price <= 0) { | ||
|
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. В while уже проверяется это условие, if можно убрать |
||
| System.out.println("Ошибка цена не может быть отрицательной,ошибка введите цену заного"); | ||
| price = scanner.nextDouble(); | ||
| if (price > 0) { | ||
| priceList.add(price); | ||
| sum = sum + price; | ||
| System.out.println("Товар успешно добавлен! "); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| System.out.println("Если хотите продолжить введите - любой символ,если хотите остановиться введите 'завершить' "); | ||
| menu = scanner.next(); | ||
| if (menu.equalsIgnoreCase(toComplete)) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public String GetRubleAddition(double num) { | ||
| int numToInt = (int) num; | ||
| double preLastDigit = num % 100 / 10; | ||
| if (preLastDigit == 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. Из-за того, что переменная типа double, это условие сработает только для num = 10. Если бы была int, работало бы для 11-19 |
||
| return "рублей"; | ||
| } | ||
| switch (numToInt % 10) { | ||
| case 1: | ||
| return "рубль"; | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| return "рубля"; | ||
| default: | ||
| return "рублей"; | ||
| } | ||
| } | ||
|
|
||
| public String GetHumanAdddition(int num) { | ||
|
|
||
| double preLastDigit = num % 100 / 10; | ||
| if (preLastDigit == 1) { | ||
| return "Человек"; | ||
| } | ||
| switch (num % 10) { | ||
| case 1: | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| return "Человека"; | ||
| default: | ||
| return "рублей"; | ||
|
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. Кажется, тут ошибка) |
||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,53 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимость | ||
| Scanner scanner = new Scanner(System.in); | ||
| System.out.println("На сколько человек нужно разделить счет ?"); | ||
| while (!scanner.hasNextInt()) { | ||
| System.out.println("Введите корректное значение :"); | ||
| scanner.next(); | ||
| } | ||
| int human = scanner.nextInt(); | ||
|
|
||
|
|
||
| while (human <= 1) { | ||
| if (human < 1) { | ||
| System.out.println("Колличество человек " + human); | ||
| System.out.println("некоректное значение для подсчета.Введите колличество человек заного "); | ||
| human = scanner.nextInt(); | ||
| } | ||
| if (human == 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. Поскольку в while может быть только human <= 1, а human < 1 проверено в верхнем if, тут можно сделать просто else к нему |
||
| System.out.println("Колличество человек " + human); | ||
| System.out.println("нет смысла ничего считать и делить.Введите колличество человек заного"); | ||
| human = scanner.nextInt(); | ||
| } | ||
| } | ||
| Calculate calculate = new Calculate(); | ||
|
|
||
| calculate.product(); | ||
|
|
||
|
|
||
|
|
||
| double check = calculate.sum / human; | ||
|
|
||
| System.out.println("Счет нужно разделить на : " + human + " " + calculate.GetHumanAdddition(human)); | ||
| System.out.println("Список выбранных товаров: "); | ||
| for (String str : calculate.menuList) { | ||
| System.out.println(str); | ||
| } | ||
| System.out.println("Общая сумма заказа : " + String.format("%.2f", calculate.sum) + " " + calculate.GetRubleAddition(calculate.sum)); | ||
| System.out.println("Каждый должен заплатить :" + String.format("%.2f", +check) + " " + calculate.GetRubleAddition(check)); | ||
|
|
||
|
|
||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||