Repository navigation
Первая практическая работа #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 |
|---|---|---|
| @@ -1,22 +1,61 @@ | ||
| class Calculator { | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
| import java.util.Scanner; | ||
|
|
||
| int friendsCount; | ||
| public class Calculator { | ||
|
|
||
| String cart = "Добавленные товары:"; | ||
| double totalPrice = 0; | ||
| List<String> menuList = new LinkedList<>(); | ||
|
|
||
| Calculator(int friendsCount) { | ||
| this.friendsCount = friendsCount; | ||
| } | ||
| String stopWord = "завершить"; | ||
| String dishes; | ||
| double price; | ||
| static double priceSum; | ||
|
|
||
| void addItem(Item item) { | ||
| totalPrice += item.price; | ||
| cart = cart + "\n" + item.name; | ||
| public void costCalculator() { | ||
|
|
||
| System.out.println(item.name + " в корзине"); | ||
| while (true) { | ||
| Scanner scanner = new Scanner(System.in); | ||
| System.out.println("Введите название еды:"); | ||
| dishes = scanner.nextLine(); | ||
| menuList.add(dishes); | ||
| System.out.println("Введите стоимость в формате: \"'рубли.копейки' [10,45; 11,40]\""); | ||
| while (!scanner.hasNextDouble()) { | ||
| System.out.println("Вводить нужно только цифры! Попробуйте заново:"); | ||
| scanner.nextLine(); | ||
| } | ||
| price = scanner.nextDouble(); | ||
| if (price > 0) { | ||
| priceSum = priceSum + price; | ||
| System.out.println("Товар успешно добавлен! "); | ||
| } | ||
| while (price <= 0) { | ||
| if (price <= 0) { | ||
| System.out.println("Цена отрицательная, доходы мы еще не считаем! Введите заново: "); | ||
| price = scanner.nextDouble(); | ||
| if (price > 0) { | ||
| priceSum = priceSum + price; | ||
| System.out.println("Товар успешно добавлен! "); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| System.out.println("Если хотите продолжить введите - любой символ,если хотите остановиться введите 'завершить' "); | ||
| dishes = scanner.next(); | ||
| if (dishes.equalsIgnoreCase(stopWord)) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| double divideSum() { | ||
| return totalPrice / friendsCount; | ||
| static String costRounding(double i) { | ||
| i = Math.floor(i); | ||
| double modularDivision = i % 10; | ||
| if (modularDivision == 1) { | ||
| return "рубль"; | ||
| } else if (modularDivision == 2 || modularDivision == 3 || modularDivision == 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. Можно упростить modularDivision >= 2 && modularDivision <= 4 |
||
| return "рубля"; | ||
| } | ||
| 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-19, окончание должно быть "рублей", а сейчас дает рубля для 12-14. Для этого стоит проверять, что i % 100 между 11 и 19 включительно находится |
||
| } | ||
| } | ||
|
|
||
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,48 +2,44 @@ | |
|
|
||
| public class Main { | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| int friendCount; | ||
| while (true) { | ||
| System.out.println("На сколько человек необходимо разделить счет?"); | ||
| friendCount = scanner.nextInt(); | ||
|
|
||
| if (friendCount > 1) { | ||
| break; | ||
| } else if (friendCount == 1) { | ||
| System.out.println( | ||
| "Нет смысла делить сумму на одного человека. Давайте попробуем ввести другое значение, которое будет больше единицы."); | ||
| } else { | ||
| System.out.println("Неверное количество друзей. Значение должно быть болье единицы, давайте попробуем еще раз."); | ||
| Scanner scanner = new Scanner(System.in); | ||
| System.out.println("Введите количество людей:"); | ||
| while (!scanner.hasNextInt()) { | ||
| System.out.println("Вводить нужно только цифры! Попробуйте заново:"); | ||
| scanner.nextLine(); | ||
| } | ||
| int numberOfPeople = scanner.nextInt(); | ||
| while (numberOfPeople <= 1) { | ||
| if (numberOfPeople < 1) { | ||
| System.out.println("Введенно не корректное количество людей! Попробуйте занво:"); | ||
| numberOfPeople = scanner.nextInt(); | ||
| } | ||
| if (numberOfPeople == 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. Можно поставить просто else |
||
| System.out.println("Вы один и платите только вы. Попробуйте занво: "); | ||
| numberOfPeople = scanner.nextInt(); | ||
| } | ||
| } | ||
|
|
||
| Calculator calculator = new Calculator(friendCount); | ||
| Calculator calculator = new Calculator(); | ||
| calculator.costCalculator(); | ||
|
|
||
| while (true) { | ||
| System.out.println("Введите название товара"); | ||
| String name = scanner.next(); | ||
| double needPay = Calculator.priceSum / numberOfPeople; | ||
|
|
||
| System.out.println("Введите стоимость товара в формате: 'рубли.копейки' [10.45, 11.40]"); | ||
| double price = scanner.nextDouble(); | ||
| System.out.println("Список выбранных товаров: "); | ||
|
|
||
| calculator.addItem(new Item(name, price)); | ||
| for (String str : calculator.menuList) { | ||
| System.out.println(str); | ||
| } | ||
| System.out.printf("Общая сумма: %.2f %s%n", Calculator.priceSum, Calculator.costRounding(Calculator.priceSum)); | ||
| System.out.printf("Кол-во человек: %s%n", numberOfPeople); | ||
| System.out.printf("Каждый платит по: %.2f %s%n", needPay, Calculator.costRounding(needPay)); | ||
|
|
||
| System.out.println( | ||
| "Хотите добавить еще один товар? Введите любой символ для продолжения, либо 'Завершить' если больше нет товаров для добавления"); | ||
| String answer = scanner.next(); | ||
|
|
||
| if (answer.equalsIgnoreCase("Завершить")) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| double result = calculator.divideSum(); | ||
| Formatter formatter = new Formatter(); | ||
|
|
||
| System.out.println(calculator.cart); | ||
| System.out.println("Каждому человеку к оплате: " + formatter.roundResult(result) + " " + formatter.formatValue(result)); | ||
| } | ||
| } | ||
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.
Из-за строчки выше всегда будет true, можно убрать