diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java new file mode 100644 index 000000000..018b29750 --- /dev/null +++ b/src/main/java/Calculator.java @@ -0,0 +1,27 @@ +import java.util.ArrayList; + +public class Calculator { + + ArrayList products = new ArrayList(); + public void addProduct(Product product){ + if (products.add(product)) + System.out.println("Товар успешно добавлен"); + } + + public void printProducts() { + System.out.println("Добавленные товары:"); + for (Product it : products){ + System.out.println(it.name + ": " + it.price); + } + } + + public void calculateForPersons(int persons) { + double sum = 0f; + for (Product it : products){ + sum += it.price; + } + double total = sum / persons; + Formatter format = new Formatter(total); + System.out.println("Сумма, которую должен заплатить каждый: " + format.getStringPrice()); + } +} diff --git a/src/main/java/Formatter.java b/src/main/java/Formatter.java new file mode 100644 index 000000000..27cae94ce --- /dev/null +++ b/src/main/java/Formatter.java @@ -0,0 +1,23 @@ +public class Formatter { + double price; + public Formatter(double price){ + this.price = price; + } + public String getStringPrice() { + double prc = Math.floor(price); + prc %= 100; + if (prc>= 5 && prc <= 20){ + return String.format("%.2f", price) + " рублей"; + } + else { + prc %= 10; + if (prc == 1) { + return String.format("%.2f", price) + " рубль"; + } else if (prc> 1 && prc < 5){ + return String.format("%.2f", price) + " рубля"; + } else { + return String.format("%.2f", price) + " рублей"; + } + } + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..74f7da489 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,52 @@ +import java.util.Scanner; public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + int persons = -1; + Scanner scaner = new Scanner(System.in); + while (persons <= 1) { + System.out.println("Введите количество человек"); + if (scaner.hasNextInt()) { + persons = scaner.nextInt(); + if (persons <= 1){ + System.out.println("Введено некорректное значение (должно быть больше 1)"); + } + } else { + System.out.println("Введено некорректное значение (должно быть число)"); + scaner.next(); + } + } + System.out.println("Количество человек для расчета " + persons); + + Calculator calculator = new Calculator(); + while (true) { + System.out.println("Введите название товара (для завершения введите \"Завершить\"):"); + String name = scaner.next(); + if (name.equalsIgnoreCase("Завершить")) { + break; + } else { + double price = askForPrice(scaner); + calculator.addProduct(new Product(name, price)); + } + } + scaner.close(); + calculator.printProducts(); + calculator.calculateForPersons(persons); + } + public static double askForPrice(Scanner scanerPrice) { + while (true) { + System.out.println("Введите стоимость товара (в формате рубли.копейки)"); + if (scanerPrice.hasNextFloat()){ + double price = scanerPrice.nextDouble(); + if (price> 0) { + return price; + } else { + System.out.println("Стоимость не может быть меньше или равна нулю"); + } + }else { + System.out.println("Формат стоимости должен быть: рубли.копейки"); + scanerPrice.next(); + } + } } } \ No newline at end of file diff --git a/src/main/java/Product.java b/src/main/java/Product.java new file mode 100644 index 000000000..cbd9d7835 --- /dev/null +++ b/src/main/java/Product.java @@ -0,0 +1,8 @@ +public class Product { + double price; + String name; + public Product(String name, double price) { + this.name = name; + this.price = price; + } +}

AltStyle によって変換されたページ (->オリジナル) /