-
Notifications
You must be signed in to change notification settings - Fork 220
Проектная работа No1 - Николаев #124
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
08a8e55
4727ba8
f9d820e
abb1396
b527950
bc44e54
1c0acfd
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,18 @@ | ||
| public class Ending { | ||
|
|
||
| String end(float num) { | ||
|
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. В Java, да и не только, принято называть методы глаголами. |
||
| int num100 = (int) (Math.floor(num % 100)); | ||
| if (num100 > 4 && num100 < 21) { | ||
| return "Рублей"; | ||
| } else { | ||
| int num10 = num100 % 10; | ||
| if (num10 == 1) { | ||
| return "Рубль"; | ||
| } else if (num10 > 1 && num10 < 5) { | ||
| return "Рубля"; | ||
| } else { | ||
| return "Рублей"; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,58 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
|
|
||
| byte friends; | ||
| String order; | ||
| String table = ""; | ||
| float price; | ||
| float total = 0.0f; | ||
| String exit; | ||
|
|
||
| while (true) { | ||
| System.out.println("Введите число участников:"); | ||
| Scanner who = new Scanner(System.in); | ||
| if (who.hasNextByte()) { | ||
| friends = who.nextByte(); | ||
| if (friends <= 1) { | ||
| System.out.println("Нет смысла делить счёт. Попробуйте ещё раз."); | ||
| } else { | ||
| break; | ||
| } | ||
| } else { | ||
| 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(true), который отвечает за ввод количества гостей/ввод продуктов, в отдельные методы inputGuests, inputProducts. |
||
|
|
||
| while (true) { | ||
| System.out.println("Введите название блюда:"); | ||
| Scanner input = new Scanner(System.in); | ||
| order = input.nextLine(); | ||
| table += order + "\n"; | ||
| System.out.println("Стоимость в формате 'руб,коп':"); | ||
| if (input.hasNextFloat()) { | ||
| price = input.nextFloat(); | ||
| if (price > 0) { | ||
| total += price; | ||
| System.out.println("Товар успешно добавлен!\nПродолжить? Да/Завершить"); | ||
| exit = input.next(); | ||
| if (exit.equalsIgnoreCase("Завершить")) { | ||
| break; | ||
| } | ||
| } else { | ||
| System.out.println("Отрицательное значение."); | ||
| } | ||
| } else { | ||
| System.out.println("Неверно указана сумма, попробуйте снова."); | ||
| } | ||
| } | ||
|
|
||
| Ending name = new Ending(); | ||
|
|
||
| System.out.println("Добавленные товары:\n" + table); | ||
| System.out.println("Общая стоимость блюд: " + String.format("%.2f", total) + " " + name.end(total)); | ||
| System.out.println("Сумма к оплате каждым участником: " + String.format("%.2f", total/friends) + " " + name.end(total/friends)); | ||
| } | ||
| } | ||
| } | ||