Repository navigation
Expand file tree
/
Copy pathUserInput.java
More file actions
78 lines (64 loc) · 2.35 KB
/
Copy pathUserInput.java
File metadata and controls
78 lines (64 loc) · 2.35 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
package utils;
import java.util.Scanner;
import exception.KopecksFormatException;
public class UserInput {
private final static Scanner scanner = new Scanner(System.in);
public static int enterNumberOfGuests() {
int numberOfGuests;
while (true) {
try {
numberOfGuests = Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException e) {
System.out.println("Ошибка. Введите число");
continue;
}
if (numberOfGuests > 1) {
break;
} else {
System.out.println("Ошибка. Введите число > 1");
}
}
return numberOfGuests;
}
public static String enterProductName() {
System.out.println("Введите название продукта");
return scanner.nextLine();
}
public static double enterCostOfTheProduct() {
System.out.println("Введите сумму товара в формате рубли.копейки");
double cost;
while (true) {
try {
String line = scanner.nextLine();
cost = Double.parseDouble(line);
validateKopecksAmount(line);
} catch (NumberFormatException e) {
System.out.println("Ошибка. Введите число в формате рубли.копейки ");
continue;
} catch (KopecksFormatException e) {
System.out.println("Ошибка. Введите копейки в формате .xx");
continue;
}
if (cost < 0) {
System.out.println("Введите положительное число");
} else {
return cost;
}
}
}
public static String askToContinueAddingProducts() {
return scanner.nextLine();
}
private static void validateKopecksAmount(String line) throws KopecksFormatException {
String kopecks;
try {
kopecks = line.split("\\.")[1];
} catch (IndexOutOfBoundsException e) {
throw new KopecksFormatException();
}
int length = kopecks.length();
if (length != 2) {
throw new KopecksFormatException();
}
}
}