Repository navigation
Expand file tree
/
Copy pathGoods.java
More file actions
88 lines (73 loc) · 3.46 KB
/
Copy pathGoods.java
File metadata and controls
88 lines (73 loc) · 3.46 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.util.Scanner;
public class Goods {
private double totalBill = 0;
private String goods = "";
/**
Метод создает String со списком товаров и их ценой, а также суммирует все товары
в отдельную переменную totalBill
*/
public void createListOfGoods() {
String tempGoods = "";
String userInputGoodName;
double userInputGoodValue;
Scanner scanner = new Scanner(System.in);
while (true) {
if (tempGoods.isEmpty()) {
System.out.println("Введите название товара (Или \"Завершить\" для окончания формирования списка):");
} else {
System.out.println("Введите название следующего товара (Или \"Завершить\" для окончания формирования списка):");
}
userInputGoodName = scanner.nextLine();
if (userInputGoodName.equalsIgnoreCase("Завершить")) {
break;
}
if (tempGoods.isEmpty()) {
tempGoods = tempGoods.concat(userInputGoodName);
} else {
tempGoods = tempGoods.concat("\n" + userInputGoodName);
}
System.out.println("Введите стоимость товара в формате рубли.копейки (например 10.22):");
while (true) {
userInputGoodValue = checkUserInputAsDouble();
if (userInputGoodValue < 0) {
System.out.println("Стоимость товара должна быть неотрицательной");
} else {
break;
}
}
totalBill += userInputGoodValue;
tempGoods = tempGoods.concat(String.format(" - Цена: %.2f", userInputGoodValue));
}
goods = tempGoods;
}
/**
Метод проверяет, является ли ввод пользователя double
*/
private double checkUserInputAsDouble() {
Scanner scanner = new Scanner(System.in);
double correctDoubleInput;
while (true) {
try {
correctDoubleInput = Double.parseDouble(scanner.next());
break;
} catch (NumberFormatException e) {
System.out.println("Введите корректную стоимость в формате рубли.копейки (например 10.22):");
}
}
return correctDoubleInput;
}
/**
Метод выводит, сколько каждый человек должен заплатить в зависимости от
введенного количества гостей
*/
public void calculateAndShowResultsPerGuest(int numberOfGuests) {
if (goods.isEmpty()) {
System.out.println("Никаких товаров не было добавлено");
return;
}
System.out.println("Добавленные товары: \n" + goods);
double billPerPerson = totalBill / numberOfGuests;
String roubles = RoubleFormatter.returnRoubleString(billPerPerson);
System.out.println(String.format("Каждый человек должен заплатить %.2f %s", billPerPerson, roubles));
}
}