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
67 lines (59 loc) · 2.74 KB
/
Calculator.java
File metadata and controls
67 lines (59 loc) · 2.74 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
60
61
62
63
64
65
66
67
import java.util.ArrayList;
import java.util.Scanner;
public class Calculator {
private double sum;
private Scanner scaner = new Scanner(System.in);
private ArrayList<Product> products = new ArrayList<>();
private String iputName() {
System.out.println("Введите название товара");
String name = scaner.next();
return name;
}
private double inputCost() {
while (true) {
System.out.println("Введите стоимость");
if (scaner.hasNextDouble()) {
double cost = scaner.nextDouble();
if (cost <= 0) {
System.out.println("Стоимость не может быть меньше 0");
scaner.nextLine();
continue;
}
return cost;
} else {
System.out.println("Ошибка, попробуйте еще раз");
scaner.nextLine();
}
}
}
public void addProduct() {
while (true) {
System.out.println("Добавление товара в калькулятор");
Product product = new Product(iputName(), inputCost());
products.add(product);
sum += product.cost;
System.out.println(String.format("Товар %s успешно добавлен!", product.name));
System.out.println("Продолжить добовление товара?");
System.out.println("Ввести команду \"Завершить\" для того, чтоб завершить процесс добавления товаров");
String comand = scaner.next();
if (comand.equalsIgnoreCase("Завершить")) {
break;
}
}
}
public void showProducts() {
FormatterRub formatterRub = new FormatterRub();//для форматирования окончания рубля
System.out.println("Добавленные товары:");
String message = "Товар: %s цена: %.2f %s";
for (Product product : products) {
System.out.println(String.format(message, product.name, product.cost, formatterRub.correctEnding(product.cost)));
}
}
public void showResult(int countHuman) {
FormatterRub formatterRub = new FormatterRub();
System.out.println(String.format("Всего товаров на сумму: %.2f %s", sum, formatterRub.correctEnding(sum)));
double result = sum / countHuman;
String text = "Каждый должен заплатить: %.2f %s";
System.out.println(String.format(text, result, formatterRub.correctEnding(result)));
}
}