Skip to content

create Calculator - #1

Open
dencharski wants to merge 2 commits into
mainfrom
mydev
Open

dencharski wants to merge 2 commits into
mainfrom
mydev

Conversation

@dencharski

Copy link
Copy Markdown
Owner

No description provided.

Comment thread src/main/java/Main.java Outdated
System.out.println("На сколько человек делим счет?");

try {
String s = scanner.next();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

У Scanner есть удобный метод hasNextInt, можно сразу узнать без дополнительных проверок возможно ли интерпретировать строку как int

Comment thread src/main/java/Main.java Outdated
System.out.println("Ошибка! " + errorString);
}

static void onDestroy() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Совсем не обязательно пытаться уничтожить объекты самостоятельно, после завершения программы, сборщик мусора будет удалять эти объекты, а их зануление по факту ничего не делает.

Comment thread src/main/java/Calculator.java Outdated
}
priceForOnePeople = finalPrice / peoples;

return ("Продукт " + product.productName + " по цене " + product.productPrice + " добавлен.");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В таких случаях для вывода лучше использовать System.out.printf, ему можно передать параметры, которые будут поставлены в строку.

Comment thread src/main/java/Calculator.java Outdated
private String convertPriceToString(double price) {
String stringPrice = String.format("%.2f", price);
String zeroEnd = "0";
if (stringPrice.endsWith(zeroEnd)) {

@gusar-off gusar-off Dec 8, 2022 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Немного некорректно рассчитывается окончание. Получается если цена будет 14 рублей, метод вернет рубля. Правильный алгоритм звучит так - Округлить double до целых, проверить лежит ли остаток от деления на 100 в интервале от 11 до 14 включительно, если да, то окончание "рублей", если нет, то берем остаток от деления на 10 и проверяем: если 1 - рубль, если 2-4 - рубля, остальные случаи - рублей. То есть нужно во-первых проверять именно остаток от деления, во-вторых учесть, что числа заканчивающиеся на 11-12-13-14 и заканчивающиеся на 1-2-3-4, будут иметь разные окончания

Comment thread src/main/java/Calculator.java Outdated
} else {
allProductNames = allProductNames + ", \n" + product.productName;
}
priceForOnePeople = finalPrice / peoples;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно посчитать это значение один раз - при выводе значения в getPriceForOnePeople

-deleted fun onDestroy().
-in fun addProduct() change return String to String.format().
-changed the calculation of the endings of the word "рубль".
-the variable "priceForOnePeople" is counted once
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants