-
Notifications
You must be signed in to change notification settings - Fork 0
первый ПР #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
base: main
Are you sure you want to change the base?
первый ПР #1
Changes from all commits
ed457f2
c9d563d
238ce96
0f2b0ad
d3ecd23
0751b64
83a80c8
f77dbec
619607f
1dcc413
cc48455
96771fd
9c51a9c
a4a19d9
c6da514
884c99d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| # Пустой репозиторий для работы с Java кодом в Android Studio | ||
| Курсовая работа Шаймарданов Марсель |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Calculator { | ||
| static int persons = 0; | ||
| static String menuSum = ""; | ||
| static double priceSum = 0; | ||
| public static void calculator() { | ||
|
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. Имена функций должны содержать в себе действие. Функция делает что-либо. Хорошо помогает практика переводить имена функций дословно, если после перевода понятно чем занимается функция, то название хорошее |
||
| Scanner scanner = new Scanner(System.in); | ||
| System.out.println("Введите количество человек:"); | ||
| while (true) { | ||
| if (scanner.hasNextInt()) { | ||
| persons = scanner.nextInt(); | ||
| if (persons < 2) { | ||
| System.out.println("Введите число больше 1"); | ||
| } else { | ||
| break; | ||
| } | ||
| } else { | ||
| System.out.println("Введите целое число "); | ||
| scanner.next(); | ||
| } | ||
| } | ||
| while (true) { | ||
| System.out.println("Введите название товара или введите 'Завершить', что бы выйти"); | ||
| scanner.nextLine(); | ||
| String menu = scanner.nextLine(); | ||
| if (menu.equalsIgnoreCase("Завершить")) { | ||
| System.out.println("Товар успешно добавлен."); | ||
| break; | ||
| } | ||
| menuSum += menu + "\n"; | ||
| while (true) { | ||
| System.out.println("Введите цену товара:"); | ||
| if (scanner.hasNextDouble()) { | ||
| double price = scanner.nextDouble(); | ||
| if (price > 0) { | ||
| priceSum += price; | ||
| break; | ||
| } else { | ||
| System.out.println("Введите число больше 0"); | ||
| } | ||
| } else { | ||
| System.out.println("Введите число"); | ||
| scanner.next(); | ||
|
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. Рекомендация: - хорошей практикой является делать вызов scanner.close() после того, как сканнер больше не используется. Это необходимо для того, что бы этот объект не потреблял ресурсы, тогда когда это уже не требуется. |
||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| public class FormatRub { | ||
|
|
||
| static double rubAverage = Calculator.priceSum / Calculator.persons; | ||
|
|
||
| public static String formatRub(){ | ||
| int priceRemainder = (int)Math.floor(rubAverage); | ||
| if (priceRemainder % 10==1) | ||
|
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. В ситуациях, когда у тебя есть более 2-3 условий лучше использовать оператор switch-case - это улучшит читабельность кода и его легче будет расширить или изменить в дальнейшем |
||
| return " рубль"; | ||
| else if (priceRemainder % 10==2 || priceRemainder % 10==3 || priceRemainder % 10==4) | ||
| return " рубля"; | ||
| else | ||
| return " рублей"; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,9 @@ | ||
|
|
||
| import java.util.Scanner; | ||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| Calculator.calculator(); | ||
| String formatText = "Добавленные товары:\n%sКаждый человек должен заплатить: %.2f %s\n"; | ||
| System.out.printf(formatText, Calculator.menuSum, FormatRub.rubAverage, FormatRub.formatRub()); | ||
|
|
||
| } | ||
| } | ||
| } |
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.
старайся по возможности не использовать статические переменные, они живут столько же, сколько живет само приложение. В этом месте можно вполне использовать без модификатора static