Repository navigation
Expand file tree
/
Copy pathProductCalculator.java
More file actions
101 lines (78 loc) · 3.67 KB
/
Copy pathProductCalculator.java
File metadata and controls
101 lines (78 loc) · 3.67 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class ProductCalculator {
private static final Scanner scanner = new Scanner(System.in);
private final ArrayList<Product> products = new ArrayList<>();
private float totalPriceOfAllProducts = 0.00f;
private final int peopleCount;
ProductCalculator(int peopleCount) {
this.peopleCount = peopleCount;
}
public void addProduct(Product product) {
this.products.add(product);
this.totalPriceOfAllProducts += product.getPrice();
}
public float getPricePerOnePerson() {
return totalPriceOfAllProducts / peopleCount;
}
public float getTotalAmountOfProducts() {
return this.totalPriceOfAllProducts;
}
public void showAllProducts() {
System.out.println("Список продуктов:");
for (int i = 0; i < products.size(); i++) {
System.out.printf("%d) Название: \"%s\", стоимость: %.2f %s. \n",
i + 1,
products.get(i).getName(),
products.get(i).getPrice(),
getCorrectEnding(products.get(i).getPrice()));
}
}
public void showResult() {
showAllProducts();
System.out.printf("Конечная стоимость всего товара: %.2f %s \n", getTotalAmountOfProducts(), getCorrectEnding(getTotalAmountOfProducts()));
System.out.printf("Каждый человек должен будет заплатить: %.2f %s", getPricePerOnePerson(), getCorrectEnding(getPricePerOnePerson()));
}
private String getCorrectEnding(float rubles) {
float roundedOffRubles = (float) Math.floor(rubles);
if (roundedOffRubles == 1.00f) {
return "рубль";
} else if (roundedOffRubles > 1 && roundedOffRubles < 5) {
return "рубля";
} else {
return "рублей";
}
}
public void inputProducts() {
String productName;
float productPrice;
while (true) {
System.out.print("Введите название товара: ");
productName = scanner.next();
System.out.println("Введите стоимость товара стоимость в формате: \"'рубли.копейки' [10.45, 11.40]\"");
try {
productPrice = scanner.nextFloat();
} catch (InputMismatchException er) {
System.out.println("Цена товара некорректна, введите товар заново.");
scanner.nextLine();
continue;
}
if (productPrice <= 0.0f) {
System.out.println("Цена товара некорректна, введите товар заново.");
continue;
}
Product product = new Product(productName, productPrice);
addProduct(product);
System.out.printf("Вы успешно довавила товар \"%s\" \n", productName);
System.out.printf("Предварительная стоимость всех товаров = %.2f %s\n", getTotalAmountOfProducts(), getCorrectEnding(getTotalAmountOfProducts()));
System.out.println("\nВедите \"Завершить\", если добавили всё нужные товары");
System.out.println("Добавить ещё товар?");
String answer = scanner.next();
if (answer.equalsIgnoreCase("завершить")) {
showResult();
break;
}
}
}
}