forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
[init] Introduce Calculator #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
coyotedev
wants to merge
2
commits into
dev
Choose a base branch
from
feature/olepakhin/project_work_1
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
Changes from all commits
Commits
Show all changes
2 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 |
|---|---|---|
| @@ -1,6 +1,143 @@ | ||
| import static java.lang.Math.floor; | ||
|
|
||
| import java.util.Scanner; | ||
| import java.util.function.Consumer; | ||
|
|
||
| import calculator.Calculator; | ||
| import calculator.Goods; | ||
|
|
||
| public class Main { | ||
|
|
||
| private static final String MESSAGE_HOW_MUCH_PERSON = "На скольких человек необходимо разделить счёт?"; | ||
| private static final String MESSAGE_SUFFIX_RETRY = " Пожалуйста, повторите попытку..."; | ||
| private static final String MESSAGE_PERSON_COUNT_INCORRECT = "Количество человек, разделяющих счет, должно быть больше 1." + MESSAGE_SUFFIX_RETRY; | ||
| private static final String MESSAGE_INVALID_INPUT = "Осуществлен некорректный ввод." + MESSAGE_SUFFIX_RETRY; | ||
| private static final String MESSAGE_INVALID_INPUT_NEGATIVE_PRICE = "Цена товара не может быть меньше 0." + MESSAGE_SUFFIX_RETRY; | ||
| private static final String MESSAGE_GOODS_REQUEST_NAME = "Введите название товара:"; | ||
| private static final String MESSAGE_GOODS_REQUEST_PRICE = "И его стоимость в формате 00.00 (рубли.копейки):"; | ||
| private static final String MESSAGE_GOODS_REQUEST_NAME_INVALID = "Название не должно быть пустым. " + MESSAGE_SUFFIX_RETRY; | ||
| private static final String MESSAGE_GOODS_ADDED = "Товар успешно добавлен! Для того, чтобы добавить ещё один товар - введите любой символ. Введите \"Завершить\", чтобы завершить ввод товаров."; | ||
| private static final String MESSAGE_GOODS_SHOW_TITLE = "Добавленные товары:"; | ||
| private static final String MESSAGE_GOODS_SHOW_ITEM_FORMAT = "Название: %s, Цена: %.2f"; | ||
| private static final String MESSAGE_PART_PRICE_FORMAT = "Каждый человек должен заплатить %.2f %s"; | ||
| private static final String RUBLES_ROOT = "руб"; | ||
| private static final String[] RUBLES_SUFFIX = {"ль", "ля", "лей"}; | ||
| private static final int PERSON_COUNT_CORRECT = 2; | ||
| private static final String SAFE_WORD = "завершить"; | ||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println("Hello world!"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| Calculator calculator = new Calculator(requestPersonCount(scanner)); | ||
|
|
||
| requestGoods(scanner, calculator); | ||
|
|
||
| showResult(calculator); | ||
|
|
||
| scanner.close(); | ||
| } | ||
|
|
||
| private static void showResult(Calculator calculator) { | ||
| System.out.println(MESSAGE_GOODS_SHOW_TITLE); | ||
|
|
||
| for (Goods item : calculator.getAllGoods()) { | ||
| System.out.println(String.format(MESSAGE_GOODS_SHOW_ITEM_FORMAT, item.getName(), item.getPrice())); | ||
| } | ||
|
|
||
| double onePersonPrice = calculator.getOnePersonPrice(); | ||
| System.out.println(String.format(MESSAGE_PART_PRICE_FORMAT, onePersonPrice, getRublesText(onePersonPrice))); | ||
| } | ||
|
|
||
| private static String getRublesText(double price) { | ||
| String ret = RUBLES_ROOT; | ||
|
|
||
| double priceTransform = floor(price) % 100; | ||
| if (priceTransform > 19) priceTransform = priceTransform % 10; | ||
|
|
||
| if (priceTransform == 0 || priceTransform > 4) { | ||
| ret += RUBLES_SUFFIX[2]; | ||
| } else if (priceTransform == 1) { | ||
| ret += RUBLES_SUFFIX[0]; | ||
| } else { | ||
| ret += RUBLES_SUFFIX[1]; | ||
| } | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| private static void requestGoods(Scanner scanner, Calculator calculator) { | ||
| String safeWord = ""; | ||
|
|
||
| while (!safeWord.equalsIgnoreCase(SAFE_WORD)) { | ||
| System.out.println(MESSAGE_GOODS_REQUEST_NAME); | ||
| String goodsName = requestName(scanner); | ||
|
|
||
| System.out.println(MESSAGE_GOODS_REQUEST_PRICE); | ||
| double goodsPrice = requestPrice(scanner); | ||
|
|
||
| calculator.addGoods(new Goods(goodsName, goodsPrice)); | ||
|
|
||
| System.out.println(MESSAGE_GOODS_ADDED); | ||
| safeWord = scanner.nextLine(); | ||
| } | ||
| } | ||
|
|
||
| private static double requestPrice(Scanner scanner) { | ||
| double ret = .0; | ||
|
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. Зачем это нужно? |
||
|
|
||
| try { | ||
| Consumer<String> checkInput = (input) -> { | ||
| String[] doubleParts = input.split("\\."); | ||
| if (doubleParts.length == 1 || (doubleParts.length > 1 && doubleParts[1].length() != 2)) { | ||
| throw new IllegalStateException(); | ||
| } | ||
| }; | ||
|
|
||
| String input = scanner.nextLine(); | ||
| double inputConverted = Double.parseDouble(input); | ||
| checkInput.accept(input); | ||
| while (inputConverted < 0) { | ||
| System.out.println(MESSAGE_INVALID_INPUT_NEGATIVE_PRICE); | ||
| input = scanner.nextLine(); | ||
| checkInput.accept(input); | ||
| inputConverted = Double.parseDouble(input); | ||
| } | ||
| ret = inputConverted; | ||
| } catch (Exception e) { | ||
| System.out.println(MESSAGE_INVALID_INPUT); | ||
| return requestPrice(scanner); | ||
| } | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| private static String requestName(Scanner scanner) { | ||
| String ret = scanner.nextLine(); | ||
|
|
||
| while (ret.isBlank()) { | ||
| System.out.println(MESSAGE_GOODS_REQUEST_NAME_INVALID); | ||
| ret = scanner.nextLine(); | ||
| } | ||
|
|
||
| return ret.trim(); | ||
| } | ||
|
|
||
| private static int requestPersonCount(Scanner scanner) { | ||
| int ret = 0; | ||
|
|
||
| try { | ||
| while (ret < PERSON_COUNT_CORRECT) { | ||
| System.out.println(MESSAGE_HOW_MUCH_PERSON); | ||
| String input = scanner.nextLine(); | ||
| ret = Integer.parseInt(input); | ||
| if (ret < PERSON_COUNT_CORRECT) { | ||
| System.out.println(MESSAGE_PERSON_COUNT_INCORRECT); | ||
| } | ||
| } | ||
| } catch (Exception e) { | ||
| System.out.println(MESSAGE_INVALID_INPUT); | ||
| return requestPersonCount(scanner); | ||
| } | ||
|
|
||
| return ret; | ||
| } | ||
| } | ||
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,30 @@ | ||
| package calculator; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| public class Calculator { | ||
| private final int personCount; | ||
| private final ArrayList<Goods> m_goods = new ArrayList<>(); | ||
|
|
||
| public Calculator(int personCount) { | ||
| this.personCount = personCount; | ||
| } | ||
|
|
||
| public void addGoods(Goods goods) { | ||
| m_goods.add(goods); | ||
| } | ||
|
|
||
| public ArrayList<Goods> getAllGoods() { | ||
| return new ArrayList<>(m_goods); | ||
| } | ||
|
|
||
| public double getOnePersonPrice() { | ||
| double ret = .0; | ||
|
|
||
| for (Goods item : m_goods) { | ||
| ret += item.getPrice(); | ||
| } | ||
|
|
||
| return ret / personCount; | ||
| } | ||
| } |
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 @@ | ||
| package calculator; | ||
|
|
||
| public class Goods { | ||
| private final String name; | ||
| private final double price; | ||
|
|
||
| public Goods(String name, double price) { | ||
| this.name = name; | ||
| this.price = price; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return this.name; | ||
| } | ||
|
|
||
| public double getPrice() { | ||
| return this.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.
А это ты молодец, вынес строки для переиспользования при необходимости