diff --git a/src/main/java/Actions.java b/src/main/java/Actions.java new file mode 100644 index 000000000..1753bee8b --- /dev/null +++ b/src/main/java/Actions.java @@ -0,0 +1,57 @@ +import java.util.Scanner; + +public class Actions { + public static double GetProductPrice(Scanner scan) { + while (true) { + System.out.println("Введите цену товара!"); + if (scan.hasNextDouble()) { + double price = scan.nextDouble(); + if(price <= 0) + { + System.out.println("Ошибка! Ведите цену больше 0!"); + } + else + { + return price; + } + } else { + scan.nextLine(); + System.out.println("Ошибка! Введите дробное число!"); + } + } + } + + public static int GetNumber(Scanner scan) { + while (true) { + if (scan.hasNextInt()) { + return scan.nextInt(); + } else { + scan.nextLine(); + System.out.println("Ошибка! Введите число!"); + } + } + } + + public static String GetProducts(String name, String allNames) { + allNames = allNames + name + ".\n"; + return allNames; + } + + public static double GetPrices(double price, double allPrice) { + allPrice += price; + return allPrice; + } + + public static void ShowResult(double price) { + String result = "Цена для каждого: \"%.2f %s\""; + if (Math.floor(price) > 11 && Math.floor(price) < 15) { + System.out.println(String.format(result, price, "рублей")); + } else if (Math.floor(price) % 10 == 1) { + System.out.println(String.format(result, price, "рубль")); + } else if (Math.floor(price) % 10 > 1 && Math.floor(price) % 10 < 5) { + System.out.println(String.format(result, price, "рубля")); + } else { + System.out.println(String.format(result, price, "рублей")); + } + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..78bb1d1d1 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,39 @@ +import java.util.Scanner; public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + //Уважаемый ревьюрер, я забыл при прошлом отправлении сделать коммит, прошу прощения. + while (true) { + System.out.println("На сколько человек хотите разделить покупку?"); + Scanner scan = new Scanner(System.in); + + int n = Actions.GetNumber(scan); + + if (n == 1) { + System.out.println("Ощибка! Введите значение >1!"); + } else if (n < 1) { + System.out.println("Ощибка! Некорректное число! Введите значение >1!"); + } else { + double prices = 0.0; + String products = ""; + + while (true) { + System.out.println("Введите название товара или \"Завершить!\""); + scan.nextLine(); + String name = scan.nextLine(); + if (name.equalsIgnoreCase("Завершить")) { + System.out.println("Ваши товары: \n" + products); + Actions.ShowResult(prices / n); + scan.close(); + break; + } else { + double price = Actions.GetProductPrice(scan); + prices = Actions.GetPrices(price, prices); + products = Actions.GetProducts(name, products); + } + } + break; + } + } } -} \ No newline at end of file +}