Repository navigation
Калькулятор консольное приложение #1 #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,61 @@ | ||
| import java.util.*; | ||
|
|
||
|
|
||
|
|
||
| public class Calculator { | ||
| public static final Scanner scanner = new Scanner(System.in); | ||
| String productNames; | ||
| double sumMoney; | ||
| Calculator(String productNames, double sumMoney){ | ||
|
ralvveo marked this conversation as resolved.
|
||
| this.productNames = productNames; | ||
| this.sumMoney = sumMoney; | ||
| } | ||
| void addProduct(){ | ||
| System.out.println("Введите название нового товара "); | ||
| String nameOfProduct = scanner.next(); | ||
|
|
||
|
|
||
|
Comment on lines
+16
to
+17
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. 🍏 Внутри методов лучше не оставлять пустые строки |
||
| System.out.println("Введите цену товара"); | ||
| double priceOfProduct = checkInputPrice(); | ||
|
|
||
|
|
||
|
|
||
| System.out.println("\nТовар успешно добавлен! \n"); | ||
| Product prod = new Product(nameOfProduct, priceOfProduct); | ||
|
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. 🍏Это поле, как и класс |
||
| productNames += nameOfProduct + "\n"; | ||
| sumMoney += priceOfProduct; | ||
|
|
||
| } | ||
|
|
||
| //Проверка ввода корректной цены | ||
| public static double checkInputPrice(){ | ||
|
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. 🍏 |
||
| double userInputDouble; | ||
| while (true) { | ||
|
|
||
| String userInput = scanner.next(); | ||
| if (isDouble(userInput)) { | ||
| userInputDouble = Double.parseDouble(userInput); | ||
|
Comment on lines
+36
to
+37
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. 🍏Ты 2 раза парсишь |
||
| if (userInputDouble > 0) | ||
| break; | ||
| else | ||
| System.out.println("Введите корректную цену товара больше нуля!"); | ||
| } else | ||
| System.out.println("Введите корректную цену товара!"); | ||
|
|
||
| } | ||
| return userInputDouble; | ||
| } | ||
|
|
||
|
|
||
|
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. 🍏Необходимо оставлять только одну строку между методами |
||
| //Функция проверки на ввод дробного числа | ||
| public static boolean isDouble(String x) throws NumberFormatException | ||
|
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. 🍏 |
||
| { | ||
| try { | ||
| Double.parseDouble(x); | ||
| return true; | ||
| } catch(Exception e) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,88 @@ | ||
| import java.util.*; | ||
|
|
||
| public class Main { | ||
| public static final Scanner scanner = new Scanner(System.in); | ||
|
|
||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
|
|
||
| //Запросили число гостей | ||
| System.out.println("Сколько у нас гостей?"); | ||
| int peopleNumber = checkInputGuests(); | ||
|
|
||
|
|
||
| Product prod = new Product("", 0); | ||
| Calculator calc = new Calculator("", 0); | ||
|
|
||
| //Бесконечный ввод товаров до завершения | ||
| String askToContinue; | ||
|
|
||
| while (true){ | ||
| calc.addProduct(); | ||
| System.out.println("Хотите продолжить ввод товаров?\nДля продолжения введите любой символ\nДля завершения введите \"Завершить\" в любом регистре\n"); | ||
| askToContinue = scanner.next(); | ||
| if (askToContinue.equalsIgnoreCase("Завершить")) | ||
| break; | ||
| } | ||
|
|
||
| //Подсчёт и вывод результатов | ||
| System.out.println("Добавленные товары:\n" + calc.productNames); | ||
|
|
||
| double oneGuestSum = calc.sumMoney / peopleNumber; | ||
| int OGS = (int) Math.floor(oneGuestSum); | ||
| String letter = checkCorrectLetter(OGS); | ||
| System.out.println(String.format("Каждый должен заплатить: %.2f рубл%s", oneGuestSum, letter)); | ||
|
|
||
|
|
||
| } | ||
|
|
||
| //Поиск правильного окончания слова рубль | ||
| public static String checkCorrectLetter(int OGS){ | ||
| int temp = OGS % 100; | ||
| if (temp % 10 == 1 && temp != 11) | ||
|
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 "ь"; | ||
| else if ((temp % 10 == 2 || temp % 10 == 3 || temp % 10 == 4) && (temp != 12 && temp != 13 && temp!= 14)) | ||
|
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 "я"; | ||
| else | ||
| return "ей"; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| //Проверка ввода корректного числа гостей | ||
| public static int checkInputGuests(){ | ||
| int userInputInt; | ||
| while (true) { | ||
|
|
||
| String userInput = scanner.next(); | ||
| if (isInt(userInput)) { | ||
|
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. 🍏Тоже самое замечание, что и с |
||
| userInputInt = Integer.parseInt(userInput); | ||
| if (userInputInt> 1) | ||
| break; | ||
| else | ||
| System.out.println("Введите корректное число гостей больше 1!"); | ||
| } else | ||
| System.out.println("Введите корректное число гостей!"); | ||
|
|
||
| } | ||
| return userInputInt; | ||
| } | ||
|
|
||
| //Функция проверки на ввод целого числа | ||
| public static boolean isInt(String x) throws NumberFormatException | ||
| { | ||
| try { | ||
| Integer.parseInt(x); | ||
| return true; | ||
| } catch(Exception e) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
Comment on lines
+81
to
+86
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. 🍏Много лишних строк |
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| //Класс товаров | ||
| public class Product { | ||
| String name; | ||
| double price; | ||
| Product (String name, double price){ | ||
| this.name = name; | ||
| this.price = price; | ||
| } | ||
| } |
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.
🍏
scannerне используется за пределами класса, значит можно убратьstaticи поменять модификатор наprivate