Repository navigation
Приложение готово! #61
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: master
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,15 @@ | ||
| public class Calculator { | ||
| double _amount; | ||
| String _listOfProducts = "Добавленные товаровы:"; | ||
| private int _countProd = 0; | ||
| public void addProduct(String nameProd) | ||
| { | ||
| _listOfProducts = _listOfProducts + ("\n" + String.format("%d) ", _countProd) + nameProd); | ||
| } | ||
|
|
||
| public void riseAmount(double priceProd) | ||
| { | ||
| _amount += priceProd; | ||
| _countProd++; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,108 @@ | ||
| import java.util.InputMismatchException; | ||
| import java.util.Scanner; | ||
| public class Main { | ||
|
|
||
| private static String nounForm(double num) | ||
| { | ||
| String numStr = Double.toString(num); | ||
| numStr = numStr.substring(0, numStr.indexOf(".")); | ||
| if (num > 10) { | ||
| for (int i = 11; i < 15; i++) { | ||
| if (i == Integer.parseInt(numStr.substring(numStr.length() - 2))) { | ||
| return "рублей"; | ||
|
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 до 14 с циклом работает, но, кажется, слишком усложнено)
Author
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-14? |
||
| } | ||
| } | ||
| } | ||
| switch (numStr.charAt(numStr.length() - 1)) | ||
| { | ||
| case '0': | ||
| case '5': | ||
| case '6': | ||
| case '7': | ||
| case '8': | ||
| case '9': | ||
| return "рублей"; | ||
| case '1': | ||
| return "рубль"; | ||
| case '2': | ||
| case '3': | ||
| case '4': | ||
| return "рубля"; | ||
| default: | ||
| return "ERROR"; | ||
| } | ||
| } | ||
|
|
||
| private static int askNumOfPeople() | ||
| { | ||
| Scanner sc = new Scanner(System.in); | ||
| int numOfPeople = 0; | ||
| System.out.println("На какое количство людей поделить чек?"); | ||
| while (numOfPeople <= 1) { | ||
| while (true) { | ||
| try { | ||
| numOfPeople = sc.nextInt(); | ||
| break; | ||
| } catch (Exception e) { | ||
| System.err.println("Невозможное значение. Введите натуральное число:"); | ||
| sc.nextLine(); | ||
| } | ||
|
|
||
| } | ||
| if (numOfPeople < 1) { | ||
| System.out.println("Значение меньше единицы. Введите натуральное число:"); | ||
| } else if (numOfPeople == 1) { | ||
| System.out.println("Не имеет смысла считать"); | ||
| } | ||
| } | ||
| return numOfPeople; | ||
|
|
||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
|
|
||
| Calculator calc = new Calculator(); | ||
|
|
||
| Scanner sc = new Scanner(System.in); | ||
|
|
||
| int numOfPeople = askNumOfPeople(); | ||
| System.out.println("Теперь давайте выберем товары!"); | ||
|
|
||
| while(true) | ||
| { | ||
| String prodName = null; | ||
|
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. Если мы создаем переменную и не задаем ей изначальное значение, то она и так будет равна null, т.е. задавать ей null излишне |
||
| System.out.println("Введите название товара или 'завершить':"); | ||
| prodName = sc.nextLine(); | ||
| if (prodName.equalsIgnoreCase("завершить")) | ||
| { | ||
| System.out.println("Все товары добавлены!\n"); | ||
| break; | ||
| } | ||
| System.out.println("Введите стоимость товара:"); | ||
| double price = 0; | ||
| while(true) | ||
| { | ||
|
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{ | ||
| price = sc.nextDouble(); | ||
| if (price > 0){ | ||
| break; | ||
| } | ||
| else{ | ||
| System.err.println("Невозможное значение. Введите значение больше нуля:"); | ||
| } | ||
| } catch (Exception e){ | ||
| System.err.println("Невозможное значение. Введите коректное значение (дробные значения пишите с запятой):"); | ||
| sc.nextLine(); | ||
| } | ||
|
|
||
| } | ||
| sc.nextLine(); | ||
| calc.riseAmount(price); | ||
| calc.addProduct(prodName); | ||
| System.out.println("Товар добавлен!\n"); | ||
| } | ||
|
|
||
| System.out.println(calc._listOfProducts); | ||
| System.out.println(String.format("\nС каждого человека по %.2f %s", calc._amount / numOfPeople, nounForm(calc._amount / numOfPeople))); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| public class numOfPeople { | ||
|
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. Неиспользуемые классы лучше не хранить |
||
| } | ||
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.
Лучше называть методы глаголами. Например, getNounForm. О правилах нейминга можно почитать здесь:
https://github.com/qcha/JBook/blob/master/start/code_style.md
Также эта тема хорошо раскрывается во 2 главе книги Роберта Мартина "Чистый код".