diff --git a/settings.gradle b/settings.gradle index 716abf2..be110bf 100644 --- a/settings.gradle +++ b/settings.gradle @@ -12,4 +12,4 @@ dependencyResolutionManagement { mavenCentral() } } -rootProject.name = "BillCalculator" +rootProject.name = "Java-Module-Project" diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java index 2fbc00d..9657425 100644 --- a/src/main/java/Calculator.java +++ b/src/main/java/Calculator.java @@ -1,22 +1,58 @@ -class Calculator { +import java.util.Scanner; - int friendsCount; +public class Calculator { + public static void calculator(int delitel) { - String cart = "Добавленные товары:"; - double totalPrice = 0; + Scanner scannerProduct = new Scanner(System.in); + Product productAll = new Product(); + while (true) { + System.out.println("Введите название товара"); + String product = scannerProduct.next(); + String and = "Завершить"; + if (product.equalsIgnoreCase(and)) { + break; + } - Calculator(int friendsCount) { - this.friendsCount = friendsCount; - } + System.out.println("Введите стоимость товара (рубли.копейки)"); + Float price = cost(); + productAll.allProduct = productAll.allProduct + "\n" + product ; + productAll.allPrice = productAll.allPrice + price; + System.out.println("Товар добавлен, хотите добавить еще товар ? \n Если нет, введите команду \"Завершить\""); + String answer = scannerProduct.next(); + if (answer.equalsIgnoreCase(and)) { + break; + } - void addItem(Item item) { - totalPrice += item.price; - cart = cart + "\n" + item.name; + } + float personPrice = productAll.allPrice / delitel; + String personPriceString = String.format("%.2f", personPrice); + int personPriceInteger = (int) personPrice; + String rub = PrintRub.getStringRub(personPriceInteger); + System.out.println("Добавленные товары:" + productAll.allProduct + "\n" + "Сумма к оплате для каждого человека - " + personPriceString + " " + rub); - System.out.println(item.name + " в корзине"); } - double divideSum() { - return totalPrice / friendsCount; + //введение цены + public static float cost() { + Scanner scanner = new Scanner(System.in); + float price = 0.0f; + try { + String value = scanner.next().trim(); + price = Float.parseFloat(value); + int pointPosition = value.indexOf('.'); + if (!(pointPosition == -1 || value.length() - 1 - pointPosition <= 2)) { + System.out.println("Введено некорректное значение, попробуйте еще."); + price = cost(); + } + if (price < 0) { + System.out.println("Введено некорректное значение, попробуйте еще."); + price = cost(); + } + } catch (Exception e) { + System.out.println("Введено некорректное значение, попробуйте еще."); + price = cost(); + } + + return price; } } diff --git a/src/main/java/Formatter.java b/src/main/java/Formatter.java deleted file mode 100644 index 3f915b7..0000000 --- a/src/main/java/Formatter.java +++ /dev/null @@ -1,17 +0,0 @@ -public class Formatter { - - String formatValue(double price) { - double roundedValue = Math.floor(price); - if (roundedValue == 1) { - return "рубль"; - } else if (roundedValue >= 2 && roundedValue <= 4) { - return "рубля"; - } else { - return "рублей"; - } - } - - String roundResult(final double result) { - return String.format("%.2f", result); - } -} diff --git a/src/main/java/Item.java b/src/main/java/Item.java deleted file mode 100644 index fad8a4e..0000000 --- a/src/main/java/Item.java +++ /dev/null @@ -1,10 +0,0 @@ -class Item { - - String name; - double price; - - Item(String name, double price) { - this.name = name; - this.price = price; - } -} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 11ba5d3..83128ba 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -3,47 +3,106 @@ public class Main { public static void main(String[] args) { + System.out.println("Здравствуйте! На сколько человек разделить счёт?"); Scanner scanner = new Scanner(System.in); - - int friendCount; - while (true) { - System.out.println("На сколько человек необходимо разделить счет?"); - friendCount = scanner.nextInt(); - - if (friendCount > 1) { - break; - } else if (friendCount == 1) { - System.out.println( - "Нет смысла делить сумму на одного человека. Давайте попробуем ввести другое значение, которое будет больше единицы."); - } else { - System.out.println("Неверное количество друзей. Значение должно быть болье единицы, давайте попробуем еще раз."); - } - } - - Calculator calculator = new Calculator(friendCount); - + int x = 0; while (true) { - System.out.println("Введите название товара"); - String name = scanner.next(); - - System.out.println("Введите стоимость товара в формате: 'рубли.копейки' [10.45, 11.40]"); - double price = scanner.nextDouble(); - - calculator.addItem(new Item(name, price)); - - System.out.println( - "Хотите добавить еще один товар? Введите любой символ для продолжения, либо 'Завершить' если больше нет товаров для добавления"); - String answer = scanner.next(); - - if (answer.equalsIgnoreCase("Завершить")) { - break; + try { + x = scanner.nextInt(); + if (x >= 2) { // если пользователь вводит корректное значение + System.out.println(); + break; + } + } catch (Exception e) { + scanner.next(); } + System.out.println("Введено некорректное значение, попробуйте еще."); } + Calculator.calculator(x); + } - double result = calculator.divideSum(); - Formatter formatter = new Formatter(); +// +// +// public static void calculator(int delitel) { +// +// Scanner scannerProduct = new Scanner(System.in); +// String allProduct = ""; +// Float allPrice = 0.0f; +// while (true) { +// System.out.println("Введите название товара"); +// String product = scannerProduct.next(); +// String and = "Завершить"; +// if (product.equalsIgnoreCase(and)) { +// break; +// } +// +// System.out.println("Введите стоимость товара (рубли.копейки)"); +// Float price = cost(); +// allProduct = allProduct + "\n" + product + ": " + price; +// allPrice = allPrice + price; +// System.out.println("Товар добавлен, хотите добавить еще товар ? \n Если нет, введите команду \"Завершить\""); +// String answer = scannerProduct.next(); +// if (answer.equalsIgnoreCase(and)) { +// break; +// } +// +// } +// float personPrice = allPrice / delitel; +// String personPriceString = String.format("%.2f", personPrice); +// int personPriceInteger = (int) personPrice; +// String rub = conjugation(personPriceInteger); +// System.out.println("Добавленные товары:" + "\n" + allProduct + "\n" + "Сумма к оплате для каждого человека - " + personPriceString + rub); +// +// } +// +// //введение цены +// public static float cost() { +// Scanner scanner = new Scanner(System.in); +// float price = 0.0f; +// try { +// String value = scanner.next().trim(); +// price = Float.parseFloat(value); +// int pointPosition = value.indexOf('.'); +// if (!(pointPosition == -1 || value.length() - 1 - pointPosition <= 2)) { +// System.out.println("Введено некорректное значение, попробуйте еще."); +// price = cost(); +// } +// if (price > 0) { +// System.out.println("Введено некорректное значение, попробуйте еще."); +// price = cost(); +// } +// } catch (Exception e) { +// System.out.println("Введено некорректное значение, попробуйте еще."); +// price = cost(); +// } +// +// return price; +// } - System.out.println(calculator.cart); - System.out.println("Каждому человеку к оплате: " + formatter.roundResult(result) + " " + formatter.formatValue(result)); - } +// public static String conjugation(int integer) { +// int remainder = integer % 100; +// if (remainder >= 11 && remainder <= 14) { +// return "рублей"; +// } +// remainder = integer % 10; +// switch (remainder) { +// case 1: +// return "рубль"; +// case 2: +// case 3: +// case 4: +// return "рубля"; +// case 0: +// case 5: +// case 6: +// case 7: +// case 8: +// case 9: +// return "рублей"; +// default: +// return "рубль"; +// } +// +// } } + diff --git a/src/main/java/PrintRub.java b/src/main/java/PrintRub.java new file mode 100644 index 0000000..822ab74 --- /dev/null +++ b/src/main/java/PrintRub.java @@ -0,0 +1,27 @@ +class PrintRub { + public static String getStringRub(int integer) { + int remainder = integer % 100; + if (remainder >= 11 && remainder <= 14) { + return "рублей"; + } + remainder = integer % 10; + switch (remainder) { + case 1: + return "рубль"; + case 2: + case 3: + case 4: + return "рубля"; + case 0: + case 5: + case 6: + case 7: + case 8: + case 9: + return "рублей"; + default: + return "рубль"; + } + + } +} diff --git a/src/main/java/Product.java b/src/main/java/Product.java new file mode 100644 index 0000000..c97dd73 --- /dev/null +++ b/src/main/java/Product.java @@ -0,0 +1,5 @@ +class Product { + String allProduct = ""; + Float allPrice = 0.0f; + +}