diff --git a/src/main/java/CountFriends.java b/src/main/java/CountFriends.java new file mode 100644 index 000000000..3f18417ea --- /dev/null +++ b/src/main/java/CountFriends.java @@ -0,0 +1,49 @@ +import java.util.Scanner; + +public class CountFriends { + + static int countFriends = 0; + static Scanner sc = new Scanner(System.in); + + //Функция ввода количества человек + public static int inputCountFriends() { + do { + System.out.println("На скольких человек необходимо разделить счёт?"); + try { + if (sc.hasNextLine()) { + String inputLine = sc.nextLine(); + inputLine = inputLine.trim(); + countFriends = Integer.parseInt(inputLine); + } + } catch (Exception e) { + System.out.println("Ошибка: " + e.getMessage()); + } + switch (countFriends) { + case 0: + System.out.println("Введено некорректное количество человек.\n Введите еще раз!)"); + break; + case 1: + System.out.println("Нечего делить в счете оплачиваемым одним человеком.\n Введите еще раз!)"); + break; + default: + if (countFriends < 0) + System.out.println("Кхм... Попробуем еще раз!"); + else return countFriends; + } + + } + while (true); + } + + public static double calculate(int countFriends, double countMoney) { + return countMoney / countFriends; + } + + public static String ruble(double manyForOne) { + int value = (int) manyForOne; + if ((value % 100 / 10) % 10 == 1 || value % 10 == 0 || value % 10>= 5) return "рублей"; + else if (value % 10 == 1) return "рубль"; + else if (value % 10> 1 && value % 10 < 5) return "рубля"; + else return "ошибка"; + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..95ed77353 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,15 @@ +import java.util.Scanner; public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + Scanner sc = new Scanner(System.in); + int countFriends = CountFriends.inputCountFriends(); + Products.addProducts(); + double countMoney = Products.getSumm(); + double manyForOne = CountFriends.calculate(countFriends, countMoney); + + System.out.println(Products.getListProducts()); + System.out.println("Каждый должен заплатить по: " + String.format("%.2f", manyForOne).replace(",", ".") + " " + CountFriends.ruble(manyForOne)); + sc.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..eb6b43a65 --- /dev/null +++ b/src/main/java/Products.java @@ -0,0 +1,59 @@ + +import java.util.Scanner; + +public class Products { + static Scanner sc = new Scanner(System.in); + private static String listProducts = "Добавленные товары:"; + private static double summ = 0.00; + static String inputName; + static double price; + + public static void addProducts() { + do { + System.out.println("Введите название товара"); + if (sc.hasNextLine()) { + inputName = sc.nextLine(); + inputName = inputName.trim(); + } + if (inputName.equalsIgnoreCase("Завершить")) { + break; + } else if (inputName.length() == 0) continue; + else { + listProducts += "\n" + inputName; + } + while (true) { + System.out.println("Введите стоимость товара цифрой"); + try { + if (sc.hasNextLine()) { + String inputLine = sc.nextLine().trim(); + double price = Double.parseDouble(inputLine); + if (price < 0) { + System.out.println("Неправильная стоимость товара"); + } else { + summ += (double) price; + break; + } + } + } catch (Exception e) { + System.out.println("Ошибка: " + e.getMessage()); + } + + } + System.out.println("Товар успешно добавлен!"); + System.out.println("Введите \"Завершить\" для завершения или любой другой символ для продолжения"); + inputName = sc.nextLine(); + inputName = inputName.trim(); + if (inputName.equalsIgnoreCase("Завершить")) break; + } while (true); + } + + public static String getListProducts() { + return listProducts; + } + + public static double getSumm() { + return summ; + } + + +}

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