forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
60 lines (51 loc) · 1.98 KB
/
Calculator.java
File metadata and controls
60 lines (51 loc) · 1.98 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
import java.util.Scanner;
public class Calculator {
public int scannerNumber() {
int number = 0;
while (number <= 1) {
Scanner in = new Scanner(System.in);
System.out.print("Укажите на сколько человек необходимо разделить счет: ");
if (in.hasNextInt()) {
number = in.nextInt();
}
}
return number;
}
public boolean checkProduct(String[] scannerProduct) {
if (scannerProduct.length != 2) {
return false;
}
float f;
try {
f = Float.parseFloat(scannerProduct[1]);
if (f <= 0) {
return false;
}
}
catch (NumberFormatException e) {
return false;
}
return true;
}
public Product scannerProducts(Product prod) {
while (true) {
Scanner sc = new Scanner(System.in);
System.out.println("Введите товар и его стоимость через пробел:");
String productAndCost = sc.nextLine();
String[] productAndCostArray = productAndCost.split(" ");
if (checkProduct(productAndCostArray)) {
float price = Float.parseFloat(productAndCostArray[1]);
prod.addProduct(productAndCostArray[0], price);
System.out.println("Продукт успешно добавлен");
} else {
System.out.println("Неверный формат данных, продукт не был добавлен");
}
System.out.println("Если хотите добавить ещё один продукт введите любой символ или \"Завершить\"");
String newProduct = sc.nextLine();
if (newProduct.equalsIgnoreCase("завершить")) {
break;
}
}
return prod;
}
}