diff --git a/src/main/java/Formatter.java b/src/main/java/Formatter.java new file mode 100644 index 000000000..dfa259fd7 --- /dev/null +++ b/src/main/java/Formatter.java @@ -0,0 +1,15 @@ +public class Formatter { + + public static String formatAmount(double amount) { + String currency = "рубль"; + int rubles = (int) amount; + if (rubles == 0 || rubles>= 5 && rubles <= 20 || rubles % 10>= 5 || rubles % 10 == 0 || rubles % 100>= 11 && rubles % 100 <= 19) { + currency = "рублей"; + } else if (rubles % 10 == 1) { + currency = "рубль"; + } else if (rubles % 10>= 2 && rubles % 10 <= 4) { + currency = "рубля"; + } + return String.format("%.2f %s", amount, currency); + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..be8890550 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,70 @@ -public class Main { +import java.util.InputMismatchException; +import java.util.Scanner; +public class Main +{ public static void main(String[] args) { - System.out.println("Hello world!"); + + Scanner scanner = new Scanner(System.in); + int numberOfPeople = 0; + + + while (numberOfPeople < 2) { + try { + System.out.println("Сколько человек разделяет счет?"); + numberOfPeople = scanner.nextInt(); + + if (numberOfPeople == 1) { + System.out.println("Нет смысла ничего считать и делить для одного человека."); + } else if (numberOfPeople < 1) { + System.out.println("Некорректное значение для подсчета."); + } + } catch (InputMismatchException e) { + System.out.println("Ошибка. Пожалуйста, введите корректное количество гостей."); + scanner.next(); // Discard the input + } + } + + System.out.println("Программа продолжает выполнение для " + numberOfPeople + " человек."); + + + double totalCost = 0; + String items = ""; + + while (true) { + System.out.println("Введите название товара (или 'Завершить', чтобы закончить):"); + String itemName = scanner.next(); + + if (itemName.equalsIgnoreCase("Завершить")) { + break; + } + + double itemCost = 0; + while (itemCost <= 0) { + try { + System.out.println("Введите стоимость товара в формате рубли.копейки:"); + itemCost = scanner.nextDouble(); + if (itemCost <= 0) { + System.out.println("Стоимость должна быть положительным числом."); + } + } catch (InputMismatchException e) { + System.out.println("Ошибка. Пожалуйста, введите корректную стоимость."); + scanner.next(); + } + } + + totalCost += itemCost; + items += itemName + ": " + itemCost + "\n"; + System.out.println("Товар успешно добавлен."); + } + + System.out.println("Добавленные товары:\n" + items); + + + double amountPerPerson = totalCost / numberOfPeople; + System.out.println("Каждый человек должен заплатить: " +Formatter.formatAmount(amountPerPerson)); + } -} \ No newline at end of file +} + +