forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Task1 #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
yakoeka
wants to merge
3
commits into
dev
Choose a base branch
from
task1
base: dev
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
Task1 #1
Changes from all commits
Commits
Show all changes
3 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,78 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.InputMismatchException; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Calculator { | ||
| int guests; | ||
| double sum; | ||
| ArrayList<Product> productsList = new ArrayList<>(); | ||
|
|
||
| Calculator(int guests) { | ||
| this.guests = guests; | ||
| } | ||
|
|
||
| static int getAmountOfGuests(Scanner scanner) { | ||
| int guests; | ||
|
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("Введите количество гостей:"); | ||
| while (true) { | ||
| try { | ||
| guests = scanner.nextInt(); | ||
| } catch (InputMismatchException e) { | ||
| System.out.println("Неверный тип вводимого значения"); | ||
| // Очистка символа новой строки из буфера ввода | ||
| scanner.nextLine(); | ||
| continue; | ||
| } | ||
| if (guests > 1) break; | ||
| else System.out.println("Некорректное значение для подсчёта"); | ||
| } | ||
| return guests; | ||
| } | ||
|
|
||
| double addProductAndGetSum(Calculator calculator, Scanner scanner) { | ||
| double sum = 0; | ||
| // Очистка символа новой строки из буфера ввода | ||
| scanner.nextLine(); | ||
| while (true) { | ||
| System.out.println("Введите название товара:"); | ||
| String name = scanner.nextLine(); | ||
|
|
||
| double price; | ||
| do { | ||
| System.out.println("Введите стоимость товара:"); | ||
| try { | ||
| price = scanner.nextDouble(); | ||
| } catch (InputMismatchException e) { | ||
| System.out.println("Неверный тип вводимого значения"); | ||
| // Очистка символа новой строки из буфера ввода | ||
| scanner.nextLine(); | ||
| continue; | ||
| } | ||
| break; | ||
| } while (true); | ||
| Product product = new Product(name, price); | ||
| calculator.productsList.add(product); | ||
| sum += price; | ||
| System.out.println(String.format("Товар %s успешно добавлен", name)); | ||
| System.out.println("Хотите ли добавить еще один товар?"); | ||
| // Очистка символа новой строки из буфера ввода | ||
| scanner.nextLine(); | ||
| String word = scanner.nextLine(); | ||
| if ("завершить".equalsIgnoreCase(word)) break; | ||
| } | ||
| return sum; | ||
| } | ||
|
|
||
| void printAllProducts(Calculator calculator) { | ||
| System.out.println("Добавленные товары:"); | ||
| for (Product element : calculator.productsList) { | ||
| System.out.println(element.name); | ||
| } | ||
| } | ||
|
|
||
| void printResultingSum(Calculator calculator) { | ||
| double resultSum = calculator.sum / calculator.guests; | ||
| Formatter formatter = new Formatter(); | ||
| System.out.println(String.format("%.2f %s", resultSum, formatter.getDeclension(resultSum))); | ||
| } | ||
| } | ||
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,25 @@ | ||
| public class Formatter { | ||
| String getDeclension(double price) { | ||
| int resultSumIntDiv10 = ((int) price) % 10; | ||
| String rub = ""; | ||
| switch (resultSumIntDiv10) { | ||
| case 1: | ||
| rub = "рубль"; | ||
| break; | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| rub = "рубля"; | ||
| break; | ||
| case 5: | ||
| case 6: | ||
| case 7: | ||
| case 8: | ||
| case 9: | ||
| case 0: | ||
| rub = "рублей"; | ||
| break; | ||
| } | ||
| return rub; | ||
| } | ||
| } |
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,13 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| int guests = Calculator.getAmountOfGuests(scanner); | ||
| Calculator calc = new Calculator(guests); | ||
| calc.sum = calc.addProductAndGetSum(calc, scanner); | ||
| scanner.close(); | ||
| calc.printAllProducts(calc); | ||
| calc.printResultingSum(calc); | ||
| } | ||
| } |
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,9 @@ | ||
| public class Product { | ||
| String name; | ||
| double price; | ||
|
|
||
| Product(String name, double price) { | ||
| this.name = name; | ||
| this.price = price; | ||
| } | ||
| } |
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.
старайся использовать нестатические функции, так так статические функции постоянно висят в памяти и соответственно занимают её, чего стоит избегать, иначе на устройстве может банально закончится память и твоё приложение закроется с ошибкой