Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
156 lines (110 loc) · 4.09 KB
/
Copy pathMain.java
File metadata and controls
156 lines (110 loc) · 4.09 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.start();
}
}
class Calculator {
static final String INCORRECT_INPUT_MSG = "Нверный ввод. Попробуйте еще раз.";
Scanner scanner = new Scanner(System.in);
int personCount;
public void start() {
System.out.println("Добро пожаловать в калькулятор!\n");
personCount = 0;
while(personCount < 2) {
System.out.println("Укажите на скольких человек необходимо разделить счёт:");
try {
personCount = Integer.parseInt(scanner.nextLine());
if (personCount < 2)
System.out.println(INCORRECT_INPUT_MSG);
} catch (NumberFormatException e) {
System.out.println(INCORRECT_INPUT_MSG);
scanner.reset();
}
}
System.out.println("Теперь давайте сформируем список товаров. Для завершения ввода наберите 'Завершить'");
CommodArray commodArray = new CommodArray();
while(true) {
System.out.println("Введите имя товара");
String name = scanner.nextLine();
float price;
if(name.equalsIgnoreCase("завершить") || name.equals("e"))
break;
while(true) {
try {
System.out.println("Укажите стоимость товара в формате [рубли.копейки]");
price = Float.parseFloat(scanner.nextLine());
if (price <= 0.0f) {
System.out.println(INCORRECT_INPUT_MSG);
continue;
}
break;
} catch (NumberFormatException e) {
System.out.println(INCORRECT_INPUT_MSG);
}
}
// добавляем товар
commodArray.add(new Commodity(name, price));
}
commodArray.print();
System.out.printf("Общая стоимость товаров: %.2f %s. Количество человек: %d\nС каждого человека по %.2f %s\n",
commodArray.sum,
Formatter.format(commodArray.sum),
personCount,
(float)(commodArray.sum / personCount),
Formatter.format(commodArray.sum / personCount));
System.out.println("Программа завершена!");
}
}
class CommodArray {
ArrayList<Commodity> commods = new ArrayList<>();
float sum = 0.0f;
public void add(Commodity commod)
{
commods.add(commod);
sum += commod.price;
}
public void print()
{
System.out.println("Добавленные товары:");
Iterator<Commodity> it = commods.iterator();
while(it.hasNext()) {
Commodity c = it.next();
System.out.printf("%s %.2f %s\n", c.name, c.price, Formatter.format(c.price));
}
}
}
class Commodity {
String name;
float price;
public Commodity(String name, float price)
{
this.name = name;
this.price = price;
}
}
class Formatter {
static String format(float price)
{
String rubFormat = "рублей";
int tmp = Math.round((float) Math.floor(price)) % 100;
if((tmp < 10) || (tmp > 20))
{
switch (Math.round((float) Math.floor(price)) % 10) {
case 1:
rubFormat = "рубль";
break;
case 2:
case 3:
case 4:
rubFormat = "рубля";
break;
}
}
return rubFormat;
}
}