From 6c415a035cb4c3b00fc6111cf77513552ce4293c Mon Sep 17 00:00:00 2001 From: dencharski Date: Wed, 7 Dec 2022 21:26:03 +0300 Subject: [PATCH 1/2] create Calculator --- src/main/java/Calculator.java | 50 ++++++++++++++++++ src/main/java/Main.java | 96 ++++++++++++++++++++++++++++++++++- src/main/java/Product.java | 9 ++++ 3 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 src/main/java/Calculator.java create mode 100644 src/main/java/Product.java diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java new file mode 100644 index 000000000..616fd0a42 --- /dev/null +++ b/src/main/java/Calculator.java @@ -0,0 +1,50 @@ +public class Calculator { + private int peoples; + private double finalPrice; + private String allProductNames = ""; + private Double priceForOnePeople; + + public void setPeoples(int peoples) { + this.peoples = peoples; + } + + public String addProduct(Product product) { + + finalPrice = finalPrice + product.productPrice; + if (allProductNames.isEmpty()) { + allProductNames = product.productName; + } else { + allProductNames = allProductNames + ", \n" + product.productName; + } + priceForOnePeople = finalPrice / peoples; + + return ("Продукт " + product.productName + " по цене " + product.productPrice + " добавлен."); + + } + + public String getAllProductNames() { + + return allProductNames; + } + + public String getFinalPrice() { + + return ("Итоговая общая цена: " + convertPriceToString(finalPrice)); + } + + public String getPriceForOnePeople() { + + return ("Каждый человек должен заплатить: " + convertPriceToString(priceForOnePeople)); + } + + private String convertPriceToString(double price) { + String stringPrice = String.format("%.2f", price); + String zeroEnd = "0"; + if (stringPrice.endsWith(zeroEnd)) { + stringPrice = stringPrice + " рубля"; + } else { + stringPrice = stringPrice + " рубль"; + } + return stringPrice; + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index a9198c435..103f1e177 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,8 +1,102 @@ +import java.util.Scanner; + public class Main { + static Scanner scanner; + static Calculator calculator; + static int peoples = 0; + static boolean isPeoplesSetCorrectly = true; + static boolean isProductPurchased = false; + static String stopProductPurchased = "Завершить"; + public static void main(String[] args) { + scanner = new Scanner(System.in); + // ваш код начнется здесь // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости - System.out.println("Привет Мир"); + while (isPeoplesSetCorrectly) { + System.out.println("На сколько человек делим счет?"); + + try { + String s = scanner.next(); + peoples = Integer.parseInt(s); + + if (peoples == 1) { + showErrorMessage("Количество человек должно быть более 1."); + } + if (peoples < 1) { + showErrorMessage("Маловато будет. Это менее 1."); + } + if (peoples> 1) { + isPeoplesSetCorrectly = false; + isProductPurchased = true; + } + + + } catch (Exception e) { + showErrorMessage(e.getMessage()); + } + + } + + calculator = new Calculator(); + calculator.setPeoples(peoples); + + while (isProductPurchased) { + System.out.println("Ведите название товара."); + String productName; + double productPrice; + productName = scanner.next(); + + boolean isPriceSetCorrectly = false; + while (!isPriceSetCorrectly) { + + System.out.println("Введите цену товара в формате <рубли.копейки>."); + String stringProductPrice = scanner.next().trim(); + if (stringProductPrice.contains(",")) { + stringProductPrice = stringProductPrice.replace(",", "."); + } + + try { + productPrice = Double.parseDouble(stringProductPrice); + if (productPrice <= 0.0) { + showErrorMessage("Цена товара слишком мала. " + productPrice); + break; + } else { + Product product = new Product(productName, productPrice); + System.out.println(calculator.addProduct(product)); + isPriceSetCorrectly = true; + } + } catch (Exception e) { + System.out.println(e.getMessage()); + showErrorMessage("Цена должна быть указана в виде ." + e.getMessage()); + } + } + System.out.println("Желаете добавить еще товар? Для завершения наберите <Завершить>."); + + String buyerWantsBuy = scanner.next(); + if (buyerWantsBuy.trim().equalsIgnoreCase(stopProductPurchased)) { + isProductPurchased = false; + } + } + + + System.out.println(calculator.getAllProductNames()); + System.out.println(calculator.getFinalPrice()); + + System.out.println(calculator.getPriceForOnePeople()); + + onDestroy(); } + + + static void showErrorMessage(String errorString) { + System.out.println("Ошибка! " + errorString); + } + + static void onDestroy() { + scanner = null; + calculator = null; + } + } diff --git a/src/main/java/Product.java b/src/main/java/Product.java new file mode 100644 index 000000000..1a827c8ff --- /dev/null +++ b/src/main/java/Product.java @@ -0,0 +1,9 @@ +public class Product { + String productName; + double productPrice; + + public Product(String productName, double productPrice) { + this.productName = productName; + this.productPrice = productPrice; + } +} From 692a7a201fe812f3efc2110b2826a61807aee88e Mon Sep 17 00:00:00 2001 From: dencharski Date: Fri, 9 Dec 2022 18:34:54 +0300 Subject: [PATCH 2/2] =?UTF-8?q?-scanner=20change=20next()=20to=20nextInt.?= =?UTF-8?q?=20-deleted=20fun=20onDestroy().=20-in=20fun=20addProduct()=20c?= =?UTF-8?q?hange=20return=20String=20to=20String.format().=20-changed=20th?= =?UTF-8?q?e=20calculation=20of=20the=20endings=20of=20the=20word=20"?= =?UTF-8?q?=D1=80=D1=83=D0=B1=D0=BB=D1=8C".=20-the=20variable=20"priceForO?= =?UTF-8?q?nePeople"=20is=20counted=20once?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Calculator.java | 30 ++++++++++++++++++------------ src/main/java/Main.java | 9 +-------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java index 616fd0a42..446c208a2 100644 --- a/src/main/java/Calculator.java +++ b/src/main/java/Calculator.java @@ -2,49 +2,55 @@ public class Calculator { private int peoples; private double finalPrice; private String allProductNames = ""; - private Double priceForOnePeople; public void setPeoples(int peoples) { this.peoples = peoples; } public String addProduct(Product product) { - finalPrice = finalPrice + product.productPrice; if (allProductNames.isEmpty()) { allProductNames = product.productName; } else { allProductNames = allProductNames + ", \n" + product.productName; } - priceForOnePeople = finalPrice / peoples; - - return ("Продукт " + product.productName + " по цене " + product.productPrice + " добавлен."); + return String.format("Продукт %s по цене %.2f добавлен.", product.productName, product.productPrice); } public String getAllProductNames() { - return allProductNames; } public String getFinalPrice() { - return ("Итоговая общая цена: " + convertPriceToString(finalPrice)); } public String getPriceForOnePeople() { - + double priceForOnePeople = finalPrice / peoples; return ("Каждый человек должен заплатить: " + convertPriceToString(priceForOnePeople)); } private String convertPriceToString(double price) { String stringPrice = String.format("%.2f", price); - String zeroEnd = "0"; - if (stringPrice.endsWith(zeroEnd)) { - stringPrice = stringPrice + " рубля"; + + price = Math.floor(price); + double remains = price % 100; + + if (11 <= remains && remains <= 14) { + stringPrice = stringPrice + " рублей"; } else { - stringPrice = stringPrice + " рубль"; + remains = remains % 10; + if (remains == 1) { + stringPrice = stringPrice + " рубль"; + } + else if (2 <= remains && remains <= 4) { + stringPrice = stringPrice + " рубля"; + } else { + stringPrice = stringPrice + " рублей"; + } } + return stringPrice; } } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 103f1e177..17e023944 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -18,8 +18,7 @@ public static void main(String[] args) { System.out.println("На сколько человек делим счет?"); try { - String s = scanner.next(); - peoples = Integer.parseInt(s); + peoples = scanner.nextInt(); if (peoples == 1) { showErrorMessage("Количество человек должно быть более 1."); @@ -86,7 +85,6 @@ public static void main(String[] args) { System.out.println(calculator.getPriceForOnePeople()); - onDestroy(); } @@ -94,9 +92,4 @@ static void showErrorMessage(String errorString) { System.out.println("Ошибка! " + errorString); } - static void onDestroy() { - scanner = null; - calculator = null; - } - }

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