-
Notifications
You must be signed in to change notification settings - Fork 218
Выполненное задание. Попытка 1. #52
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
Open
NamerPRO
wants to merge
3
commits into
Yandex-Practicum:master
Choose a base branch
from
NamerPRO:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,74 @@ | ||
| import java.lang.NumberFormatException; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| private static final Scanner scanner = new Scanner(System.in); | ||
|
|
||
| private static String getRoubleWordWithCorrectEnding(int number) { | ||
| if (number % 100 > 9 && number % 100 < 20) { | ||
| return "рублей"; | ||
| } | ||
| switch (number % 10) { | ||
| case 1: | ||
| return "рубль"; | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| return "рубля"; | ||
| default: | ||
| return "рублей"; | ||
| } | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
| System.out.println("На скольких человек необходимо разделить счет?"); | ||
|
|
||
| String amountOfUsersAsString; | ||
| int amountOfUsers; | ||
|
|
||
| while (true) { | ||
| amountOfUsersAsString = scanner.nextLine(); | ||
| try { | ||
| amountOfUsers = Integer.parseInt(amountOfUsersAsString); | ||
| if (amountOfUsers > 1) { | ||
| break; | ||
| } | ||
| System.out.println("Должно быть как минимум два человека, чтобы разделить счет!\nПовторите ввод еще раз!"); | ||
| } catch (NumberFormatException exception) { | ||
| System.out.println("Введен недопустимый параметр в качестве количества человек! Ожидалось целое число.\nПовторите ввод еще раз!"); | ||
| } | ||
| } | ||
|
|
||
| ProductCalculator calculator = new ProductCalculator(); | ||
|
|
||
| double productPrice; | ||
| String productPriceAsString; | ||
| String productName; | ||
|
|
||
| System.out.println("Введите товар и его стоимость через пробел:"); | ||
| while (!(productName = scanner.next()).equalsIgnoreCase("завершить")) { | ||
| productPriceAsString = scanner.nextLine(); | ||
| try { | ||
| productPrice = Double.parseDouble(productPriceAsString); | ||
| if (productPrice < 0) { | ||
| System.out.println("У товара не может быть отрицательная цена!\nТовар не добавлен. Повторите ввод."); | ||
| continue; | ||
| } | ||
| calculator.addProduct(productName, productPrice); | ||
| System.out.println("Товар был успешно добавлен! Хотите добавить еще?\nЕсли да, то введите название товара и цену, иначе напишите слово \"завершить\":"); | ||
| } catch (NumberFormatException exception) { | ||
| System.out.println("Не указана допустимая цена товара! Ожидалось вещественное число.\nТовар не был добавлен! Повторите корректный ввод заново."); | ||
| } | ||
| } | ||
|
|
||
| System.out.println("Добавленные товары:"); | ||
| for (Product product : calculator.getProductsList()) { | ||
| System.out.println(product.getProductName()); | ||
| } | ||
|
|
||
| double sumForEachToPay = calculator.getSumOfProducts() / amountOfUsers; | ||
| System.out.printf("Сумма, которую должен заплатить каждый: %.2f %s%n", sumForEachToPay, getRoubleWordWithCorrectEnding((int) sumForEachToPay)); | ||
|
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| public class Product { | ||
|
|
||
| String name; | ||
| double price; | ||
|
|
||
| public Product(String name, double price) { | ||
| this.name = name; | ||
| this.price = price; | ||
| } | ||
|
|
||
| public String getProductName() { | ||
| return name; | ||
| } | ||
|
|
||
| public double getProductPrice() { | ||
|
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. Метод не используется, а можно было бы выводить цену товара вместе с именем при завершении расчетов |
||
| return price; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import java.util.ArrayList; | ||
|
|
||
| public class ProductCalculator { | ||
|
|
||
| private ArrayList<Product> products = new ArrayList<>(); | ||
| private double totalSumOfProducts = 0; | ||
|
|
||
| public void addProduct(String name, double price) { | ||
| products.add(new Product(name, price)); | ||
| recalculateSum(price); | ||
| } | ||
|
|
||
| private void recalculateSum(double price) { | ||
| totalSumOfProducts += price; | ||
| } | ||
|
|
||
| public double getSumOfProducts() { | ||
| return totalSumOfProducts; | ||
| } | ||
|
|
||
| public ArrayList<Product> getProductsList() { | ||
| return products; | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Можно поделить функцию main на несколько - считывание количества человек, считывание товаров, вывод результата