Repository navigation
Add dev branch #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
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,50 @@ | ||
| import java.util.Scanner; | ||
| public class BillCalculator { | ||
| public void startBillCalculator() { | ||
| int people = enterNumberOfPeople(); | ||
| Calculator calculator = new Calculator(); | ||
| calculator.enterListOfItems(); | ||
| System.out.println("Добавленные товары:"); | ||
| calculator.printNamesItems(); | ||
| double amountPerPerson = calculator.calculate(people); | ||
| printAmountPerPerson(amountPerPerson); | ||
| } | ||
|
|
||
| private int enterNumberOfPeople() { | ||
| Scanner scanner = new Scanner(System.in); | ||
| while (true) { | ||
| System.out.println("Введите количество людей"); | ||
| if (scanner.hasNextInt()) { | ||
| int n = scanner.nextInt(); | ||
| if (n > 1) { | ||
| return n; | ||
| } | ||
| } | ||
| scanner.nextLine(); | ||
| System.out.println("Вы ввели некорректное количество людей"); | ||
| } | ||
| } | ||
|
|
||
| private void printAmountPerPerson(double amountPerPerson) { | ||
| String ruble; | ||
| int intAmountPerPerson = (int) amountPerPerson; | ||
| if (intAmountPerPerson % 100 >= 10 && intAmountPerPerson % 100 <= 20) { | ||
| ruble = "рублей"; | ||
| } else { | ||
| switch (intAmountPerPerson % 10) { | ||
| case 1: | ||
| ruble = "рубль"; | ||
| break; | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| ruble = "рубля"; | ||
| break; | ||
| default: | ||
| ruble = "рублей"; | ||
| } | ||
| } | ||
| System.out.printf("С каждого человека %.2f %s", amountPerPerson, ruble); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import java.util.Scanner; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
|
|
||
| public class Calculator { | ||
| private final List<Item> list = new ArrayList<>(); | ||
| public void enterListOfItems() { | ||
| Scanner scanner = new Scanner(System.in); | ||
| while (true) { | ||
| System.out.println("Введите название товара"); | ||
| String name = scanner.nextLine(); | ||
| if (name.equals("")) { | ||
| System.out.println("Вы не ввели название товара, попробуйте снова"); | ||
| continue; | ||
| } | ||
| System.out.println("Введите цену товара"); | ||
| double cost; | ||
| if (scanner.hasNextDouble()) { | ||
| cost = scanner.nextDouble(); | ||
| scanner.nextLine(); | ||
| if (cost < 0) { | ||
| System.out.println("Цена не может быть отрицательной, попробуйте снова"); | ||
| continue; | ||
| } | ||
| } else { | ||
| System.out.println("Вы некорректно ввели цену, попробуйте снова"); | ||
| scanner.nextLine(); | ||
| continue; | ||
| } | ||
| list.add(new Item(name, cost)); | ||
| System.out.println("Товар добавлен"); | ||
| System.out.println("Для завершения ввода товаров напишите \"Завершить\". Если хотите " + | ||
| "продолжить, нажмите Enter"); | ||
| String endPhrase = scanner.nextLine(); | ||
| if (endPhrase.equalsIgnoreCase("завершить")) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public double calculate(int people) { | ||
| if (list.isEmpty()) { | ||
| return 0; | ||
| } else { | ||
| double summa = 0; | ||
| for (Item item : list) { | ||
| summa += item.getCost(); | ||
| } | ||
| return summa / people; | ||
| } | ||
| } | ||
|
|
||
| public void printNamesItems() { | ||
| if (list.isEmpty()) { | ||
| System.out.println("Нет товаров"); | ||
| } else { | ||
| for (Item item : list) { | ||
| System.out.println(item.getName()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| public class Item { | ||
|
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. Можно более очевидно назвать, например, Product или Good |
||
| private final String name; | ||
| private final double cost; | ||
|
|
||
| public Item(String name, double cost) { | ||
| this.name = name; | ||
| this.cost = cost; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public double getCost() { | ||
| return cost; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,12 @@ | ||
| import java.util.Scanner; | ||
|
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. Этот импорт уже неактуален, его можно удалить. Чтобы студия сама оптимизировала импорт в файле, можно нажать Ctrl+Alt+O, или в студии Code - Optimize Imports |
||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
|
|
||
| System.out.println("Алоха Гаваи"); | ||
|
|
||
| new BillCalculator().startBillCalculator(); | ||
| } | ||
| } | ||
|
|
||
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.
Можно более очевидно назвать переменную, например, products