diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java new file mode 100644 index 000000000..c7a7061ed --- /dev/null +++ b/src/main/java/Calculator.java @@ -0,0 +1,62 @@ +import java.util.ArrayList; +import java.util.Scanner; + +public class Calculator { + public Calculator(int totalPersons) { + this.totalPersons = totalPersons; + + } + static ArrayList products = new ArrayList(); + static int totalPersons; + static double calculate(){ + double sum = 0; + for (Product product:products) {sum += product.price;} + return sum; + } + static void addProducts(){ + Scanner scanner = new Scanner(System.in); + + System.out.println("Здравствуйте!"); + System.out.println("Сделайте заказ (для завершения введите 'завершить'): "); + + String productName; + double productPrice; + double totalPrice = 0; + Declension roubles; + + do { + System.out.print("Наименование товара: "); + productName = scanner.nextLine(); + + if (!productName.equalsIgnoreCase("завершить")) { + System.out.print("Цена товара: "); + while (!scanner.hasNextDouble()) { + System.out.println("Некорректное значение. Повторите ввод"); + scanner.next(); + } + productPrice = scanner.nextDouble(); + scanner.nextLine(); + + if (productPrice> 0) { + Product product = new Product(productName, productPrice); + + roubles = new Declension(product.price); + System.out.println("Товар '" + product.name + "' добавлен в счет, стоимость: " + String.format("%.2f", product.price)+roubles.getDeclension()); + + products.add(product); + totalPrice = calculate(); + + roubles = new Declension(totalPrice); + + System.out.println("Добавлено товаров на сумму "+String.format("%.2f", totalPrice) + roubles.getDeclension()); + } else { + System.out.println("Введите цену товара больше нуля"); + } + } + } while (!productName.equalsIgnoreCase("завершить")); + + double result = totalPrice/totalPersons; + roubles = new Declension(result); + System.out.println("Каждый из вас должен заплатить по "+String.format("%.2f", result) +roubles.getDeclension()); + } +} diff --git a/src/main/java/Declension.java b/src/main/java/Declension.java new file mode 100644 index 000000000..e2206b7b3 --- /dev/null +++ b/src/main/java/Declension.java @@ -0,0 +1,18 @@ +public class Declension { + + String declension; + double value; + public Declension(double value) { + this.value = value; + } + + public String getDeclension() { + if( ((int)value % 10 ==1) && ((int)value % 100 != 11)){ + return " рубль";} + else if ((((int)value % 10>= 2) && ((int)value %10 <= 4))&& (((int)value % 100 < 10) || ((int)value %100>=20))){ + return " рубля"; + } + else {return " рублей";} + } +} + diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..87a6bb5b9 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,31 @@ +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 number = 0; + boolean intScanner = false; + + //Ввод количества человек на которых будет разделен счет + while (!intScanner) { + System.out.print("На скольких человек будет разделен счёт? "); + if(scanner.hasNextInt()) { + number = scanner.nextInt(); + if (number> 1){intScanner = true;} + else { + System.out.println("Введите количество человек больше одного"); + } + + } else { + System.out.println("Некорректное значение. Повторите ввод"); + scanner.next(); + } + } + System.out.println("Счет будет разделен на " + number); + + Calculator calculator = new Calculator(number); + calculator.addProducts(); + } } \ 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..c23983dbc --- /dev/null +++ b/src/main/java/Product.java @@ -0,0 +1,9 @@ +public class Product { + String name; + double price; + + public Product(String name, double price) { + this.name = name; + this.price = price; + } +}

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