Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/Calculated.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,5 @@ public Product(String name, double price) {
this.name = name;
this.price = price;
}

}
1 change: 0 additions & 1 deletion src/main/java/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
public class Input {
private final Scanner scanner = new Scanner(System.in);


public void quantityOfPeople() {
Calculated calculated = new Calculated();

Expand Down
1 change: 1 addition & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

public class Main {

public static void main(String[] args) {
Input input = new Input();
input.quantityOfPeople();
Expand Down
22 changes: 19 additions & 3 deletions src/main/java/TotalAmountSplitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,27 @@ public class TotalAmountSplitter {
public static void calculatePerPerson(double totalAmount, int quantity) {
double perPersonAmount = totalAmount / quantity;

double roundedAmount = Math.floor(perPersonAmount * 100) / 100;

String formattedAmount;
if (perPersonAmount == Math.floor(perPersonAmount)) {
formattedAmount = String.format("%.0f рубль", perPersonAmount);
boolean isSpecialCase = Math.floor(roundedAmount * 100) % 100 >= 11 && Math.floor(roundedAmount * 100) % 100 <= 19;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Результат выполнения Math.floor(roundedAmount * 100) % 100 можно вынести в переменную и выполнять это вычисление один раз, вместо двух


if (!isSpecialCase) {
int lastDigit = (int) (roundedAmount * 100 % 10);
switch (lastDigit) {
case 1:
formattedAmount = String.format("%.2f рубль", roundedAmount);
break;
case 2:
case 3:
case 4:
formattedAmount = String.format("%.2f рубля", roundedAmount);
break;
default:
formattedAmount = String.format("%.2f рублей", roundedAmount);
}
} else {
formattedAmount = String.format("%.2f рубля", perPersonAmount).replace(".", ",");
formattedAmount = String.format("%.2f рублей", roundedAmount);
}

System.out.println("Каждый человек должен заплатить: " + formattedAmount);
Expand Down