diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java new file mode 100644 index 000000000..784863701 --- /dev/null +++ b/src/main/java/Calculator.java @@ -0,0 +1,91 @@ +import java.util.ArrayList; +import java.util.Scanner; + + +public class Calculator { + private ArrayList items; + private int numberOfPeople; + + public Calculator() { + items = new ArrayList<>(); + } + + public void start() { + inputNumberOfPeople(); + inputItems(); + printItems(); + printAmountPerPerson(); + } + + private void inputNumberOfPeople() { + Scanner scanner = new Scanner(System.in); + boolean validInput = false; + while (!validInput) { + System.out.println("На сколько человек необходимо разделить счёт:"); + String input = scanner.nextLine().trim(); + if (input.matches("\\d+")) { + numberOfPeople = Integer.parseInt(input); + if (numberOfPeople <= 1) { + System.out.println("Ошибка: Введите корректное количество гостей, больше 1."); + } else { + validInput = true; + } + } else { + System.out.println("Ошибка: Введите целое число."); + } + } + } + + private void inputItems() { + Scanner scanner = new Scanner(System.in); + while (true) { + System.out.println("Введите название товара и его стоимость в формате 'название стоимость': например, пиво 58.99\nЛибо введите команду 'Завершить' для того, чтобы завершить процесс добавления товаров."); + String line = scanner.nextLine().trim(); + if (line.equalsIgnoreCase("завершить")) { + break; + } + + String[] parts = line.split(" "); + if (parts.length != 2 || !isValidPrice(parts[1])) { + System.out.println("Ошибка: Некорректный формат ввода или некорректная сумма товара."); + continue; + } + + String name = parts[0]; + double price = Double.parseDouble(parts[1].replace(',', '.')); + + items.add(new Item(name, price)); + System.out.println("Товар успешно добавлен."); + System.out.println("Хотите добавить еще один товар?"); + } + } + + private void printItems() { + System.out.println("Добавленные товары:"); + for (Item item : items) { + System.out.println(item.getName() + " - " + item.getPrice()); + } + } + + private void printAmountPerPerson() { + double total = 0; + for (Item item : items) { + total += item.getPrice(); + } + double perPerson = total / numberOfPeople; + + String rublesString = Formatter.formatRubles(perPerson); + + System.out.println("Каждый человек должен заплатить: " + rublesString); + } + + private boolean isValidPrice(String priceStr) { + try { + String normalizedPrice = priceStr.replace(',', '.'); + double price = Double.parseDouble(normalizedPrice); + return price >= 0; + } catch (NumberFormatException e) { + return false; + } + } +} diff --git a/src/main/java/Formatter.java b/src/main/java/Formatter.java new file mode 100644 index 000000000..f2ad677a3 --- /dev/null +++ b/src/main/java/Formatter.java @@ -0,0 +1,17 @@ +public class Formatter { + public static String formatRubles(double amount) { + int rubles = (int) amount; + int kopecks = (int) Math.round((amount - rubles) * 100); + + String rublesString; + if (rubles % 10 == 1 && rubles % 100 != 11) { + rublesString = "рубль"; + } else if (rubles % 10 >= 2 && rubles % 10 <= 4 && (rubles % 100 < 10 || rubles % 100 >= 20)) { + rublesString = "рубля"; + } else { + rublesString = "рублей"; + } + + return rubles + " " + rublesString + " " + String.format("%02d", kopecks) + " копеек"; + } +} \ No newline at end of file diff --git a/src/main/java/Item.java b/src/main/java/Item.java new file mode 100644 index 000000000..f7e2e7b85 --- /dev/null +++ b/src/main/java/Item.java @@ -0,0 +1,17 @@ +public class Item { + private String name; + private double price; + + public Item(String name, double price) { + this.name = name; + this.price = price; + } + + public String getName() { + return name; + } + + public double getPrice() { + return price; + } +} \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..eb25e764c 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,8 @@ + public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + Calculator calculator = new Calculator(); + calculator.start(); } } \ No newline at end of file