forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputLoopFunctions.java
More file actions
36 lines (31 loc) · 1.41 KB
/
InputLoopFunctions.java
File metadata and controls
36 lines (31 loc) · 1.41 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
import java.util.InputMismatchException;
import java.util.Scanner;
public class InputLoopFunctions {
public static void addingItem(Scanner sc, Calculator calculator){
String itemName;
while (true) {
System.out.println("Введите название товара (либо -> 'завершить' ): ");
itemName = sc.next();
if (itemName.equalsIgnoreCase("завершить")) break;
else {
while (true) {
System.out.println("Введите стоимость товара в формате ХХ,ХХ (рубли,коп.): ");
try {
double itemPrice = sc.nextDouble();
if (itemPrice <= 0) {
System.out.println("Цена должна быть положительным числом!");
} else {
calculator.addDish(itemName, itemPrice);
break;
}
} catch (InputMismatchException ex) {
System.out.println("Формат ввода неверный!" +
" Введите дробное число через запятую!");
sc.nextLine();
}
}
}
}
sc.close();
}
}