diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java new file mode 100644 index 000000000..ebd01748e --- /dev/null +++ b/src/main/java/Calculator.java @@ -0,0 +1,42 @@ +import java.util.ArrayList; + +public class Calculator { + int people; + float total = 0; + ArrayList products = new ArrayList(); + Calculator(int people){ + this.people = people; + } + public void addProduct(Product newProduct){ + products.add(newProduct); + total += newProduct.price; + } + public void print(){ + System.out.println("Добавленные товары:"); + for (int i = 0; i < products.size(); i ++){ + System.out.println(String.format("Продукт %s с ценой в %.2f %s", products.get(i).name, products.get(i).price, rubleFormat(products.get(i).price))); + } + System.out.println(String.format("\nИтого: %.2f %s", total, rubleFormat(total))); + float perPerson = total / (float) people; + System.out.println(String.format("Получается %.2f %s с человека", perPerson, rubleFormat(perPerson))); + } + + public String rubleFormat(float price){ + int check = (int) price; + String result = "ОшИбКаааааААаАаА"; + if (check % 100>= 11 && check % 100 <= 19){ + result = "рублей"; + } + else if (check % 10 == 1){ + result = "рубль"; + } + else if (check % 10>= 2 && check % 10 <= 4) { + result = "рубля"; + } + else{ + result = "рублей"; + } + + return result; + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..cb9cf6432 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,68 @@ +import java.util.Scanner; public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + Calculator calculator; + int peopleCount; + Scanner scanner = new Scanner(System.in); + while (true){ + System.out.println("На скольких человек необходимо разделить счёт?"); + + if (scanner.hasNextInt()){ + peopleCount = scanner.nextInt(); + scanner.nextLine(); + } + else { + System.out.println("Некорректное значение для подсчёта. Повторите попытку"); + scanner.nextLine(); + continue; + } + + if (peopleCount == 1){ + System.out.println("Зачем что то считать для одного человека?) Повторите попытку"); + } + else if (peopleCount < 0) { + System.out.println("Некорректное значение для подсчёта. Повторите попытку"); + } + else if (peopleCount> 1){ + calculator = new Calculator(peopleCount); + System.out.println("Ок, начинаем считать!"); + break; + } + } + + String name; + float price; + while (true){ + System.out.println( + "Добавим товар в список! \n" + + "Введите название товара:"); + name = scanner.nextLine(); + System.out.println("Введите цену товара в формате [рубли],[копейки], например 10,45 или 11,40:"); + + if (scanner.hasNextInt()){ + price = scanner.nextFloat(); + scanner.nextLine(); + } + else{ + System.out.println("Некорректное значение цены. Повторите попытку"); + scanner.nextLine(); + continue; + } + + if (price < 0){ + System.out.println("Некорректное значение цены. Повторите попытку"); + continue; + } + + calculator.addProduct(new Product(name, price)); + System.out.println(String.format("Товар %s с ценой %.2f %s добавлен!", name, price, calculator.rubleFormat(price))); + System.out.println("Если вы хотите завершить процесс добавления товаров, введите 'Завершить'"); + if (scanner.nextLine().equalsIgnoreCase("Завершить")){ + break; + } + } + + calculator.print(); } } \ 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..ac19b5ac5 --- /dev/null +++ b/src/main/java/Product.java @@ -0,0 +1,8 @@ +public class Product { + final String name; + final float price; + Product(String name, float price){ + this.name = name; + this.price = price; + } +}

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