forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Пишем консольное приложение №1 #1
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
mfoxtrot
wants to merge
7
commits into
main
Choose a base branch
from
dev
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e6fb7da
Add dev branch
358bda0
add Dish class
mfoxtrot 03c266b
add Bill class
mfoxtrot 6bb59f1
add Calculator class
mfoxtrot 837308e
input data & output result
mfoxtrot a2b8d3e
fix after code review
mfoxtrot 4b9997c
fix: price should be greater than zero
mfoxtrot 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
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,8 +1,123 @@ | ||
| // dev branch for Y.Practicum | ||
| import java.lang.invoke.ConstantCallSite; | ||
| import java.util.Scanner; | ||
|
|
||
| class Dish { | ||
| String name; | ||
| double price; | ||
|
|
||
| Dish(String name, double price) { | ||
| this.name = name; | ||
| this.price = price; | ||
| } | ||
| } | ||
|
|
||
| class Bill { | ||
| int numberOfPersons; | ||
| String listOfDishes; | ||
| double totalSum; | ||
|
|
||
| Bill(int numberOfPersons) { | ||
| this.numberOfPersons = numberOfPersons; | ||
| this.listOfDishes = ""; | ||
| this.totalSum = 0; | ||
| } | ||
|
|
||
| public void addDish(Dish dish) { | ||
| this.listOfDishes = this.listOfDishes + (this.listOfDishes.length()==0 ? "" : "\n") + dish.name; | ||
| this.totalSum += dish.price; | ||
| System.out.println("Блюдо " + dish.name + " добавлено в чек"); | ||
| } | ||
|
|
||
| public void print() { | ||
| System.out.println("Добавленные товары:"); | ||
| System.out.println(listOfDishes); | ||
| } | ||
| } | ||
|
|
||
| class Calculator { | ||
| Bill bill; | ||
|
|
||
| Calculator(Bill bill) { | ||
| this.bill = bill; | ||
| } | ||
| public double payment() { | ||
| return bill.totalSum/bill.numberOfPersons; | ||
| } | ||
|
|
||
| public String formattedOutPut() { | ||
| double payment = this.payment(); | ||
| return String.format("%.2f " + this.rubbleProperCase(payment), payment); | ||
| } | ||
|
|
||
| private String rubbleProperCase(Double sum) { | ||
| int intSum = (int)Math.floor(sum); | ||
|
|
||
| if (intSum%10==1 && intSum%100!=11) { | ||
| return "рубль"; | ||
| } | ||
| else if (intSum%10==2 && intSum%100!=12) { | ||
| return "рубля"; | ||
| } | ||
| else if (intSum%10==3 && intSum%100!=13) { | ||
| return "рубля"; | ||
| } | ||
| else if (intSum%10==4 && intSum%100!=14) { | ||
| return "рубля"; | ||
| } | ||
| else { | ||
| return "рублей"; | ||
| } | ||
| } | ||
| } | ||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
| Bill bill; | ||
|
|
||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| //Получаем количество гостей | ||
| while (true) { | ||
| System.out.print("На сколько гостей разбить чек? "); | ||
| if (scanner.hasNextInt()) { | ||
| int numberOfPersons = scanner.nextInt(); | ||
| if (numberOfPersons > 1) { | ||
| bill = new Bill(numberOfPersons); | ||
| break; | ||
| } | ||
| } | ||
| scanner.nextLine(); | ||
| System.out.println("Неверное значение. Количество гостей должно быть больше 1. Попробуйте еще раз."); | ||
| } | ||
| scanner.nextLine();//Вынужденная мера для обхода ошибки https://stackoverflow.com/questions/23450524/java-scanner-doesnt-wait-for-user-input | ||
|
|
||
| while (true) { | ||
| System.out.print("Введите название блюда или команду Завершить: "); | ||
| String inputString = scanner.nextLine(); | ||
| if (inputString.equalsIgnoreCase("завершить")) { | ||
| break; | ||
| } | ||
|
|
||
| while (true) { | ||
| System.out.print("Введите цену блюда: "); | ||
| if (scanner.hasNextDouble()) { | ||
| double inputPrice = scanner.nextDouble(); | ||
| if (inputPrice>0) { | ||
| Dish dish = new Dish(inputString, inputPrice); | ||
| bill.addDish(dish); | ||
| break; | ||
| } | ||
| } | ||
| scanner.nextLine(); | ||
| } | ||
| scanner.nextLine();//аналогично, комментарию выше | ||
| } | ||
|
|
||
| bill.print();//выводим счет на экран | ||
| Calculator calc = new Calculator(bill); | ||
| System.out.println(calc.formattedOutPut());//выводим итог расчета | ||
| scanner.close(); | ||
| } | ||
| } | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🍏 В данной ситуации это нормально