diff --git a/src/main/java/Bill.java b/src/main/java/Bill.java new file mode 100644 index 000000000..a4961fc10 --- /dev/null +++ b/src/main/java/Bill.java @@ -0,0 +1,114 @@ +public class Bill { + String mealList = ""; + double totalBillPrice = 0; + int billVolume = 0; + double billPerMan = 0; + int peopleCount = 0; + void billNewMealAdd(String mealName, String mealPrice) { + this.totalBillPrice += Double.parseDouble(mealPrice); + this.billVolume++; + this.billPerMan = this.totalBillPrice / this.peopleCount; + if (this.mealList != "") { + this.mealList = this.mealList.concat("\n"); + } + String pricePerNoteTemplate = "%.2f"; + this.mealList = this.mealList.concat(getBillVolume()+". ").concat(mealName).concat(" - " + String.format(pricePerNoteTemplate, Double.parseDouble(mealPrice)) + " " + chooseCurrencyEnding(Double.parseDouble(mealPrice))); + } + void showBillReport() { + int peopleCount = getPeopleCount(); + String mealList = getMealList(); + double totalBillPrice = getTotalBillPrice(); + String totalCurrencyEnding = chooseCurrencyEnding(totalBillPrice); + double billPerMan = getBillPerMan(); + String perManCurrencyEnding = chooseCurrencyEnding(billPerMan); + String messageTemplate = "Добавленные товары в счёте на %d человек:\n%s\nОбщая сумма: %.2f %s.\nС каждого %.2f %s."; + System.out.println("============================"); + System.out.println(String.format(messageTemplate, peopleCount, mealList, totalBillPrice, totalCurrencyEnding, billPerMan, perManCurrencyEnding)); + System.out.println("============================"); + } + String getMealList() { + return this.mealList; + } + int getBillVolume() { + return this.billVolume; + } + double getTotalBillPrice() { + return this.totalBillPrice; + } + double getBillPerMan() { + return this.billPerMan; + } + int getPeopleCount() { + return this.peopleCount; + } + void setPeopleCount(String peopleCount) { + int peopleCountInt = Integer.parseInt(peopleCount); + this.peopleCount = peopleCountInt; + } + public static String chooseCurrencyEnding(double money) { + int moneyInt = (int) money; + String moneyString = "" + moneyInt; + if (moneyString.length() == 1) { + switch (moneyString) { + case "1": { + return "рубль"; + } + case "2": + case "3": + case "4": { + return "рубля"; + } + default: { + return "рублей"; + } + } + } else { + moneyString = moneyString.substring(moneyString.length() - 2, moneyString.length()); + switch (moneyString) { + case "21": + case "31": + case "41": + case "51": + case "61": + case "71": + case "81": + case "91": + case "01": { + return "рубль"; + } + case "22": + case "23": + case "24": + case "32": + case "33": + case "34": + case "42": + case "43": + case "44": + case "52": + case "53": + case "54": + case "62": + case "63": + case "64": + case "72": + case "73": + case "74": + case "82": + case "83": + case "84": + case "92": + case "93": + case "94": + case "02": + case "03": + case "04": { + return "рубля"; + } + default: { + return "рублей"; + } + } + } + } +} diff --git a/src/main/java/Main.java b/src/main/java/Main.java index db9356a08..a87e440ef 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,96 @@ +import java.util.Scanner; public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + System.out.println("Консольное приложение для распределения счёта на оплату.\n\n"); + Bill bill = new Bill(); + // ожидание адекватного количества человек + while (true) { + System.out.println("На сколько человек разделить счёт?"); + Scanner userReply = new Scanner(System.in); + String billShareCountString = userReply.nextLine(); + if (isBillShareCorrect(billShareCountString) == true) { + bill.setPeopleCount(billShareCountString); + break; + } + } + // ожидание команды "ЗАВЕРШИТЬ" + while (true) { + if (bill.getMealList() == "") { + System.out.println("Вы хотите добавить товар?\nЕсли нет, то введите слово \"завершить\" в любом регистре,\nесли да, то введите любой символ!"); + } else { + System.out.println("Вы хотите добавить еще один товар?\nЕсли нет, то введите слово \"завершить\" в любом регистре,\nесли да, то введите любой символ!"); + } + Scanner userReply = new Scanner(System.in); + if (userReply.nextLine().equalsIgnoreCase("завершить")) { + System.out.println("Работа программы завершена!\nИтоговый счёт:\n"); + bill.showBillReport(); + break; + } + billInputInAction(bill); + } + } + + public static boolean isBillShareCorrect(String billShareCountString) { + if (isNumericInt(billShareCountString) == false) { + System.out.println("Введенные данные не являются целым числом! Введите значение еще раз!"); + return false; + } + int peopleCount = Integer.parseInt(billShareCountString); + switch (peopleCount) { + case 1: { + System.out.println("Деление счёта на одного человека не имеет смысла! Число должно быть целое, положительное, больше 1."); + return false; + } + case 0: { + System.out.println("На ноль делить нельзя! Число должно быть целое, положительное, больше 1."); + return false; + } + default: { + if (peopleCount> 0) { + System.out.println("Данные о количестве человек приняты."); + return true; + } else { + System.out.println("Наверное, это опечатка. Число должно быть целое, положительное, больше 1."); + return false; + } + } + } + } + + public static void billInputInAction(Bill bill) { + System.out.println("Введите название товара!"); + Scanner userReplyMealName = new Scanner(System.in); + String newMealName = userReplyMealName.nextLine(); + while (true) { + System.out.println("Введите стоимость товара в формате \"рубли.копейки\", например 10.45 или 11.40!"); + Scanner userReplyMealPrice = new Scanner(System.in); + String newMealPrice = userReplyMealPrice.nextLine(); + // Проверка введенных числовых данных + if (isNumericDouble(newMealPrice) == true) { + bill.billNewMealAdd(newMealName, newMealPrice); + System.out.println("\"" + newMealName + "\" успешно добавлено в счёт!"); + break; + } + System.out.println("Введенные данные не корректны! Введите значение еще раз!"); + } + } + + public static boolean isNumericDouble(String priceString) { + try { + Double.parseDouble(priceString); + return true; + } catch (NumberFormatException e) { + return false; + } + } + + public static boolean isNumericInt(String peopleCount) { + try { + Integer.parseInt(peopleCount); + return true; + } catch (NumberFormatException e) { + return false; + } } } \ No newline at end of file

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