forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Pull Request. Приложение калькулятор #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
Open
MaxSatin
wants to merge
14
commits into
dev
Choose a base branch
from
devPull
base: dev
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
14 commits
Select commit
Hold shift + click to select a range
acd8a17
Тестируем
MaxSatin 043a426
Тестируем
MaxSatin ee58b72
Тестируем
MaxSatin 598444f
Тестируем
MaxSatin 954f49f
Тестируем
MaxSatin b56858d
Пробуем иначе
MaxSatin 694cbdd
Вариант1
MaxSatin bb86582
Вариант2
MaxSatin 998d315
Вариант3
MaxSatin 0cdee44
Вариант4
MaxSatin d50fb76
Итоговый вариант: "Калькулятор"
MaxSatin 8402143
"Калькулятор".
MaxSatin 8f33569
"Калькулятор Финал".
MaxSatin 2cf52f9
"Калькулятор Исправления после ревью.
MaxSatin 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 +1,3 @@ | ||
| # Пустой репозиторий для работы с Java кодом в Android Studio | ||
|
|
||
|
|
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,113 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Calculator { | ||
|
|
||
| private ArrayList<Products> productList = new ArrayList<>(); | ||
| private Scanner scanner = new Scanner(System.in); | ||
|
|
||
| private double sum; | ||
| private int peopleCount; | ||
| private String template; | ||
|
|
||
| public void calcStart() { | ||
| while (!scanner.hasNextInt()) { | ||
| System.out.println("Введите, пожалуйста число"); | ||
| scanner.next(); | ||
| } | ||
| peopleCount = scanner.nextInt(); | ||
| while (true) { | ||
| if (peopleCount == 1) { | ||
| System.out.println("Все оплатить придется самому"); | ||
| break; | ||
| } | ||
| if (peopleCount > 1) { | ||
| prodCount(); | ||
| break; | ||
| } | ||
| else { | ||
| System.out.println("Вы ввели неправильное значение. Попробуйте снова!"); | ||
| while (!scanner.hasNextInt()) { | ||
| System.out.println("Введите, пожалуйста число"); | ||
| scanner.next(); | ||
| } | ||
| peopleCount = scanner.nextInt(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void prodCount() { | ||
| while (true) { | ||
|
|
||
| System.out.println("Уточните название продукта: "); | ||
| String name = scanner.next(); | ||
|
|
||
| System.out.println("Уточните стоимость продукта: "); | ||
| while (!scanner.hasNextDouble()) { | ||
| System.out.println("Введите, пожалуйста, число формате 1,45"); | ||
| scanner.next(); | ||
| } | ||
| double price = scanner.nextDouble(); | ||
| while (price < 0) { | ||
| System.out.println("Введите положительное число!"); | ||
| while (!scanner.hasNextDouble()) { | ||
| System.out.println("Введите, пожалуйста, число формате 1,45"); | ||
| scanner.next(); | ||
| } | ||
| price = scanner.nextDouble(); | ||
| } | ||
| Products productNew = new Products(name, price); | ||
| productList.add(productNew); | ||
| sum += productNew.price; | ||
|
|
||
| template = "Продукт %s стоимостью %.2f %s добавлен в список." + | ||
| "\nОбщая сумма составляет: %.2f %s"; | ||
| System.out.println(String.format(template, productNew.name, productNew.price, | ||
| formatter(productNew.price), sum, formatter(sum))); | ||
|
|
||
| System.out.println("\nЧтобы ввести новый товар введите любой символ.\n" + | ||
| "Чтобы закончить, введите 'Завершить'"); | ||
|
|
||
| if (scanner.next().equalsIgnoreCase("Завершить")) { | ||
| finalSum(); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void finalSum(){ | ||
|
|
||
| System.out.println("\nДобавленные товары: "); | ||
| for (Products product : productList) { | ||
| template = "%s %.2f %s"; | ||
| System.out.println(String.format(template, product.name, | ||
| product.price, formatter(product.price))); | ||
| } | ||
| template = "Итоговая сумма: %.2f %s"; | ||
| System.out.println(String.format(template, sum, formatter(sum))); | ||
|
|
||
| int name = peopleCount; | ||
| double finalSum = sum / peopleCount; | ||
| template = "Каждый должен оплатить %.2f %s"; | ||
| System.out.println(String.format(template, finalSum, formatter(finalSum))); | ||
| } | ||
|
|
||
| public String formatter (double number) { | ||
|
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. В этой функции дополнительно нужно учесть, что для чисел, которые оканчиваются на 11-19 включительно, необходимо выводить слово "рублей" |
||
| if ((number % 100) > 10 && (number % 100) < 20) | ||
| return "рублей"; | ||
|
|
||
| else if ((number % 10) >= 5 && (number % 10) < 10) | ||
| return "рублей"; | ||
|
|
||
| else if ((number % 10) >= 1 && (number % 10) < 2) | ||
| return "рубль"; | ||
|
|
||
| else if ((number % 10) > 1 && (number % 10) < 5) | ||
| return "рубля"; | ||
|
|
||
| else if ((number % 10) >= 0 && (number % 10) < 1) | ||
| return "рублей"; | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
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,6 +1,10 @@ | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
|
|
||
| Calculator newCalc = new Calculator(); | ||
| System.out.println("Добрый день! На сколько человек будем делить счет?"); | ||
|
|
||
| newCalc.calcStart(); | ||
| } | ||
| } | ||
| } |
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,9 @@ | ||
| public class Products { | ||
| final String name; | ||
| final double price; | ||
|
|
||
| public Products(String name, double price) { | ||
| this.name = name; | ||
| this.price = price; | ||
| } | ||
| } |
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.
templateлучше не писать, а сразу всё в одном вызове писать, иначе потом запутаешься с этой переменной сам