-
Notifications
You must be signed in to change notification settings - Fork 218
Проектная работа №1. Калькулятора счёта. #40
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,18 @@ | ||
| public class Calculator { | ||
| int countPeople; | ||
| double currentTotalAmount = 0; | ||
| String products = ""; | ||
|
|
||
| Calculator(int countPeople) { | ||
| this.countPeople = countPeople; | ||
| } | ||
|
|
||
| void addProductCost(Product product) { | ||
| products += product.name + "\r\n"; | ||
| currentTotalAmount += product.cost; | ||
| } | ||
|
|
||
| double getEachFriendAmount() { | ||
| return currentTotalAmount / countPeople; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,94 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
| public static Calculator initializeCalculator(Scanner inputScanner) { | ||
| int countFriends = -1; | ||
| // Получение количества друзей. | ||
| while (countFriends <= 1) { | ||
| System.out.println("На скольких человек необходимо разделить счёт?"); | ||
|
|
||
| if (inputScanner.hasNextInt()) { | ||
| countFriends = inputScanner.nextInt(); | ||
|
|
||
| } else { | ||
| System.out.println("[Ошибка] Необходимо ввести целочисленное количество друзей."); | ||
| inputScanner.next(); | ||
| continue; | ||
|
|
||
| } | ||
| if (countFriends == 1) { | ||
| System.out.println("[Ошибка] Нет смысла ничего считать и делить."); | ||
|
|
||
| } else if (countFriends < 1) { | ||
| System.out.println("[Ошибка] " + countFriends + " - это некорректное значение для подсчёта."); | ||
|
|
||
| } | ||
| } | ||
|
|
||
| // Инициализация калькулятора в зависимости от количества друзей. | ||
| return new Calculator(countFriends); | ||
| } | ||
|
|
||
| // Добавление товаров в калькулятор. | ||
| public static void addProductsToCalculator(Scanner inputScanner, Calculator calculator) { | ||
| while (true) { | ||
| System.out.println("Введите название товара:"); | ||
| String product = inputScanner.next(); | ||
|
|
||
| System.out.println("Введите цену товара в формате рубли.копейки:"); | ||
| while (true) { | ||
| if (inputScanner.hasNextDouble()) { | ||
| double productCost = inputScanner.nextDouble(); | ||
| if (productCost < 0) { | ||
| System.out.println("[Ошибка] Введите корректную цену товара: цена не может быть отрицательной."); | ||
| } else { | ||
| calculator.addProductCost(new Product(product, productCost)); | ||
| System.out.println("Товар успешно добавлен в корзину."); | ||
| break; | ||
| } | ||
| } else { | ||
| inputScanner.next(); | ||
| System.out.println("[Ошибка] Введите корректную цену товара в формате: рубли.копейки"); | ||
| } | ||
| } | ||
|
|
||
| System.out.println("Хотите ли добавить ещё один товар? завершить/что-либо"); | ||
| String answer = inputScanner.next(); | ||
| if (answer.equalsIgnoreCase("завершить")) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static void printEachFriendAmount(Calculator calculator) { | ||
| double eachFriendAmount = calculator.getEachFriendAmount(); | ||
| String rubles = ""; | ||
| switch ((int)eachFriendAmount % 10) { | ||
| case 1: | ||
| rubles = "рубль"; | ||
| break; | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| rubles = "рубля"; | ||
| break; | ||
| case 0: | ||
| case 5: | ||
| case 6: | ||
| case 7: | ||
| case 8: | ||
| case 9: | ||
|
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. Можно поставить вместо перечисления оставшихся вариантов (0, 5, ..., 9) просто ветку |
||
| rubles = "рублей"; | ||
| break; | ||
| } | ||
| System.out.println("Добавленные товары:\r\n" + calculator.products + String.format("%.2f", eachFriendAmount) + " " + rubles); | ||
|
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 void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
| Scanner inputScanner = new Scanner(System.in); | ||
| Calculator calculator = initializeCalculator(inputScanner); | ||
| addProductsToCalculator(inputScanner, calculator); | ||
| printEachFriendAmount(calculator); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| public class Product { | ||
| String name; | ||
| double cost; | ||
|
|
||
| Product(String name, double cost) { | ||
| this.name = name; | ||
| this.cost = cost; | ||
| } | ||
| } |
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.
Можно подробнее писать пользователю, что ему стоит ввести слово завершить, чтобы закончить ввод