diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java new file mode 100644 index 000000000..8cf81b08d --- /dev/null +++ b/src/main/java/Calculator.java @@ -0,0 +1,20 @@ +import java.util.ArrayList; +public class Calculator { + ArrayList prices = new ArrayList<>(); + Formatter rub = new Formatter(); + public void addPrice(float price){ + prices.add(price); + } + public void samib() { + float sum = 0; + for(Float price : prices) { + sum += price; + } + int result2; + result2 = (int)Math.floor(sum / Main.quantity); + float result = sum / Main.quantity; + rub.formatRub(result2); + String format = "С каждого по %.2f %s"; + System.out.println(String.format(format, result, Formatter.padezh)); + } +} \ No newline at end of file diff --git a/src/main/java/Formatter.java b/src/main/java/Formatter.java new file mode 100644 index 000000000..05bb9b202 --- /dev/null +++ b/src/main/java/Formatter.java @@ -0,0 +1,15 @@ +public class Formatter { + public static String padezh; + public String formatRub(int result2) { + int x = result2 % 10; + int y = result2 % 100; + if(x == 0 || (x >= 5 && x <= 9) || (y>= 11 && y <= 14)){ + padezh = "рублей"; + }else if (x == 1 ) { + padezh = "рубль"; + }else { + padezh = "рубля"; + } + return padezh; + } +} \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..3ee565125 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,58 @@ - +import java.util.Scanner; public class Main { + + public static int quantity; public static void main(String[] args) { - System.out.println("Hello world!"); + Scanner scanner = new Scanner(System.in); + Products products = new Products(); + Calculator prices = new Calculator(); + String product; + float price; + + while (true) { + System.out.println("На скольких человек необходимо разделить счёт?"); + if(scanner.hasNextInt()){ + quantity = scanner.nextInt(); + scanner.nextLine(); + if (quantity > 1 ) { + break; + } else { + System.out.println("Введите значение больше 1"); + } + } else { + System.out.println("Введите целое число"); + scanner.nextLine(); + } + } + + String end = ""; + while (!end.equalsIgnoreCase("Завершить")) { + System.out.println("Введите название блюда"); + product = scanner.nextLine(); + products.addProduct(product); + while (true) { + System.out.println("Введите цену блюда"); + if (scanner.hasNextFloat() ) { + price = scanner.nextFloat(); + scanner.nextLine(); + if(price > 0) { + prices.addPrice(price); + break; + } else { + System.out.println("Введите цену больше 0"); + } + } else { + System.out.println("введите цифровое значение"); + scanner.nextLine(); + } + } + System.out.println("Товар успешно добавлен"); + System.out.println("Добавить новый товар или завершить?"); + end = scanner.nextLine(); + } + System.out.println("Добавленные товары:"); + products.outputProducts(); + prices.samib(); + scanner.close(); } } \ No newline at end of file diff --git a/src/main/java/Products.java b/src/main/java/Products.java new file mode 100644 index 000000000..5d06a670e --- /dev/null +++ b/src/main/java/Products.java @@ -0,0 +1,12 @@ +import java.util.ArrayList; +public class Products { + ArrayList products = new ArrayList<>(); + public void addProduct(String product){ + products.add(product); + } + public void outputProducts() { + for(String product : products) { + System.out.println(product); + } + } +} \ No newline at end of file