-
Notifications
You must be signed in to change notification settings - Fork 220
HW #66
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
HW #66
Changes from all commits
83a7216
2616510
83e4746
ba761a0
544d467
9e32c9e
27c3c49
df34b55
bb64e7e
fab258e
52ba7b8
d75519a
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 |
|---|---|---|
| @@ -1,8 +1,110 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
|
|
||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| //считываем кол-во гостей | ||
| int visitor = 0; | ||
| System.out.println ("Введите колличество гостей"); | ||
| while (true) { | ||
| String str = scanner.next(); | ||
| while (!isNumeric(str)) { | ||
| System.out.println("Введите колличество гостей"); | ||
|
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, тогда отсюда и перед while её можно удалить |
||
| str = scanner.next(); | ||
| } | ||
| visitor = Integer.parseInt(str); | ||
| if ((visitor <= 1) | (visitor == 0)) { | ||
| System.out.println("Некорректное кол-во гостей"); | ||
| System.out.println("Введите колличество гостей"); | ||
| } else if (visitor > 1) { | ||
| break; | ||
| } | ||
| } | ||
| //считываем кол-во товаров | ||
| boolean enough = true; | ||
|
|
||
| Product list = new Product(); | ||
| Calculate menu_list = new Calculate(); | ||
|
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 (enough) { | ||
|
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(true) |
||
| System.out.println("Введите название "); | ||
| list.name = scanner.next(); | ||
| System.out.println("Введите цену"); | ||
| String str = scanner.next(); | ||
| while (!isNumeric(str)) { | ||
| System.out.println("Введите цену с разделителем \".\" "); | ||
| str = scanner.next(); | ||
| } | ||
| list.price = Double.parseDouble(str); | ||
| menu_list.getAddList(list.name, list.price); | ||
| menu_list.getListPrice(list.price); | ||
| System.out.println("Хотите ввести ещё товар?"); | ||
| System.out.println("Eсли вы хотите ввести ещё товар, введите ДА"); | ||
| System.out.println("Eсли вы хотите ввести ещё товар, введите Завершить "); | ||
|
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. Наверное, тут должно быть "Если вы не хотите больше вводить товары, введите Завершить" |
||
| String answer = scanner.next(); | ||
|
|
||
| if (answer.equalsIgnoreCase("завершить")) { | ||
| enough = false; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (!enough){ | ||
|
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. Этот if можно убрать, и переменную enough тоже. Из цикла while может быть выход, только если пользователь введет Завершить, и тогда должен будет выполниться код здесь |
||
| System.out.println("Добавленные товары:"); | ||
| System.out.println(menu_list.receipt); | ||
| float pay = (float) (menu_list.pay/visitor); | ||
| String rubString = ""; | ||
| int rub = (int) (pay % 10); | ||
| int rubTeen = (int) (pay % 100); | ||
| if ((rub == 0 ) || (rub == 5) || (rub == 6) || (rub == 7 ) || (rub == 8 ) || (rub == 9 ) || ((rubTeen >= 11) && (rubTeen <= 14))){ | ||
|
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. Можно упростить условие - |
||
| rubString = "рублей"; | ||
| } else if ((rub == 2) || (rub == 4) || (rub == 3) ){ | ||
|
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. Можно упростить - |
||
| rubString = "рубля"; | ||
| } else if (rub == 1) { | ||
| rubString = "рубль"; | ||
| } | ||
|
|
||
| String message = "Каждый должен заплатить %.2f %s"; | ||
| System.out.println(String.format(message, pay, rubString)); | ||
| } | ||
| } | ||
| public static class Calculate{ | ||
| String receipt = ""; | ||
| double pay = 0; | ||
|
|
||
| public void getAddList (String name, double price) { | ||
|
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. Этот и |
||
| receipt = receipt + String.format("%s %.2f \n", | ||
| name, | ||
| price); | ||
| System.out.println("Товар успешно добавлен"); | ||
| } | ||
|
|
||
| public void getListPrice(double price){ | ||
| pay = pay + price; | ||
| } | ||
| } | ||
|
|
||
| public static class Product { | ||
|
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. Классы лучше в отдельный файл располагать, и тогда не надо будет делать их статическими |
||
| String name; | ||
| double price; | ||
| } | ||
|
|
||
| public static boolean isNumeric(String string) { | ||
| double value; | ||
|
|
||
| if(string == null || string.equals("")) { | ||
| return false; | ||
| } | ||
| try { | ||
| value = Double.parseDouble(string); | ||
| return true; | ||
| } catch (NumberFormatException e) { | ||
|
|
||
| } | ||
| return false; | ||
| } | ||
| } | ||