Repository navigation
Expand file tree
/
Copy pathBillSplitter.java
More file actions
73 lines (60 loc) · 2.56 KB
/
Copy pathBillSplitter.java
File metadata and controls
73 lines (60 loc) · 2.56 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
70
71
72
73
import java.util.List;
import java.util.Locale;
import java.util.Scanner;
import java.util.Vector;
public class BillSplitter {
double totalAmount = 0;
int personCount;
List<Product> productList = new Vector<>();
BillSplitter(int count) {
personCount = count;
}
public String checkDouble(Double value) {
double d = value % 10;
String result = String.format("%.2f", value);
if (d==1)
result+=" рубль";
else if(d==2||d==3||d==4)
result+=" рубля";
else
result+=" рублей";
return result;
}
public void addProduct() {
String commandText;
String productName;
Double productPrice;
Scanner scanner = new Scanner(System.in);
scanner.useLocale(Locale.US);
while (true) {
System.out.print("Введите название товара: ");
productName = scanner.nextLine();
System.out.print("Введите стоимость в формате: \"'рубли.копейки' [10.45, 11.40]\": ");
if (scanner.hasNextDouble()) {
productPrice = scanner.nextDouble();
if (productPrice >= 0) {
productList.add(new Product(productName, productPrice));
totalAmount += productPrice;
} else {
System.out.println("Вы ввели некорректные данные цены, попробуйте заново");
scanner.nextLine();
continue;
}
} else {
System.out.println("Вы ввели некорректные данные цены, попробуйте заново");
scanner.nextLine();
continue;
}
System.out.println("Продукт " + productName + " был добавлен\nВы хотите добавить еще один товар?");
commandText = scanner.next();
scanner.nextLine();
if (commandText.equalsIgnoreCase("завершить"))
break;
}
System.out.println("Добавленные товары:");
for (int i = 0; i < productList.size(); i++) {
System.out.println(productList.get(i).title + "\t" + this.checkDouble(productList.get(i).price));
}
System.out.println("\nОбщая сумма на каждого человека: " + this.checkDouble(totalAmount / personCount));
}
}