Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
57 lines (42 loc) · 1.94 KB
/
Copy pathCalculator.java
File metadata and controls
57 lines (42 loc) · 1.94 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
public class Calculator //получает на вход название и цену товара, сохраняет названия и общую сумму товаров, выводит в нужном формате
{
private String tovarName = "Добавленные товары:";
private float totalPrice = 0f;
public void AddTovarName(String foodName)//добавить товар
{
tovarName = "\n" + tovarName + "\n" + foodName;
}
public void AddTovarPrice(float foodPrice)//добавить цену на товар
{
totalPrice = totalPrice + foodPrice;
}
public void totalOutput(int numberOfFriends)//вывод результата
{
System.out.println(tovarName + "\n"); //выводим список всех товаров
System.out.println("Общая сумма к оплате: " + String.format("%.2f", totalPrice) + rubFormat(Math.floor(totalPrice)));
System.out.println("Каждый друг должен " + String.format("%.2f", (totalPrice / numberOfFriends)) + rubFormat(Math.floor(totalPrice / numberOfFriends)));
}
private String rubFormat(double totalSum) {
String rubleView;
int totalIntBaseSum = (int) totalSum;//базовое число, не меняется
int totalIntSum = totalIntBaseSum;
if (totalIntBaseSum >= 100) {
totalIntSum = totalIntBaseSum % 100;
if (totalIntSum > 20) {
totalIntSum = totalIntSum % 10;
}
} else if (totalIntBaseSum > 20) {
totalIntSum = totalIntBaseSum % 10;
}
if (totalIntSum < 1) {
rubleView = " рублей!";
} else if (totalIntSum == 1) {
rubleView = " рубль!";
} else if (totalIntSum < 5) {
rubleView = " рубля!";
} else {
rubleView = " рублей!";
}
return (rubleView);
}
}