Repository navigation
Первая домашка: 4 попытка #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
ShKeril
wants to merge
5
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
5 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
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,58 @@ | ||
| public class Calculation { | ||
|
|
||
| String productList = "Добавленные товары:"; | ||
| int numberPersons; | ||
| double totalPrice; | ||
|
|
||
| Calculation(int numberPersons) { | ||
| this.numberPersons = numberPersons; | ||
| } | ||
|
|
||
| public void addProduct(String product, double price) { | ||
| productList = String.format("%s\n%s стоимость: %.2f %s", productList, product, price, getEnding(price)); | ||
| totalPrice = totalPrice + price; | ||
| } | ||
|
|
||
| public String divideCheck() { | ||
|
|
||
| if (numberPersons == 0) { | ||
| return "Ошибка: нет персон"; | ||
| } | ||
|
|
||
| double result = totalPrice / numberPersons; | ||
|
|
||
| return String.format("С одного человека: %.2f %s", result, getEnding(result)); | ||
| } | ||
|
|
||
| public String getEnding(double number){ | ||
|
|
||
| int lastNumber = (int) Math.floor(number); | ||
|
|
||
| if (lastNumber > 100) { | ||
| lastNumber = lastNumber % 100; | ||
| } | ||
|
|
||
| if (lastNumber > 20) { | ||
| lastNumber = lastNumber % 10; | ||
| } | ||
|
|
||
| String ending; | ||
|
|
||
| switch (lastNumber) { | ||
| case 1: | ||
| ending = "рубль"; | ||
| break; | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| ending = "рубля"; | ||
| break; | ||
| default: | ||
| ending = "рублей"; | ||
| break; | ||
| } | ||
|
|
||
| return ending; | ||
|
|
||
| } | ||
| } |
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,62 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
|
|
||
| System.out.println("На сколько человек необходимо разделить счёт?"); | ||
|
|
||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| int numberPersons; | ||
|
|
||
| while (true) { | ||
| if (scanner.hasNextInt()) { | ||
| numberPersons = scanner.nextInt(); | ||
| if (numberPersons > 1) { | ||
| break; | ||
| } else { | ||
| System.out.println("Ошибка! Введено меньше 2. Введите корректное количество гостей. Для закрытия программы напишите \"выход\""); | ||
| } | ||
| } else { | ||
| String exit = scanner.next(); | ||
| if (exit.equalsIgnoreCase("выход")) { | ||
| System.out.println("Завершена работы программы!"); | ||
| return; | ||
| } else { | ||
| System.out.println("Ошибка! Введено не число. Введите корректное количество гостей. Для закрытия программы напишите \"выход\""); | ||
| scanner.nextLine(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Calculation calculation = new Calculation(numberPersons); | ||
|
|
||
| while (true) { | ||
|
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. 🍏 Рекомендую разделять большие методы на методы поменьше. Например, можно вынести этот цикл в отдельный метод |
||
| System.out.println("Введите название товара. Для закрытия программы напишите \"завершить\""); // можно сделать через useDelimeter(). думаю пока как обработать несколько пробелов | ||
| String product = scanner.next(); | ||
| if (product.equalsIgnoreCase("завершить")) { | ||
| break; | ||
| } else { | ||
| while (true) { | ||
| System.out.println("Введите стоимость товара (руб,коп)."); | ||
| try { | ||
| double price = scanner.nextDouble(); | ||
| if (price > 0) { | ||
| calculation.addProduct(product, price); | ||
| break; | ||
| } else { | ||
| System.out.println("Ошибка! Введено отрицательное значение!"); | ||
| } | ||
| } catch (Exception err){ | ||
| System.out.println("Ошибка! Введено не число"); | ||
| scanner.nextLine(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| System.out.println(calculation.productList); | ||
| System.out.println(calculation.divideCheck()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
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.
🍏 Немного поехало форматирование. Для форматирования кода и удаления неиспользуемых импортов применяй следующие сочетания клавиш:
Windows: Control+Alt+L
MacOS: Command+Option+L
Больше горячих клавиш можно найти здесь: https://developer.android.com/studio/intro/keyboard-shortcuts