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
49 lines (41 loc) · 1.86 KB
/
Calculator.java
File metadata and controls
49 lines (41 loc) · 1.86 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
import java.util.InputMismatchException;
import java.util.Scanner;
public class Calculator {
static void calculate(int people) {
String prod = "";
double price = 0.0;
while (true) {
System.out.println("Для добавления позиции, Введите название" +
"\nДля завершения подсчёта, Введите 'Завершить'");
Scanner scanner = new Scanner(System.in);
String inputProd = scanner.next();
if (inputProd.equalsIgnoreCase("Завершить")) {
break;
} else {
prod = prod + inputProd + "\n";
price = price + getPrice();
}
}
System.out.println("Добавленные позиции:\n" + prod);
double result = price / people;
String temp = "Каждый человек должен заплатить %.2f" + Formatter.formatted((int) Math.floor(result));
System.out.println(String.format(temp, result));
}
static double getPrice() {
while (true) {
System.out.println("Укажите стоимость в формате: 'рубли.копейки' [например: 5,55 или 11.48]");
double inputPrice = 0;
try {
Scanner scanner = new Scanner(System.in);
inputPrice = scanner.nextDouble();
if (inputPrice <= 0){
System.out.println("Ошибка: неверное значение. Попробуйте снова");
} else {
return inputPrice;
}
} catch (InputMismatchException e) {
System.out.println("Ошибка: неверное значение. Попробуйте снова");
}
}
}
}