Repository navigation
выполненное задание. Примечание: реализовал суммы с копейками вместо … #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
David-Arcanone
wants to merge
2
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
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 +1,2 @@ | ||
| # Пустой репозиторий для работы с Java кодом в Android Studio | ||
| # Задание Яндекс Практикум | ||
| Выполнил Давид |
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,138 @@ | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| CurrencyFormatter myCurrencyFormatter = new CurrencyFormatter(); | ||
| System.out.println("На сколько людей разделить сумму?: "); | ||
| Calculator myCalculator = new Calculator(myCurrencyFormatter.nextPositiveIntegerHigherThan1FromSystem(scanner)); | ||
| String stopWord = ""; | ||
| while (true) { | ||
| String itemName; | ||
| System.out.println("Введите наименование вашего товара: "); | ||
| itemName = scanner.nextLine(); | ||
| System.out.println("Имя товара принято"); | ||
| System.out.println("Введите стоимость вашего товара: "); | ||
| myCalculator.addNewItemToTheList(itemName, myCurrencyFormatter.nextPositiveDoubleHigherThan0FromSystem(scanner)); | ||
| System.out.println("Введите \"Завершить\", если закончили с составлением списка товаров, введите любой другой текст, если хотите ввести еще товар:"); | ||
| stopWord = scanner.nextLine(); | ||
| if (stopWord.equalsIgnoreCase("Завершить")) { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| System.out.println("Добавление товаров завершено\nДобавленные товары:"); | ||
| myCalculator.showItems(); | ||
| System.out.println("Сумма которую должен заплатить каждый человек:" + myCurrencyFormatter.showPrice(myCalculator.returnCheck())); | ||
| scanner.close(); | ||
| } | ||
| } | ||
|
|
||
| class Calculator { | ||
| Integer amountOfPeople; | ||
| Double totalMoney; | ||
| ArrayList<Item> itemList = new ArrayList<>(); | ||
|
|
||
| Calculator(Integer people) { | ||
| amountOfPeople = people; | ||
| totalMoney = 0.0; | ||
| } | ||
|
|
||
| void addNewItemToTheList(String name, Double price) { | ||
| itemList.add(new Item(name, price)); | ||
| System.out.println("Товар добавлен в список"); | ||
| totalMoney += price; | ||
| } | ||
|
|
||
| void showItems() { | ||
| for (int i = 0; i < itemList.size(); i++) { | ||
| System.out.println((i + 1) + "-товар: " + itemList.get(i).name); | ||
| } | ||
| } | ||
|
|
||
| double returnCheck() { | ||
| return totalMoney / amountOfPeople; | ||
| } | ||
| } | ||
|
|
||
| class CurrencyFormatter { | ||
| String showPrice(double price) { | ||
| int integerPartOfPrice = (int) price; | ||
| int changeValue = (int) ((price * 100) % 100); //валюты меньше копейки нет | ||
| String currencyPronunciation = " рублей "; | ||
| String changePronunciation = " копеек"; | ||
| if ((integerPartOfPrice % 10) == 1) { | ||
| currencyPronunciation = " рубль "; | ||
| } | ||
| if (((integerPartOfPrice % 10) > 1) && ((integerPartOfPrice % 10) <= 4)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. integerPartOfPrice % 10 -считается несколько раз. Можно вынести в переменную |
||
| currencyPronunciation = " рубля "; | ||
| } | ||
| if (10 < ((integerPartOfPrice % 100)) && ((integerPartOfPrice % 100) < 15)) { | ||
| currencyPronunciation = " рублей "; | ||
| } | ||
| if ((changeValue % 10) == 1) { | ||
| changePronunciation = " копейка"; | ||
| } | ||
| if (((changeValue % 10) > 1) && ((changeValue % 10) <= 4)) { | ||
| changePronunciation = " копейки"; | ||
| } | ||
| if ((10 < changeValue) && (changeValue < 15)) { | ||
| changePronunciation = " копеек"; | ||
| } | ||
| return (" " + integerPartOfPrice + currencyPronunciation + changeValue + changePronunciation); | ||
| } | ||
|
|
||
| Integer nextPositiveIntegerHigherThan1FromSystem(Scanner sn) { | ||
| String myLine; | ||
| Integer myNumber = null; | ||
| boolean incorrectInput = true; | ||
| while (incorrectInput) { | ||
| myLine = sn.nextLine(); | ||
| try { | ||
| myNumber = Integer.parseInt(myLine); | ||
| } catch (NumberFormatException e) { | ||
| System.err.println("Неадекватное значение. Вводите только целые числа больше 1: " + e.getMessage()); | ||
| continue; | ||
| } | ||
| if (myNumber > 1) { | ||
| incorrectInput = false; | ||
| } else { | ||
| System.err.println("Неадекватная величина целого числа. Вводите только целые числа больше 1: " + myLine); | ||
| } | ||
| } | ||
| return myNumber; | ||
| } | ||
|
|
||
| Double nextPositiveDoubleHigherThan0FromSystem(Scanner sn) { | ||
| String myLine; | ||
| Double myNumber = null; | ||
| boolean incorrectInput = true; | ||
| while (incorrectInput) { | ||
| myLine = sn.nextLine(); | ||
| try { | ||
| myNumber = Double.parseDouble(myLine); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. также удобно использовать hasNexDouble() |
||
| if (myNumber > 0.0) { | ||
| incorrectInput = false; | ||
| } else { | ||
| System.err.println("Неадекватная величина дробного числа. Вводите только дробные числа больше 0: " + myLine); | ||
| } | ||
| } catch (NumberFormatException e) { | ||
| System.err.println("Неадекватное значение. Вводите только дробные числа больше 0: " + e.getMessage()); | ||
| continue; | ||
| } | ||
| } | ||
| return myNumber; | ||
| } | ||
| } | ||
|
|
||
| class Item { | ||
| String name; | ||
| Double price; | ||
|
|
||
| Item(String itemName, Double itemPrice) { | ||
| name = itemName; | ||
| price = itemPrice; | ||
| } | ||
| } | ||
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.
Рекомендация: - Форматирование кода - в Java принят немного другой стиль форматирования кода. Если не вдаваться в детали, то легко и быстро отформатировать код в Android Studio можно следующей комбинацией клавиш: в Windows Ctrl + Alt + L , в MacOs ⌘ + ⌥ + L.