forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheck.java
More file actions
59 lines (49 loc) · 2.23 KB
/
Check.java
File metadata and controls
59 lines (49 loc) · 2.23 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
import static java.lang.Float.parseFloat;
import java.util.Scanner;
public class Check {
Input input = new Input();
StringBuilder check = new StringBuilder();
Scanner scanner = new Scanner(System.in);
Format format = new Format();
public float getItem() {
boolean point = true;
float sum = 0.00f;
String item;
String price;
System.out.println("Если хотите завершить, веведите 'Завершить'.");
while (point){// Цикл на ввод товара и подсчет суммы чека
System.out.println("Введите наименование товара");
item = scanner.nextLine();
if (input.StopOrNot(item))//проверка на завершение ввода
break;
System.out.println("Введите цену товара");
price = scanner.nextLine();
if (getPrice(price) == 0.0f)//проверка на правильность ввода цены
System.out.println("Введите товар, стоимость которого больше нуля");
else {
sum += getPrice(price);//сумма чека
check.append("\n" + item + " " + getPrice(price) +" "+ format.ruble(getPrice(price)));
}
}
System.out.println("Добавленные товары:" + check);
return sum;
}
public float getPrice(String item) {
String strPrice;
if (item.replaceAll("[^-]","").equals("-")) {
return 0.0f;//отрицательная стоимость
}
if (!(item.replaceAll("[^a-zA-Zа-яА-Я]", "").equals(""))) {
return 0.0f; //текст в цене
}
strPrice = item.replaceAll(",",".");//Замена ',' на '.';
strPrice = strPrice.replaceAll("[^0-9?!\\.]","");//Выбор только стоимость
if (strPrice.equals("")) return 0.0f;// нет цены
else{
float price = parseFloat(strPrice);//перевод строки в число
if (price > 0.009f)
return price;
else return 0.0f;
}
}
}