Repository navigation
Expand file tree
/
Copy pathDivisionCheck.java
More file actions
69 lines (62 loc) · 2.81 KB
/
Copy pathDivisionCheck.java
File metadata and controls
69 lines (62 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.ArrayList;
import java.util.Scanner;
public class DivisionCheck {
public static String rub (float sum) {
if (sum % 10 == 1 && sum % 100 != 11) {
return "рубль";
} else if ((sum % 10 == 2 || sum % 10 == 3 || sum % 10 == 4)&& sum % 100 != 12 && sum % 100 != 13 && sum % 100 != 14) {
return "рубля";
} else {
return "рублей";
}
}
public static void divCheck (int numberPeople) {
Scanner scanner = new Scanner(System.in);
int i = 0;
String name;
float price;
float sum = 0;
ArrayList<Product> productList = new ArrayList<>();
while (true) {
i = i + 1;
while(true) {
System.out.printf("Введите название товара №%d: ", i);
name = scanner.next();
break;
}
while(true) {
System.out.printf("Введите стоимоть товара №%d: ", i);
if (scanner.hasNextFloat()){
price = scanner.nextFloat();
if (price > 0) {
break;
}
} else scanner.next();
System.out.println("Ошибка ввода. Необходимо вводить числовое значение.\n" + "Цена не может быть отрицательной или нулевой.");
}
productList.add(new Product(name, price));
sum = sum + price;
System.out.println("Товар добавлен в список. Продолжаем?");
System.out.println("Для продолжения введите любой символ, для завершения введите \"завершить\".");
String reply = scanner.next();
if (reply.equalsIgnoreCase("завершить"))
{break;}
}
System.out.println("Список добавленных товаров");
System.out.println("|Наименование товара |Стоимость |");
for (i = 0; i < productList.size(); i = i + 1) {
System.out.println("|" + String.format("%-20s", productList.get(i).name) + "|" + String.format("%-10.2f", productList.get(i).price) + "|");
}
System.out.printf("Итоговая сумма: %.2f %s\t\n", sum, rub(sum));
System.out.printf("Количество гостей: %d\t\t\n", numberPeople);
System.out.printf("Итого на человека: %.2f %s\t\t\n", sum / numberPeople, rub(sum / numberPeople));
}
}
class Product {
String name;
float price;
Product (String name, float price) {
this.name = name;
this.price = price;
}
}