forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Mihim dev 2 #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Mihim dev 2 #2
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
src/main/java/Bill.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 "рублей"; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
92 changes: 91 additions & 1 deletion
src/main/java/Main.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.