forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScannerHelper.java
More file actions
72 lines (65 loc) · 2.69 KB
/
ScannerHelper.java
File metadata and controls
72 lines (65 loc) · 2.69 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
import java.util.Scanner;
public class ScannerHelper {
public static final String DONE_EXECUTE_COMMAND = "завершить";
public static int getCountPeople(Scanner scanner) {
System.out.println("Введите количество человек:");
int count;
while (true) {
if(scanner.hasNextInt()) {
count = scanner.nextInt();
if (count > 1) {
break;
} else {
System.out.println("Ошибка ввода!\nВведите количество людей больше одного");
}
} else {
System.out.println("Ошибка ввода!\nВведите число больше одного");
}
//Считываем лишний символ
scanner.nextLine();
}
return count;
}
public static String getName(Scanner scanner) {
String name;
System.out.println("Введите название товара");
name = scanner.next();
//Считываем лишний символ
scanner.nextLine();
return name;
}
public static double getPrice(Scanner scanner) {
System.out.println("Введите цену товара");
double price;
while (true) {
if(scanner.hasNextDouble()) {
price = scanner.nextDouble();
if (price > 0) {
break;
} else {
System.out.println("Ошибка ввода!\nЦена не может быть отрицательной");
}
} else {
System.out.println("Ошибка ввода!\nВведите корректную цену");
//Считываем лишний символ
scanner.nextLine();
}
}
//Считываем лишний символ
scanner.nextLine();
return price;
}
public static boolean canStopCalculate(Scanner scanner) {
boolean isStop = false;
String command;
System.out.println("Товар успешно добавлен");
System.out.println("Продолжаем добавление?");
System.out.println("1. Чтобы получить расчет введите команду 'Завершить'");
System.out.println("2. Чтобы чтобы продолжить расчет введите любую команду");
command = scanner.nextLine();
if (command.equalsIgnoreCase(DONE_EXECUTE_COMMAND)) {
isStop = true;
}
return isStop;
}
}