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
58 lines (51 loc) · 1.98 KB
/
Calculator.java
File metadata and controls
58 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
import java.util.ArrayList;
import java.util.Scanner;
public class Calculator {
private double sum;
private int guests;
private ArrayList<Good> goods;
public Calculator() {
guests = getGuests();
goods = new ArrayList<Good>();
}
private int getGuests() {
Scanner scanner = new Scanner(System.in);
int input = 0;
while (true) {
System.out.print("На сколько человек делим счёт?:");
if (scanner.hasNextInt()) {
input = scanner.nextInt();
if (input > 1) {
return input;
} else System.out.println("Значение должно быть больше 1");
} else System.out.println("Значение некорректно!!!");
String buf = scanner.nextLine();
}
}
public void addGoods() {
Scanner scanner = new Scanner(System.in);
System.out.print("Введите наименование товара:");
String name = scanner.nextLine();
double price = -1.00;
while (true) {
System.out.print("Введите цену:");
if (scanner.hasNextDouble()) {
price = scanner.nextDouble();
if (Double.compare(price, 0.00) >= 0) break;
}
System.out.println("Значение некорректно!!!");
String buf = scanner.nextLine();
}
goods.add(new Good(name, price));
sum += price;
}
public void print() {
System.out.println("Добавленные товары:");
for (Good good : goods) {
good.print();
}
System.out.println(String.format("ИТОГО: %.2f %s", sum, Format.rublFormat(sum)));
System.out.println("Всего человек : " + guests);
System.out.println(String.format("На каждого: %.2f %s", sum / guests, Format.rublFormat(sum / guests)));
}
}