forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputProcessing.java
More file actions
95 lines (79 loc) · 2.97 KB
/
InputProcessing.java
File metadata and controls
95 lines (79 loc) · 2.97 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
89
90
91
92
93
94
95
import java.util.Locale;
import java.util.Scanner;
public class InputProcessing {
Scanner scanner = new Scanner(System.in);
public int getNumberOfPeople() {
int numberOfPeople;
while (true) {
String numberOfPeopleInput = scanner.nextLine();
if (numberOfPeopleInput.isEmpty()) {
this.emptyLineMessage();
continue;
}
try {
numberOfPeople = Integer.parseInt(numberOfPeopleInput.trim());
} catch (NumberFormatException e) {
this.wrongNumberOfPeopleMessage();
continue;
}
if (numberOfPeople > 1) {
break;
} else {
this.wrongNumberOfPeopleMessage();
}
}
return numberOfPeople;
}
public Product getNameAndPriceOfProduct() {
scanner.useLocale(Locale.US);
String productName;
double productPrice;
System.out.println("Введите название товара:");
while (true) {
productName = scanner.nextLine().trim();
if (productName.isEmpty()) {
this.emptyLineMessage();
} else {
break;
}
}
System.out.println("Введите стоимость данного товара в формате \"рубли.копейки.\"");
while (true) {
String productPriceInput = scanner.nextLine().trim();
if (productPriceInput.isEmpty()) {
this.emptyLineMessage();
} else {
try {
productPrice = Double.parseDouble(productPriceInput);
} catch (NumberFormatException e) {
this.wrongProductInfoMessage();
continue;
}
if (productPrice > 0) {
break;
} else {
this.wrongProductInfoMessage();
}
}
}
return new Product(productName, productPrice);
}
public void wrongNumberOfPeopleMessage() {
System.out.println("""
Введено некорректное значение.
Количество гостей должно быть больше 1.
Введите новое значение:""");
}
public void emptyLineMessage() {
System.out.println("""
Получена пустая строка!
Введите требуемые значения ещё раз:
""");
}
public void wrongProductInfoMessage() {
System.out.println("""
Неверное значение!
Стоимость товара должна быть представлена в виде положительного числа.
Введите данные заново:""");
}
}