forked from Yandex-Practicum/Java-Module-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
58 lines (51 loc) · 2.5 KB
/
Calculator.java
File metadata and controls
58 lines (51 loc) · 2.5 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
import java.util.Scanner;
public class Calculator {
public static void calculator(int delitel) {
Scanner scannerProduct = new Scanner(System.in);
Product productAll = new Product();
while (true) {
System.out.println("Введите название товара");
String product = scannerProduct.next();
String and = "Завершить";
if (product.equalsIgnoreCase(and)) {
break;
}
System.out.println("Введите стоимость товара (рубли.копейки)");
Float price = cost();
productAll.allProduct = productAll.allProduct + "\n" + product ;
productAll.allPrice = productAll.allPrice + price;
System.out.println("Товар добавлен, хотите добавить еще товар ? \n Если нет, введите команду \"Завершить\"");
String answer = scannerProduct.next();
if (answer.equalsIgnoreCase(and)) {
break;
}
}
float personPrice = productAll.allPrice / delitel;
String personPriceString = String.format("%.2f", personPrice);
int personPriceInteger = (int) personPrice;
String rub = PrintRub.getStringRub(personPriceInteger);
System.out.println("Добавленные товары:" + productAll.allProduct + "\n" + "Сумма к оплате для каждого человека - " + personPriceString + " " + rub);
}
//введение цены
public static float cost() {
Scanner scanner = new Scanner(System.in);
float price = 0.0f;
try {
String value = scanner.next().trim();
price = Float.parseFloat(value);
int pointPosition = value.indexOf('.');
if (!(pointPosition == -1 || value.length() - 1 - pointPosition <= 2)) {
System.out.println("Введено некорректное значение, попробуйте еще.");
price = cost();
}
if (price < 0) {
System.out.println("Введено некорректное значение, попробуйте еще.");
price = cost();
}
} catch (Exception e) {
System.out.println("Введено некорректное значение, попробуйте еще.");
price = cost();
}
return price;
}
}