-
Notifications
You must be signed in to change notification settings - Fork 222
Pull request к приложению «Калькулятор счёта» #102
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,21 @@ | ||
| public class Calculator { | ||
|
|
||
|
|
||
| public void cheque(int number, double totalPrice){ | ||
| //Вычисляем, сколько должен заплать каждый человек | ||
| double result = (float) totalPrice / (float) number; | ||
| int money = (int) Math.floor(result); | ||
|
|
||
| String str = String.format("%.2f", result); | ||
| //Вывод суммы, которую должен заплатить каждый поровну | ||
| if ((money % 100) / 10 == 1){ | ||
| System.out.println("Сумма, которую должен заплатить каждый человек составляет: " + str + " рублей"); | ||
| }else if (money % 100 == 1){ | ||
| System.out.println("Сумма, которую должен заплатить каждый человек составляет: " + str + " рубль"); | ||
| }else if (money % 10 >= 2 && money % 10 <= 4){ | ||
|
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. Выражение money % 10 считается несколько раз, можно посчитать его один раз, записать в переменную и использовать ее |
||
| System.out.println("Сумма, которую должен заплатить каждый человек составляет: " + str + " рубля"); | ||
| }else { | ||
| System.out.println("Сумма, которую должен заплатить каждый человек составляет: " + str + " рублей"); | ||
|
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("Сумма, которую должен заплатить каждый человек составляет: " + str + " рублей"); повторяется в каждой ветке, кроме слова рубль, лучше код вынести после if-else, оставив в нем только определение слова |
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,21 @@ | ||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
|
|
||
| People people = new People(); | ||
| //Ввод количества человек, на которых требуется разделить счет | ||
| System.out.println("На скольких человек необходимо разделить счёт?"); | ||
| people.numberOfPeople(); | ||
|
|
||
| //Добавление продуктов | ||
| Product product = new Product(); | ||
| product.addProduct(); | ||
|
|
||
| //Вывод суммы, которую должен заплатить каждый человек | ||
| Calculator calculator = new Calculator(); | ||
| calculator.cheque(people.number, product.totalPrice); | ||
|
|
||
|
|
||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import java.util.Scanner; | ||
| public class People { | ||
| int number = 0; | ||
|
|
||
| // Добавление количества человек, на которых необходимо разделить всю сумму | ||
| public void numberOfPeople(){ | ||
|
|
||
| while (true){ | ||
| Scanner scanner = new Scanner(System.in); | ||
| if (scanner.hasNextInt()){ | ||
| int count = scanner.nextInt(); | ||
| if (count > 1){ | ||
| number = count; | ||
| break; | ||
| }else if (count == 1){ | ||
| System.out.println("Количество человек, введенное вами, равно 1. Пожалуйста, введите значение больше 1."); | ||
| }else{ | ||
| System.out.println("Вы ввели некорректное значение. Пожалуйста, введите значение больше 1."); | ||
| } | ||
|
|
||
| }else{ | ||
| System.out.println("Вы ввели некорректное значение. Пожалуйста, введите значение больше 1."); | ||
|
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. Можно текст "Пожалуйста, введите значение больше 1." записать в константу, он повторяется 3 раза |
||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Product { | ||
|
|
||
| String productList = ""; | ||
| String spacing = "\n"; | ||
| String str = "Добавленные товары: "; | ||
| double totalPrice; | ||
|
|
||
| //Добавление товаров и их стоимости | ||
| public void addProduct(){ | ||
| Scanner scanner = new Scanner(System.in); | ||
| System.out.println("Введите название товара"); | ||
| String name = scanner.nextLine(); | ||
| productList += str + name + spacing; | ||
| System.out.println("Введите стоимость товара"); | ||
| while (true){ | ||
| if (scanner.hasNextDouble()){ | ||
| double price = scanner.nextDouble(); | ||
| totalPrice+=price; | ||
| System.out.println("Товар успешно добавлен"); | ||
| anotherProduct(); | ||
| break; | ||
| }else { | ||
| System.out.println("Вы ввели некорректное значение. Пожалуйста, введите корректное значение стоимости"); | ||
| scanner.nextLine(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| //Спрашиваем у пользователя - хочет ли он добавить еще один товар? | ||
| public void anotherProduct(){ | ||
| Scanner scanner = new Scanner(System.in); | ||
| System.out.println("Хотите добавить ещё один товар?"); | ||
| String fork = scanner.nextLine(); | ||
|
|
||
| /*Если пользователь вводит "Да", то повторно вызываем метод addProduct | ||
| Иначе завершаем добавление продуктов и выводим список добавленных продуктов*/ | ||
| if (fork.equalsIgnoreCase("Да")){ | ||
| addProduct(); | ||
|
|
||
| }else { | ||
| addedProducts(productList); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| //Вывод добавленных продуктов | ||
| public void addedProducts (String lists){ | ||
|
|
||
| System.out.println(lists); | ||
| } | ||
|
|
||
| } |
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.
Здесь лучше прописать money % 10 == 1, чтобы попадало 21, 51 и т.д.