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
101 changes: 101 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import java.text.DecimalFormat;
import java.util.Scanner;

public class Calculator {
public static void calculator(int numberPeople) {

String finish = "Завершить";
double sum = 00.00;
StringBuilder list = new StringBuilder();
String endingRub = "";
String endingSum = "";

DecimalFormat decimalFormat = new DecimalFormat("#.##");

while (true) {

System.out.println("Введите название товара или слово 'Завершить', чтобы получить результат");
Scanner scanner = new Scanner(System.in);
String nameGoods = scanner.nextLine();

if (nameGoods.equalsIgnoreCase(finish)) {
break;
}

System.out.println("Введите стоимость в формате 00,00");

if (scanner.hasNextDouble()) {

double price = scanner.nextDouble();

if (price > 0) {
sum = sum + price;
endingRub = ending(price);
decimalFormat.format(price);

list.append(" \n" + nameGoods + " – " + price + " рубл" + endingRub);

} else {
System.out.println("Ошибка! Неверный формат цены. Попробуйте ввести данные о товаре снова");
}
} else {
System.out.println("Ошибка! Неверный формат цены. Попробуйте ввести данные о товаре снова");
}
}

endingSum = ending(sum / numberPeople);
System.out.println(list + "\n" + "С каждого: " + decimalFormat.format(sum / numberPeople) + " рубл" + endingSum);
}

public static String ending(double price) {

String endingOne = "ь";
String endingTwo = "я";
String endingThree = "ей";
String ending;

int priceInt = (int) price;

if (price > 10) {
price %= 100;

if (price == 11 || price == 12 || price == 13 || price == 14) {
ending = endingThree;

} else {
priceInt %= 10;

switch (priceInt) {
case 1:
ending = endingOne;
break;
case 2:
case 3:
case 4:
ending = endingTwo;
break;
default:
ending = endingThree;
break;
}
}
} else {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⏫ тут код по сути дублируется, поэтому можно не делать if, а просто вынести и оставить только код, который в первой ветке

price %= 100;

            if (price == 11 || price == 12 || price == 13 || price == 14) {
                ending = endingThree;

            } else {
                priceInt %= 10;

                switch (priceInt) {
                    case 1:
                        ending = endingOne;
                        break;
                    case 2:
                    case 3:
                    case 4:
                        ending = endingTwo;
                        break;
                    default:
                        ending = endingThree;
                        break;
                }
            }

Этого должно хватить

priceInt %= 10;

switch (priceInt) {
case 1:
ending = endingOne;
break;
case 2:
case 3:
case 4:
ending = endingTwo;
break;
default:
ending = endingThree;
break;
}
}
return ending;
}
}
28 changes: 26 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
import java.util.Scanner;
import java.text.DecimalFormat;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");

while (true) {

Scanner scanner = new Scanner(System.in);
System.out.println("На сколько человек делим счет?");

if (scanner.hasNextInt()) {
int numberPeople = scanner.nextInt();

if (numberPeople > 1) {
Calculator.calculator(numberPeople);
break;
} else {
System.out.println("Некорректное число. Попробуйте ввести количество людей снова");
}
} else {
System.out.println("Некорректное число. Попробуйте ввести количество людей снова");
}
}
}
}
}