Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
47 lines (45 loc) · 1.83 KB
/
Copy pathCalculator.java
File metadata and controls
47 lines (45 loc) · 1.83 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
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.Locale;
import java.util.Scanner;
public class Calculator {
ArrayList<Product> products = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
public void aboutProduct(){
scanner.useLocale(Locale.ENGLISH);
System.out.println("Введите название товара");
String name = scanner.nextLine();
double price;
while (true) {
System.out.println("Введите стоимость товара в формате: рубли.копейки");
try {
price = scanner.nextDouble();
if (price < 0) {
System.out.println("Цена не может быть отрицательной!");
} else {
break;
}
} catch (InputMismatchException e) {
System.out.println("Цена должна являться неотрицательным числом!");
scanner.nextLine();
}
}
products.add(new Product(name, price));
System.out.println("Продукт успешно добавлен!");
scanner.nextLine();
}
public void result(int guests) {
System.out.println("Добавленные товары:");
Iterator<Product> iterator = products.iterator();
Double sum = 0.00;
while (iterator.hasNext()) {
Product current = iterator.next();
String out = String.format("%s : %.2f руб.",current.name, current.price);
sum += current.price;
System.out.println(out);
}
sum = sum / guests;
System.out.println("Итого с человека: " + Formatter.formatRubles(sum));
}
}