diff --git a/src/main/java/Calculator.java b/src/main/java/Calculator.java new file mode 100644 index 000000000..446c208a2 --- /dev/null +++ b/src/main/java/Calculator.java @@ -0,0 +1,56 @@ +public class Calculator { + private int peoples; + private double finalPrice; + private String allProductNames = ""; + + 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; + } + + 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); + + price = Math.floor(price); + double remains = price % 100; + + if (11 <= remains && remains <= 14) { + stringPrice = stringPrice + " рублей"; + } else { + 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 a9198c435..17e023944 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,8 +1,95 @@ +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 { + peoples = scanner.nextInt(); + + 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()); + } + + + static void showErrorMessage(String errorString) { + System.out.println("Ошибка! " + errorString); + } + } 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; + } +}

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