forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Дз #5
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
Дз #5
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f52e588
ДЗ
Trofiproll a906bb0
дз попытка n
Trofiproll af0e03d
ДЗ
Trofiproll 1d61b20
Merge remote-tracking branch 'origin/dev' into dev
Trofiproll 4b30411
ДЗ
Trofiproll dd832f3
Исправлен "человек", исправлено добавление
Trofiproll 02911fb
Добавлена переменная класса с общей суммой товаров
Trofiproll 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
73 changes: 73 additions & 0 deletions
src/main/java/Calculator.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,73 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Calculator { | ||
| private int guestsNumber; | ||
| private double totalSum = 0; | ||
| private ArrayList<String> items; | ||
| private ArrayList<Double> prices; | ||
|
|
||
|
|
||
|
|
||
| Calculator(int guestsNumber){ | ||
| this.guestsNumber = guestsNumber; | ||
| items = new ArrayList<>(); | ||
| prices = new ArrayList<>(); | ||
| String persons; | ||
| if(guestsNumber >= 11 && guestsNumber <= 14) persons = "человек"; | ||
| else persons = switch (guestsNumber%10){ | ||
| case 1,2,3,4 -> "человека"; | ||
| default -> "человек"; | ||
| }; | ||
| System.out.printf("Счёт на %d %s создан!\n" + | ||
| "Добавляйте товары в формате \"<Товар> <стоимость>\".\n" + | ||
| "Название товара может состоять из нескольких слов.\n" + | ||
| "Для завершения добавления введите \"Завершить\".\n\n", guestsNumber, persons); | ||
| } | ||
|
|
||
| void addProducts(){ | ||
| Scanner in = new Scanner(System.in); | ||
| String inputItem = ""; | ||
| double inputPrice; | ||
| while(true){ | ||
| while(in.hasNext() && !in.hasNextDouble()){ | ||
| if(inputItem.isEmpty()) inputItem += in.next(); | ||
| else inputItem += ' ' + in.next(); | ||
| if(inputItem.equalsIgnoreCase("завершить")){ | ||
| System.out.println("Добавление товаров завершено"); | ||
| return; | ||
| } | ||
| } | ||
| if(in.hasNextDouble()){ | ||
| if(!inputItem.isEmpty()){ | ||
| inputPrice = in.nextDouble(); | ||
| items.add(inputItem); | ||
| prices.add(inputPrice); | ||
| totalSum += inputPrice; | ||
| System.out.printf("Товар '%s' по цене %.2f успешно добавлен\n", inputItem, inputPrice); | ||
| inputItem = ""; | ||
| } | ||
| else in.next(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void calcutate(){ | ||
| System.out.println("\nДобавленные товары:"); | ||
| for(int i = 0; i < items.size(); i++){ | ||
| System.out.printf("%s %.2f\n", items.get(i), prices.get(i)); | ||
| } | ||
| System.out.printf("\nВсего на %.2f %s.\n", totalSum, rouble(totalSum)); | ||
| double priceForEach = totalSum / guestsNumber; | ||
| System.out.printf("\nС каждого %.2f %s.\n", priceForEach, rouble(priceForEach)); | ||
| } | ||
|
|
||
| private String rouble(double price){ | ||
| if(price > 10 && price < 15) return "рублей"; | ||
| else return switch ((int)price % 10){ | ||
| case 1 -> "рубль"; | ||
| case 2, 3, 4 -> "рубля"; | ||
| default -> "рублей"; | ||
| }; | ||
| } | ||
| } |
25 changes: 24 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,29 @@ | ||
| import java.util.Locale; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| Locale.setDefault(Locale.ENGLISH); | ||
|
|
||
| Calculator bill = new Calculator(parseGuestNumber()); | ||
| bill.addProducts(); | ||
| bill.calcutate(); | ||
| } | ||
|
|
||
| private static int parseGuestNumber(){ | ||
| Scanner in = new Scanner(System.in); | ||
| System.out.println("На сколько человек нужно разделить счёт?"); | ||
| int guestsNumber; | ||
| while(true) { | ||
| if (in.hasNextInt()) { | ||
| guestsNumber = in.nextInt(); | ||
| if(guestsNumber > 1) break; | ||
| else System.out.println("Введено число меньше 2. Повторите попытку"); | ||
| } else if (in.hasNext()) { | ||
| System.out.println("Некорректный ввод. Повторите попытку"); | ||
| in.next(); | ||
| } | ||
| } | ||
| return guestsNumber; | ||
| } | ||
| } |
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.