diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java new file mode 100644 index 000000000..6a58da5d6 --- /dev/null +++ b/src/main/java/Calculator.java @@ -0,0 +1,29 @@ +import java.util.ArrayList; + +class Calculator { + private ArrayList products; + + public Calculator() { + products = new ArrayList(); + } + + public void add(Product product) { + products.add(product); + } + + public double summa() { + double sum = 0; + for (Product product : products) { + sum += product.getPrice(); + } + return sum; + } + + public ArrayList listProducts() { + ArrayList namePrice = new ArrayList(); + for (Product product : products) { + namePrice.add(product.getName() + ":" + product.getPrice()); + } + return namePrice; + } +} \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..82b054c75 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,87 @@ +import java.util.ArrayList; +import java.util.InputMismatchException; +import java.util.Scanner; + + public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + Main prilozhenie = new Main(); + prilozhenie.go(); + } + + public void go() { + while (true) { + int human = amountOfPeople(); + if (human < 1) { + System.out.println("Некорректное значение для подсчета\nВведите корректное значение\n"); + } else if (human == 1) { + System.out.println("Сумма поделенная на 1 останется неизменной\n"); + } else if (human> 1) { + addOrCheck(human); + } + } + } + + private int amountOfPeople() { + try { + System.out.println("На скольких человек необходимо разделить счёт?"); + Scanner s = new Scanner(System.in); + return s.nextInt(); + } catch (InputMismatchException e) { + System.out.println("Введите число человек\n"); + return -1; + } + } + + private void addOrCheck(int human) { + Calculator calculator = new Calculator(); + while (true) { + System.out.println("Введите название товара или введите 'Завершить 'для завершения"); + Scanner m = new Scanner(System.in); + String name = m.nextLine(); + if (name.equalsIgnoreCase("Завершить")) { + printCheckAndCalculateSum(human, calculator); + break; + } else { + addProductToCalculator(name, calculator); + } + } + } + + private void printCheckAndCalculateSum(int human, Calculator calculator) { + System.out.println("Добавленные товары:"); + ArrayList check = calculator.listProducts(); + for (String product : check) { + System.out.println(product); + } + double sumOfCheck = calculator.summa(); + double sumForPeople = sumOfCheck / human; + int lastDigit = (int) (sumForPeople % 10); + switch (lastDigit) { + case 1: + System.out.printf("\nСумма, которую должен заплатить каждый человек: %.2f рубль\n", sumForPeople); + break; + case 2: + case 3: + case 4: + System.out.printf("\nСумма, которую должен заплатить каждый человек: %.2f рубля\n", sumForPeople); + break; + default: + System.out.printf("\nСумма, которую должен заплатить каждый человек: %.2f рублей\n", sumForPeople); + } + } + + private void addProductToCalculator(String name, Calculator calculator) { + try { + System.out.println("Введите цену товара в виде (рубли.копейки)"); + Scanner n = new Scanner(System.in); + double price = Double.parseDouble(n.next()); + Product product = new Product(name, price); + calculator.add(product); + System.out.println("Товар успешно добавлен в калькулятор\n"); + } catch (NumberFormatException exception) { + System.out.println("Введите число типа double\n"); + } } } \ 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..b963102c1 --- /dev/null +++ b/src/main/java/Product.java @@ -0,0 +1,17 @@ +class Product { + String name; + double price; + + public Product(String name, double price) { + this.name = name; + this.price = price; + } + + public double getPrice() { + return price; + } + + public String getName() { + return name; + } +}

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