-
Notifications
You must be signed in to change notification settings - Fork 0
Проектная работа №1, Комаров #2
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: base
Are you sure you want to change the base?
Changes from all commits
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.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,121 @@ | ||
| import java.util.Scanner; // импорт сканера | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
|
|
||
| Scanner scanner = new Scanner(System.in); | ||
| Calculator calc = new Calculator(); | ||
| Product product = new Product(); | ||
|
|
||
| System.out.println("На сколько человек нужно разделить счет?"); | ||
| int payingGuests = 0;// создаем переменную - число гостей | ||
| boolean isGuestsNumberCorrect = false; | ||
| while(!isGuestsNumberCorrect) { | ||
| if(scanner.hasNextInt()) { | ||
| payingGuests = scanner.nextInt(); | ||
| if(payingGuests > 1) { | ||
| isGuestsNumberCorrect = true; | ||
| scanner.nextLine(); | ||
| } else { | ||
| System.out.println("Я не могу разделить на такое число гостей. Введите значение больше 1"); | ||
| scanner.nextLine(); | ||
| } | ||
| } else { | ||
| System.out.println("Вы ввели некорректное значение. Введите числовое значение больше 1"); | ||
| scanner.nextLine(); | ||
| } | ||
| } | ||
| System.out.println("Ввод корректен. Число гостей установлено: их ровно " + payingGuests); // тут мы разобрались с гостями, обработав некорректный ввод | ||
|
|
||
| while(true) { | ||
| System.out.println("Введите название товара:"); | ||
| product.productName = scanner.nextLine(); | ||
| System.out.println("Введите стоимость товара в формате \"рубли,копейки\""); | ||
|
|
||
| while(true) { | ||
| if (scanner.hasNextDouble()) { | ||
| product.productPrice = scanner.nextDouble(); | ||
| if(product.productPrice > 0) { | ||
| scanner.nextLine(); | ||
| break; | ||
| } | ||
| System.out.println("Стоимость товара должна быть больше 0. Введите корректное значение"); | ||
| scanner.nextLine(); | ||
| } else { | ||
| System.out.println("Вы ввели некорректное значение. Введите числовое значение больше нуля"); | ||
| scanner.nextLine(); | ||
| } | ||
|
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, отвечающий за обработку ввода товаров, в отдельный метод - это позволяет делать код более читабельным и аккуратным. То же касается других логических блоков - обработка ввода количества людей, конечный вывод. |
||
| } | ||
| calc.add(product); | ||
| System.out.println("Добавить еще один товар? Введите \"Завершить\" чтобы узнать итоговую стоимость,\nили любой другой символ, чтобы добавить еще товары"); | ||
| String userChoice = scanner.nextLine(); | ||
| String enough = "Завершить"; | ||
|
|
||
| if(userChoice.equalsIgnoreCase(enough)) { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| double guestTotal = calc.total/payingGuests; | ||
| String rubleTemplate ="%.2f"; | ||
|
|
||
| System.out.println("Ваш список покупок:\n" + calc.shoppingList); | ||
| calc.findEnding(calc.total); | ||
| System.out.println("Итоговая стоимость покупок составила: " + String.format(rubleTemplate, calc.total) + " " + calc.ending); | ||
| calc.findEnding(guestTotal); | ||
| System.out.println("Каждый гость должен заплатить " + String.format(rubleTemplate, guestTotal) + " " + calc.ending); | ||
| } | ||
| } | ||
|
|
||
| class Calculator { | ||
| double total = 0; | ||
| String shoppingList = new String(); | ||
| String ending = new String(); | ||
|
|
||
| public void add(Product product) { | ||
| total += product.productPrice; | ||
| shoppingList += product.productName + " " + product.productPrice + "\n"; | ||
|
|
||
| } | ||
|
|
||
| public void findEnding(double endingPrice) { | ||
|
|
||
| int intPrice = (int) Math.floor(endingPrice); | ||
|
|
||
| if(10 < intPrice % 100 && intPrice % 100 <= 19) { | ||
| ending = "рублей"; | ||
| } else { | ||
| switch(intPrice % 10) { | ||
|
|
||
| case 1: | ||
| ending = "рубль"; | ||
| break; | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| ending = "рубля"; | ||
| break; | ||
| default: | ||
| ending = "рублей"; | ||
| } | ||
|
|
||
|
|
||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| class Product { | ||
| String productName; | ||
| double productPrice; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
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.nextLine();, почему бы не вынести его наружу всех условий?