Repository navigation
ДЗ на проверку #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
AleksandrRegulus
wants to merge
3
commits into
main
Choose a base branch
from
dev
base: main
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 |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class AdvancedScanner { | ||
| Scanner scan = new Scanner(System.in); | ||
|
|
||
| int requestInt () { | ||
| int n; | ||
| while (true) { | ||
| if (scan.hasNextInt()) { | ||
| n = scan.nextInt(); | ||
| scan.nextLine(); | ||
| break; | ||
| } else { | ||
| System.out.println("Некорректный ввод попробуйте еще раз"); | ||
| scan.nextLine(); | ||
| } | ||
| } | ||
| return n; | ||
| } | ||
|
|
||
| String requestString () { | ||
| String s; | ||
| while (true) { | ||
| if (scan.hasNextLine()) { | ||
| s = scan.nextLine(); | ||
| s = s.trim(); | ||
| if (s.isEmpty()) { | ||
| System.out.println("Вы ввели пустую строку, повторите ввод"); | ||
| } else { | ||
| break; | ||
| } | ||
| } else { | ||
| System.out.println("Некорректный ввод попробуйте еще раз"); | ||
| scan.nextLine(); | ||
| } | ||
| } | ||
| return s; | ||
| } | ||
|
|
||
| double requestDouble () { | ||
| double d; | ||
| while (true) { | ||
| if (scan.hasNextDouble()) { | ||
| d = scan.nextDouble(); | ||
| scan.nextLine(); | ||
| break; | ||
| } else { | ||
| System.out.println("Некорректный ввод поробуйте еще раз"); | ||
| scan.nextLine(); | ||
| } | ||
| } | ||
| return d; | ||
| } | ||
| } | ||
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,16 @@ | ||
| public class Calc { | ||
| String productName; | ||
| double productsSum, sumPerPerson; | ||
| int countOfPersons; | ||
|
|
||
| void add(String pName, double pSum) { | ||
| productName = productName + pName + "\n"; | ||
| productsSum = productsSum + pSum; | ||
| sumPerPerson = productsSum / countOfPersons; | ||
| } | ||
|
|
||
| Calc (String pName, int cPer) { | ||
| productName = pName; | ||
| countOfPersons = cPer; | ||
| } | ||
| } |
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,83 @@ | ||
| // dev branch for Y.Practicum | ||
|
|
||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
| AdvancedScanner as = new AdvancedScanner(); | ||
|
|
||
| System.out.println("Введите количество человек, на которых необходимо разделить счет:"); | ||
| while (true) { | ||
|
|
||
| int qPeople = as.requestInt(); | ||
| if (qPeople == 1) { | ||
| System.out.println("Нет смысла делить счет на одного. Повторите ввод:"); | ||
| } else if (qPeople < 1) { | ||
| System.out.println("Введено некорректное число человек. Повторите ввод:"); | ||
| } else { | ||
| System.out.println("Делим счет на " + qPeople+". Запускаю калькулятор"); | ||
|
|
||
| //создаем калькулятор с пустыми начальными значениями | ||
| Calc c1 = new Calc("Добавленные товары:\n", qPeople); | ||
|
|
||
| // запускаем бесконечный цикл запроса данных о товаре | ||
| int i = 1; // номер товара в списке | ||
| while (true) { | ||
| // запрос наименования товара | ||
| System.out.println("Введите наименование товара:"); | ||
| String product = as.requestString(); | ||
|
|
||
| // теперь запрос цены товара | ||
| System.out.println("Введите стоимость товара \""+product+"\" в формате \"рубли.копейки\":"); | ||
| double cost = as.requestDouble(); | ||
| while (true) { // проверка ввода цены | ||
| if (cost < 0.1) { | ||
| System.out.println("Стоимость товара не может быть меньше 1 копейки. Повторите ввод цены:"); | ||
| cost = as.requestDouble(); | ||
| } else { break; } | ||
| } | ||
|
|
||
| c1.add(i + ". " + product, cost); // добавляем товар в калькулятор | ||
| i++; | ||
|
|
||
| // предлагаем ввести еще товар или завершить ввод и расчитать | ||
| System.out.println("Товар успешно добавлен в калькулятор. \nХотите добавить еще один товар? Если нет напишите \"Завершить\""); | ||
| String userAnswer = as.requestString(); | ||
| if (userAnswer.equalsIgnoreCase("Завершить")) { | ||
| break; | ||
| } // иначе начинаем новую итерацию | ||
| } | ||
|
|
||
| System.out.println("\n" + c1.productName + | ||
| "Всего на " + String.format("%.2f", c1.productsSum) + " " + getPadejOfRubl(c1.productsSum)); | ||
|
|
||
| System.out.println("Каждый из " + qPeople + " человек должен заплатить по: " + | ||
| String.format("%.2f", c1.sumPerPerson)+ " " + getPadejOfRubl(c1.sumPerPerson)); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static String getPadejOfRubl (double sum) { | ||
|
|
||
| int i = (int) Math.floor(sum % 100); // получаем последний символ целой части суммы как число | ||
| if (i >= 11 && i <= 19) { // выбираем правильный падеж Рубля | ||
| return "рублей"; | ||
| } else { | ||
| i = (int)Math.floor(sum % 10); | ||
| switch (i) { | ||
| case 1: | ||
| return "рубль"; | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| return "рубля"; | ||
| default: | ||
| return "рублей"; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
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.
Логика этого класса выглядит, как эффективное и универсальное решение, для обработки введенных значений в консоль. Круто 👍
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.
Спасибо :)