Repository navigation
Первый Pull Request #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
VerionitKo
wants to merge
6
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
6 commits
Select commit
Hold shift + click to select a range
ff8ac8d
first commit
Verionit 4de528a
добавлены классы Calculator, Output
Verionit 4cf51c9
косметические изменения, переделан ввод цены в Main
Verionit f9f8c7a
added scanner close
Verionit 8388886
Ðизменен формат вывода, без массива
Verionit 96b1853
евынесены большие куски в отдельные функции статик
Verionit 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,39 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Calculator { | ||
|
|
||
| private List<String> items = new ArrayList<>(); | ||
| private final int humanCount; | ||
| private double totalPrice; | ||
|
|
||
| public Calculator(int humanCount) { | ||
| this.humanCount = humanCount; | ||
| totalPrice = 0; | ||
| } | ||
|
|
||
| public void addDish(String dish, Double price) { | ||
| this.items.add(dish); | ||
| totalPrice += price; | ||
| System.out.println("Товар " + dish + " успешно добавлен!"); | ||
| System.out.println("Сейчас общая сумма: " + getTotalPrice()); | ||
| } | ||
|
|
||
| public List<String> getItems() { | ||
| return items; | ||
| } | ||
|
|
||
| public String getTotalPrice() { | ||
| return String.format("%.2f", totalPrice); | ||
| } | ||
|
|
||
| public double getPriceForPerson() { | ||
| double result = 0; | ||
| try { | ||
| result = totalPrice / humanCount; | ||
| } catch (ArithmeticException ex) { | ||
| System.out.println("Внимание!! Деление на НОЛЬ!"); | ||
| } | ||
| return result; | ||
| } | ||
| } |
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,36 @@ | ||
| import java.util.InputMismatchException; | ||
| import java.util.Scanner; | ||
|
|
||
| public class InputLoopFunctions { | ||
| public static void addingItem(Scanner sc, Calculator calculator){ | ||
|
|
||
| String itemName; | ||
|
|
||
| while (true) { | ||
|
|
||
| System.out.println("Введите название товара (либо -> 'завершить' ): "); | ||
| itemName = sc.next(); | ||
|
|
||
| if (itemName.equalsIgnoreCase("завершить")) break; | ||
| else { | ||
| while (true) { | ||
| System.out.println("Введите стоимость товара в формате ХХ,ХХ (рубли,коп.): "); | ||
| try { | ||
| double itemPrice = sc.nextDouble(); | ||
| if (itemPrice <= 0) { | ||
| System.out.println("Цена должна быть положительным числом!"); | ||
| } else { | ||
| calculator.addDish(itemName, itemPrice); | ||
| break; | ||
| } | ||
| } catch (InputMismatchException ex) { | ||
| System.out.println("Формат ввода неверный!" + | ||
| " Введите дробное число через запятую!"); | ||
| sc.nextLine(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| sc.close(); | ||
| } | ||
| } |
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,44 @@ | ||
| import java.util.InputMismatchException; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
|
|
||
| Scanner sc = new Scanner(System.in); | ||
| String itemName; | ||
| int humanCount; | ||
| Calculator calculator; | ||
|
|
||
| System.out.println("Приветствую!\n Введите количество человек" + | ||
| " на сколько будем делить счет. Введите значение (больше '1'): "); | ||
|
|
||
| while (true) { //in separate static too | ||
|
|
||
| try { | ||
| humanCount = sc.nextInt(); | ||
| if (humanCount > 1) { | ||
| calculator = new Calculator(humanCount); | ||
| break; | ||
| } | ||
|
|
||
| } catch (InputMismatchException ex) { | ||
| System.out.println("ERROR! Введите число! "); | ||
| sc.nextLine(); | ||
| } | ||
| System.out.println("!!! Число должно быть больше '1'! "); | ||
| } | ||
|
|
||
| InputLoopFunctions.addingItem(sc, calculator); | ||
|
|
||
| //sc.close(); | ||
|
|
||
| System.out.println("Добавленные товары: "); | ||
|
|
||
| for (String item : calculator.getItems()) { | ||
| System.out.println(" ╰┈➤ " + item + " ** "); | ||
|
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. какая красивая стрелка) |
||
| } | ||
|
|
||
| Output finalOutput = new Output(); | ||
| finalOutput.showMessage(calculator.getPriceForPerson()); | ||
| } | ||
| } | ||
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,18 @@ | ||
| public class Output { | ||
| public static void showMessage(double personalPrice) { | ||
|
|
||
| if ((int) personalPrice % 100 >= 10 && (int) personalPrice % 100 <= 20) { | ||
| System.out.printf("Каждый человек должен заплатить %.2f рублей", | ||
| personalPrice); | ||
| } else { | ||
| switch ((int) personalPrice % 10) { | ||
| case 1 -> System.out.printf("Каждый человек должен заплатить %.2f рубль", | ||
| personalPrice); | ||
| case 2, 3, 4 -> System.out.printf("Каждый человек должен заплатить %.2f рубля", | ||
| personalPrice); | ||
| default -> System.out.printf("Каждый человек должен заплатить %.2f рублей", | ||
| personalPrice); | ||
| } | ||
| } | ||
| } | ||
| } |
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.
Отсюда можно вынести очень хорошую статическую функцию для считывания целого числа, большего единицы