forked from Yandex-Practicum/Java-Module-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
калькулятор счета #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
MedvedevMeke
wants to merge
4
commits into
main
Choose a base branch
from
master
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
4 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
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,130 @@ | ||
|
|
||
| import java.util.Scanner; | ||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| int numberOfPeople = Input.inputCheckInt(); //проверка ввода | ||
| Calculator.calculate(numberOfPeople); //подсчет и вывод | ||
|
|
||
| } | ||
| } | ||
|
|
||
| class Input { | ||
|
|
||
| public static int inputCheckInt(){ | ||
| int result; | ||
| System.out.println("Привет. Укажите на какое количество человек поделить счет: "); | ||
| while(true) { | ||
| Scanner sc = new Scanner(System.in); | ||
| if(sc.hasNextInt()) { | ||
| result = sc.nextInt(); | ||
| if(result%10 > 1 && result%10 < 5){ | ||
| System.out.println("Сумма будет поделена на " + result +" человека."); | ||
| break; | ||
| } | ||
| else if (result%10 >= 5) { | ||
| System.out.println("Сумма будет поделена на " + result +" человек."); | ||
| break; | ||
| } | ||
| else{ | ||
| System.out.println("Количество человек должно быть больше 1, попробуйте снова: "); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| else{ | ||
| System.out.println("Пожалуйста, введите ЦЕЛОЕ число: "); | ||
| } | ||
| } | ||
| return result; | ||
| } //проверка на целое число для кол-ва людей | ||
| public static double inputCheckDouble(){ | ||
| double resultDouble; | ||
| while(true){ | ||
| Scanner priceStuff = new Scanner(System.in); | ||
| if(priceStuff.hasNextDouble()){ | ||
| resultDouble = priceStuff.nextDouble(); | ||
| if(resultDouble > 0) { | ||
| break; | ||
| } | ||
| else { | ||
| System.out.println("Пожалуйста, введите корректную сумму: "); | ||
| } | ||
| } | ||
| else{ | ||
| System.out.println("Пожалуйста, введите корректную сумму: "); | ||
| } | ||
| } | ||
| return resultDouble; | ||
| } //проверка на дробное для стоимости товара | ||
| public static boolean answerCheck(){ | ||
| String resultString; | ||
| while(true){ | ||
| Scanner anwserString = new Scanner(System.in); | ||
| resultString = anwserString.nextLine(); | ||
| if(resultString.equalsIgnoreCase("завершить")){ | ||
| return false; | ||
| } | ||
| else if(resultString.equalsIgnoreCase("да")){ | ||
| return true; | ||
| } | ||
| else{ | ||
| System.out.println("Пожалуйста, введите одну из доступных команд: "); | ||
| } | ||
| } | ||
|
|
||
| } //проверка на ввод доступных команд | ||
| } | ||
|
|
||
| class Stuff { | ||
|
|
||
| String label = ""; | ||
| double price = 0; | ||
| public static double addStuff(){ | ||
| Stuff npStuff = new Stuff(); | ||
| while(true){ | ||
| System.out.println("Укажите наименование товара: "); | ||
| Scanner nameStuff = new Scanner(System.in); | ||
| npStuff.label = npStuff.label + "\n" + nameStuff.nextLine(); | ||
| System.out.println("Укажите стоимость товара: "); | ||
| double e = Input.inputCheckDouble(); | ||
| npStuff.price = npStuff.price + e; | ||
| System.out.println("Товар добавлен. \nХотите добаить еще один товар? \nВведите 'Да', чтобы продолжить и 'Завершить', чтобы закончить"); | ||
| if(!Input.answerCheck()){ | ||
| break; | ||
| } | ||
| } | ||
| System.out.println("Список товаров: \n" + npStuff.label); | ||
|
|
||
| return npStuff.price; | ||
| } //добавить товар | ||
| } | ||
|
|
||
| class Calculator { | ||
|
|
||
| public static void calculate(int result){ | ||
| double finalPrice = Stuff.addStuff(); | ||
| String fullPrice = "\nОбщая стоимость всех товаров: %.2f"; | ||
| System.out.println(String.format(fullPrice, finalPrice) + roubles(finalPrice)); | ||
| finalPrice = finalPrice/result; | ||
| String message = "\nИтоговая сумма на 1 человека: %.2f"; | ||
| System.out.println(String.format(message, finalPrice) + roubles(finalPrice)); | ||
| } //подсчет и вывод результата | ||
|
|
||
| public static String roubles(double varPrice){ | ||
| String rubResult; | ||
| if(Math.floor(varPrice)%10 == 1 && Math.floor(varPrice) != 11){ | ||
| rubResult= "рубль"; | ||
| } | ||
| else if(Math.floor(varPrice)%10 >= 2 && Math.floor(varPrice)%10 <= 4 ) { | ||
| rubResult = " рубля"; | ||
| } | ||
| else{ | ||
| rubResult = " рублей"; | ||
|
|
||
| } | ||
| return rubResult; | ||
| } //рубль/ля/лей | ||
|
|
||
|
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. Не хватает фигурной скобки |
||
|
|
||
|
|
||
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.
Лучше использовать такие названия, чтобы было сразу понятно, для чего данный класс/метод/переменная нужны. Например, метод inputCheckInt() нужен для обработки ввода количества людей - лучше и назвать соответствующе, например, calculatePersons(). То же касается других названий - inputCheckDouble(), класса Input, Stuff и др.
Эта тема хорошо раскрывается во 2 главе книги Роберта Мартина "Чистый код", рекомендую к прочтению. Также на хабре есть краткий конспект книги: https://habr.com/ru/post/485118/