forked from Yandex-Practicum/Java-Module-Project-YP
-
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
Open
Extro21
wants to merge
1
commit into
main
Choose a base branch
from
calc_dz
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 |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Calculate { | ||
|
|
||
| private final Scanner scanner = new Scanner(System.in); | ||
|
|
||
| String check = ""; | ||
| Double countProducts = 0.0; | ||
|
|
||
| public void calculate(String name) { | ||
| ArrayList<Product> products = new ArrayList<>(); | ||
|
|
||
| System.out.println(name + " Напишите название блюда"); | ||
| while (true) { | ||
| String productName = scanner.next(); | ||
|
|
||
| if (productName.equalsIgnoreCase("Завершить")) { | ||
| if (products.size() == 0) { | ||
| System.out.println("У " + name + " не добавлено ни одного блюда"); | ||
| } else { | ||
| System.out.println("Добавденные товары: "); | ||
| for (int j = 0; j < products.size(); j++) { | ||
| double temp2 = products.get(j).count; | ||
| int convertRub2 = (int) temp2; | ||
| System.out.println(products.get(j).name + " - " + products.get(j).count + " " + convertRubDeclination(convertRub2)); | ||
| } | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| if (checkString(productName)) { | ||
| System.out.println("Вы ввели некорректное название блюда"); | ||
| continue; | ||
| } | ||
|
|
||
| System.out.println(name + " Напишите цену блюда"); | ||
| double productPrice; | ||
| while (true) { | ||
| try { | ||
| productPrice = Double.parseDouble(scanner.next()); | ||
| if (productPrice <= 0) { | ||
| System.out.println("Вы ввели минусовую цену. Попробуйте ще раз."); | ||
| continue; | ||
| } | ||
| break; | ||
| } catch (Exception e) { | ||
| System.out.println("Вы ввели некорректное значение цены. Попробуйте еще раз."); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| products.add(new Product(productName, productPrice)); | ||
| double temp1 = productPrice; | ||
| int convertRub = (int) temp1; | ||
| System.out.println("Вы успешно добавили блюдо " + "\"" + productName + "\"" + " стоимостью " + productPrice + " " + convertRubDeclination(convertRub) + " в счет"); | ||
| countProducts += productPrice; | ||
| check += productName + " - " + productPrice + " " + convertRubDeclination(convertRub) + "\n"; | ||
|
|
||
| System.out.println("Хотите ли вы добавить ещё один товар?\nЕсли нет, напишите - Завершить, если хотите продолжить, напишите название следующего блюда."); | ||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
| public String convertRubDeclination(int number) { | ||
| int lastDigit = number % 100 / 10; | ||
| if (lastDigit == 1) { | ||
| return "рублей"; | ||
| } | ||
|
|
||
| switch (number % 10) { | ||
| case 1: | ||
| return "рубль"; | ||
| case 2: | ||
| case 3: | ||
| case 4: | ||
| return "рубля"; | ||
| default: | ||
| return "рублей"; | ||
| } | ||
| } | ||
|
|
||
| private boolean checkString(String s) throws NumberFormatException { | ||
| try { | ||
| Integer.parseInt(s); | ||
| return true; | ||
| } catch (NumberFormatException e) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| } |
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,8 +1,5 @@ | ||
| public class Main { | ||
|
|
||
| public static void main(String[] args) { | ||
| // ваш код начнется здесь | ||
| // вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости | ||
| System.out.println("Привет Мир"); | ||
| Person.start(); | ||
| } | ||
| } |
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,46 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Person { | ||
|
|
||
| private static final Scanner scanner = new Scanner(System.in); | ||
| private static final ArrayList<String> persons = new ArrayList<>(); | ||
| private static final Calculate calculate = new Calculate(); | ||
|
|
||
| public static void start() { | ||
| System.out.println("На скольких человек необходимо разделить счёт? Введите число."); | ||
|
|
||
| while (true) { | ||
| int countPeople; | ||
|
|
||
| try { | ||
| countPeople = Integer.parseInt(scanner.next()); | ||
| } catch (Exception e) { | ||
| System.out.println("Вы ввели некорректное значение. Введите целое число."); | ||
| continue; | ||
| } | ||
|
|
||
| if (countPeople == 1) { | ||
| System.out.println("Количество гостей должно быть больше одного"); | ||
| } else if (countPeople < 1) { | ||
| System.out.println("Количество гостей должно быть больше одного"); | ||
| } else { | ||
| for (int i = 0; i < countPeople; i++) { | ||
| persons.add("Гость_" + (i + 1)); | ||
| } | ||
|
|
||
| System.out.println("Счет будет разделён на " + countPeople + " человек"); | ||
| for (int i = 0; i < persons.size(); i++) { | ||
| calculate.calculate(persons.get(i)); | ||
| } | ||
| double result = calculate.countProducts / persons.size(); | ||
| int rubText = (int) result; | ||
| System.out.printf("\nДобавленные блюда всех гостей:\n%s \nСумма к оплате с каждого гостя: %.2f %s", calculate.check, result, calculate.convertRubDeclination(rubText)); | ||
| break; | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
|
|
||
| } | ||
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,12 @@ | ||
| public class Product { | ||
|
|
||
| String name; | ||
| Double count; | ||
|
|
||
| public Product(String name, Double count) { | ||
| this.name = name; | ||
| this.count = count; | ||
| } | ||
|
|
||
|
|
||
| } |
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.
Некритично, но можно поля и методы Calculate тоже сделать static, чтобы не пришлось создавать единственный экземпляр