diff --git a/src/main/java/Bill.java b/src/main/java/Bill.java new file mode 100644 index 0000000..ec1cff8 --- /dev/null +++ b/src/main/java/Bill.java @@ -0,0 +1,34 @@ +import java.util.ArrayList; + +public class Bill { + private ArrayList products = new ArrayList(); + private double totalSum = 0; + + public void addProduct(){ + + String productName = Product.initName(); + double productPrice = Product.initPrice(); + + Product product = new Product(productName, productPrice); + + this.products.add(product); + this.totalSum = this.totalSum + productPrice; + + System.out.printf("В счет добавили позицию \"%s\" по стоимости %s\n", product.getName(), Money.getRurPrice(product.getPrice())); + } + + public void printBill(){ + System.out.println("Добавленные товары:"); + + for(Product product : products){ + System.out.printf("\"%s\", стоимость %s\n", product.getName(), Money.getRurPrice(product.getPrice())); + } + + } + + public void printSplitSum(int persons){ + double splitSum = this.totalSum/persons; + + System.out.printf("Каждый человек должен заплатить %s.", Money.getRurPrice(splitSum)); + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index a9198c4..54f72a7 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,8 +1,74 @@ +import java.util.Scanner; + public class Main { + private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) { - // ваш код начнется здесь - // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости - System.out.println("Привет Мир"); + System.out.println("Привет, username! Это калькулятор счета.\n"); + + //Заполним количество человек на которых будем делить счет + int persons = getPersonCount(); + + //Заполним счет позициями + Bill bill = fillBill(); + + //Напечатаем счет + bill.printBill(); + + //Напечатаем информацию о сумме на человека + bill.printSplitSum(persons); } -} + + private static int getPersonCount() { + System.out.println("На сколько человек нужно разделить счет?"); + + int persons = 0; + String inputText; + boolean finishFlag = false; + + while (persons <= 1 && !finishFlag) { + inputText = scanner.nextLine().trim(); + + if (Messages.Bye(inputText)) { + finishFlag = true; + } else { + try { + persons = Integer.parseInt(inputText); + + if (persons <= 0) { + System.out.println("Нет людей - нет проблем! Нельзя разделить счет на 0 или меньше человек! Попробуйте еще раз."); + } else if (persons == 1) { + System.out.println("Forever alone! Найди друзей, нельзя разделить счет на одного! Попробуй еще раз."); + } + } catch (NumberFormatException e) { + System.out.println("Мы точно в этой вселенной? Используй арабские цифры. Попробуй еще раз. "); + } + } + } + + return persons; + } + + private static Bill fillBill(){ + Bill bill = new Bill(); + System.out.println("Введите позиции из счета.\n"); + + String inputText; + boolean finishFlag = false; + + while (!finishFlag) { + bill.addProduct(); + + System.out.println("Добавить еще один продукт? Для завершения введите \"" + Messages.FINISH_PHRASE + "\""); + inputText = scanner.nextLine(); + + if (Messages.Bye(inputText)) { + finishFlag = true; + } + + } + + return bill; + } + +} \ No newline at end of file diff --git a/src/main/java/Messages.java b/src/main/java/Messages.java new file mode 100644 index 0000000..d3dc5d9 --- /dev/null +++ b/src/main/java/Messages.java @@ -0,0 +1,11 @@ +public class Messages { + public static final String FINISH_PHRASE = "Завершить"; + + public static boolean Bye(String text){ + if (FINISH_PHRASE.equalsIgnoreCase(text.trim())) { + System.out.println("Bye!"); + return true; + } + return false; + } +} diff --git a/src/main/java/Money.java b/src/main/java/Money.java new file mode 100644 index 0000000..01a7857 --- /dev/null +++ b/src/main/java/Money.java @@ -0,0 +1,26 @@ +public class Money { + public static String getRurPrice(double price){ + + String result; + int roundedPrice = (int)Math.floor(price); + + if (roundedPrice%100>= 11 && roundedPrice%100 <= 14){ + result = String.format("%.2f рублей", price); + } else { + switch (roundedPrice%10) { + case 1: + result = String.format("%.2f рубль", price); + break; + case 2: + case 3: + case 4: + result = String.format("%.2f рубля", price); + break; + default: + result = String.format("%.2f рублей", price); + break; + } + } + return result; + } +} diff --git a/src/main/java/Product.java b/src/main/java/Product.java new file mode 100644 index 0000000..cca0ec6 --- /dev/null +++ b/src/main/java/Product.java @@ -0,0 +1,62 @@ +import java.util.Scanner; + +public class Product { + private static final Scanner scanner = new Scanner(System.in); + private final String name; + private final double price; + + Product(String name, double price){ + this.name = name; + this.price = price; + } + + //Запрос наименования позиции + public static String initName(){ + System.out.println("Введите наименовние позиции из счете"); + + String inputText = ""; + + while(inputText.isEmpty()) { + inputText = scanner.nextLine(); + + if (inputText.isEmpty()){ + System.out.println("Наименованое не может быть пустым. Попробуйте еще раз."); + } + } + + return inputText; + } + + //Запрос цены по позиции + public static double initPrice(){ + System.out.println("Введите стоимость позиции из счете в формате XX.XX"); + + String inputText; + double price = 0; + + while (price <= 0) { + inputText = scanner.nextLine().trim(); + try { + price = Double.parseDouble(inputText.replace(",", ".")); + + if(price < 0) { + System.out.println("Стоимость не может быть отрицательной. Попробуйте еще раз."); + } else if (price == 0){ + System.out.println("Стоимость не может быть равна 0. Попробуйте еще раз."); + } + } catch (NumberFormatException e) { + System.out.println("Введите стоимость позиции из счете в формате XX.XX. Попробуйте еще раз."); + } + } + + return price; + } + + public double getPrice(){ + return this.price; + } + + public String getName(){ + return this.name; + } +}

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