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
SAndreyV1683
wants to merge
7
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
Pull Request #1
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7c051aa
Входные параметры для счётчика
7e21341
Добавление товаров в калькулятор
235c1c2
форматирование строки
faac6c1
Упаковка кода в классы
61b36be
обработка отрицательного значения
b81c283
исправлены замечания
56a83c8
исправлены замечания 2
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,25 @@ | ||
| public class Calculator { | ||
| private String result = ""; | ||
| private float sum; | ||
| public String getResult() { | ||
| return result; | ||
| } | ||
|
|
||
| public float getSum() { | ||
| return sum; | ||
| } | ||
|
|
||
| public void showRequest() { | ||
| System.out.println("Введите название товара и его стоимость.\nСтоимость должна быть в формате \"рубли,копейки\", например 10,45 или 11,40"); | ||
| } | ||
|
|
||
| public void addProduct(Product product) { | ||
| result += product.getName() + "\n"; | ||
| calculate(product); | ||
| } | ||
|
|
||
| public void calculate(Product product) { | ||
| sum += product.getPrice(); | ||
| } | ||
|
|
||
| } | ||
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,23 @@ | ||
| public class Formatter { | ||
|
|
||
| private final float sum; | ||
|
|
||
| public Formatter(float sum) { | ||
| this.sum = sum; | ||
| } | ||
|
|
||
| public String getFormattedString() { | ||
| int i = (int) sum; | ||
|
|
||
| String messageTemplate = "%.2f %s"; | ||
| if (i < 2) { | ||
| return String.format(messageTemplate, this.sum, "рубль"); | ||
| } else if (i < 5) { | ||
| return String.format(messageTemplate, this.sum, "рубля"); | ||
| } else { | ||
| return String.format(messageTemplate, this.sum, "рублей"); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } |
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,70 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class MScanner { | ||
| private int peopleCount; | ||
| private Scanner scanner; | ||
| private final Calculator calculator; | ||
|
|
||
| public MScanner(Calculator calculator) { | ||
| scanner = new Scanner(System.in); | ||
| this.calculator = calculator; | ||
| } | ||
|
|
||
| public void setPeopleCount() { | ||
| System.out.println("На скольких человек необходимо разделить счёт? \nВведите значение:"); | ||
| boolean isNumber = false; | ||
| while (!isNumber) { | ||
| if (scanner.hasNextInt()) { | ||
| peopleCount = scanner.nextInt(); | ||
| if (peopleCount < 1) { | ||
| System.out.println("Это некорректное значение для подсчёта. \nВведите значение заново."); | ||
| scanner = new Scanner(System.in); | ||
| } else { | ||
| isNumber = true; | ||
| } | ||
| } else { | ||
| System.out.println("Это некорректное значение для подсчёта. Введите число"); | ||
| scanner = new Scanner(System.in); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void setPrice() { | ||
| String complete; | ||
| do { | ||
| System.out.println("Введите название товара."); | ||
| String name = scanner.next(); | ||
| System.out.println("Введите стоимость товара."); | ||
| float price = getPrice(); | ||
| Product product = new Product(name, price); | ||
| calculator.addProduct(product); | ||
| System.out.println("Введите любой символ что бы продолжить или введите \"Завершить\" что бы ЗАВЕРШИТЬ"); | ||
| complete = scanner.next(); | ||
| } while (!complete.equalsIgnoreCase("Завершить")); | ||
| } | ||
|
|
||
| private float getPrice() { | ||
| float price = 0; | ||
| boolean isFloat = false; | ||
| do { | ||
| if (scanner.hasNextFloat()){ | ||
| price = scanner.nextFloat(); | ||
| if(price <= 0) { | ||
| System.out.println("Стоимость не может быть отрицательной или нулевой. Введите стоимость заново."); | ||
| scanner = new Scanner(System.in); | ||
| } else { | ||
| isFloat = true; | ||
| } | ||
| } else { | ||
| System.out.println("Не верный формат ввода. Введите в формате рубли,копейки, например 10,45"); | ||
| scanner = new Scanner(System.in); | ||
| } | ||
| } while (!isFloat); | ||
|
|
||
| return price; | ||
| } | ||
|
|
||
| public int getPeopleCount() { | ||
| return peopleCount; | ||
| } | ||
| } |
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,17 @@ | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| Calculator calculator = new Calculator(); | ||
| MScanner mScanner = new MScanner(calculator); | ||
| int peopleCount; | ||
|
|
||
| mScanner.setPeopleCount(); | ||
| peopleCount = mScanner.getPeopleCount(); | ||
| calculator.showRequest(); | ||
| mScanner.setPrice(); | ||
|
|
||
| float endPrice = calculator.getSum() / peopleCount; | ||
| Formatter formatter = new Formatter(endPrice); | ||
|
|
||
| System.out.println("Добавленные товары:\n" + calculator.getResult() + "Сумма, которую должен заплатить каждый поровну: "+ formatter.getFormattedString()); | ||
| } | ||
| } |
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,19 @@ | ||
| public class Product { | ||
| private final String name; | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public float getPrice() { | ||
| return price; | ||
| } | ||
|
|
||
| private final float price; | ||
|
|
||
| public Product(String name, float 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.
Привет. Этот метод можно сделать приватным, потому что он используется только в классе
Calculator. Не критично.