diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java new file mode 100644 index 000000000..daaf31293 --- /dev/null +++ b/src/main/java/Calculator.java @@ -0,0 +1,18 @@ + + +public class Calculator { + String product = ""; + double totalPrice; + double personalAmount; + + public void addProduct(String productName, double costGoods) { + + product = product + productName +" "+":"+" "+Formatter.roundingCostProduct(costGoods) +" "+ Formatter.rubleCorrectCase(costGoods) + "\n" ; + } + + public void countingGoods(int numberPeople, double costGoods) { + + totalPrice = totalPrice + costGoods; + personalAmount = totalPrice / numberPeople; + } +} diff --git a/src/main/java/Formatter.java b/src/main/java/Formatter.java new file mode 100644 index 000000000..434f0c168 --- /dev/null +++ b/src/main/java/Formatter.java @@ -0,0 +1,33 @@ +public class Formatter { + + public static String roundingCostProduct(double izadadd) { + + String format = String.format("%.2f", izadadd); + return format; + } + + public static String rubleCorrectCase(double price) { + + int count = (int) Math.floor(price); + + { + double preLastDigit = count % 100 / 10; + if (preLastDigit == 1) + { + return "рублей"; + } + + switch (count % 10) + { + case 1: + return "рубль"; + case 2: + case 3: + case 4: + return "рубля"; + default: + return "рублей"; + } + } + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..4b06407f7 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,105 @@ +import java.util.Scanner; public class Main { + + private static boolean isNumber(String value) { + try { + Integer.parseInt(value); + return true; + } catch (NumberFormatException ex) { + return false; + } + } + + private static boolean isDouble(String value) { + try { + Double.parseDouble(value); + return true; + } catch (NumberFormatException ex) { + return false; + } + } + public static void main(String[] args) { - System.out.println("Hello world!"); + + //После запуска программа должна спрашивать у пользователя, на скольких человек необходимо разделить счёт. + + + int numberPeople; + while (true) { + Scanner scanner = new Scanner(System.in); + System.out.println("Введите количество людей (больше или равно 1)"); + String value = scanner.next(); + boolean isNumber = isNumber(value); + + if (isNumber) { + numberPeople = Integer.parseInt(value); + + if (numberPeople == 1) { + System.out.println("Нет смысла ничего считать и делить."); + } else if (numberPeople < 1) { + System.out.println("Это некорректное значение для подсчёта."); + } else if (numberPeople> 1) { + break; + } + } else { + System.out.println("Введите корректное количество гостей."); + } + } + + + + + //Запросите у пользователя название товара и его стоимость. + Calculator calculator = new Calculator(); + + + double costGoods = 0; + while (true) { + + Scanner scannerr = new Scanner(System.in); + + System.out.println("Введите название товара"); + String productName = scannerr.nextLine(); + + while (true) { + System.out.println("Введите стоимость товара (в формате руб.копейки) "); + String costG = scannerr.next(); + + boolean isDouble = isDouble(costG); + + if (isDouble) { + costGoods = Double.parseDouble(costG); + + if (costGoods < 1) { + System.out.println("Это некорректное значение для подсчёта."); + } else if (costGoods> 0) { + break; + } + } else { + System.out.println("Введите корректное количество стоимость товара."); + } + } + + + calculator.addProduct(productName, costGoods); + System.out.println("Товар успешно добавлен"); + + calculator.countingGoods( numberPeople, costGoods); + + System.out.println("Хотите добавить еще один товар или Завершить?"); + String addOrFinish = scannerr.next(); + + if (addOrFinish.equalsIgnoreCase("Завершить")) { + System.out.println("Все добавленные товары : " + + "\n " + calculator.product + + "\n" + "Общая сумма счета :" + " " +Formatter.roundingCostProduct(calculator.totalPrice)+" "+Formatter.rubleCorrectCase(calculator.totalPrice) + + "\n" + "Сумма счета на каждого человека :" + " " +Formatter.roundingCostProduct(calculator.personalAmount)+" "+ Formatter.rubleCorrectCase(calculator.personalAmount)); + break; + + } + + } + } } \ No newline at end of file

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