forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNameAndPrice.java
More file actions
37 lines (36 loc) · 1.45 KB
/
NameAndPrice.java
File metadata and controls
37 lines (36 loc) · 1.45 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
import java.util.Scanner;
public class NameAndPrice {
public static String nameProduct() {
Scanner scanner = new Scanner(System.in);
System.out.println(" Введите наименование товара\n" +
"(минимум 2 символа): ");
String nameProduct;
while (true) {
nameProduct = scanner.nextLine();
if (nameProduct.isEmpty()) {
System.out.println("Значение не может быть пустым. Введите ещё раз.");
} else if (nameProduct.length() < 2) {
System.out.println("Слишком короткое название (меньше 2 символов). Введите ещё раз.");
} else {
break;
}
}
return nameProduct;
}
public static double priceProduct(){
Scanner scanner = new Scanner(System.in);
System.out.println("Введите цену товара\n" +
" (формат рубли.копейки): ");
double priceProduct;
while (true) {
if (scanner.hasNextDouble()){
priceProduct = scanner.nextDouble();
break;
} else {
System.out.println("Неверный формат ввода. Попробуйте заново.");
scanner.next();
}
}
return priceProduct;
}
}