Repository navigation
Expand file tree
/
Copy pathCount.java
More file actions
87 lines (81 loc) · 3.51 KB
/
Copy pathCount.java
File metadata and controls
87 lines (81 loc) · 3.51 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
import java.util.ArrayList;
import java.util.Scanner;
public class Count {
int peoples;
ArrayList<Good> goods = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
public void count() {
getPeoples();
getGoods();
calculate();
}
public void getPeoples(){
System.out.println("На сколько человек разделить счет?");
while (true) {
if (scanner.hasNextInt()) {
int number = scanner.nextInt();
if (number == 1) {
System.out.println("Расчет для одного человека не производится, введите корректное число человек");
scanner.nextLine();
} else if (number < 1) {
System.out.println("Введите корректное число человек");
scanner.nextLine();
} else {
peoples = number;
scanner.nextLine();
break;
}
}
else
{
System.out.println("Необходимо ввести целое число");
scanner.nextLine();
}
}
}
public void getGoods() {
while (true) {
String name = "";
double price;
while (name.length() == 0) {
System.out.println("Введите название товара");
name = scanner.nextLine().toUpperCase().trim();
}
System.out.println("Введите цену товара");
while (true) {
if (scanner.hasNextDouble()) {
price = scanner.nextDouble();
if (price <= 0) {
System.out.println("Цена должна быть больше 0, введите корректную цену");
scanner.nextLine();
} else {
break;
}
} else {
System.out.println("Необходимо ввести корректную цену");
scanner.nextLine();
}
}
goods.add(new Good(name, price));
System.out.format("Вы добавили товар %s с ценой %s \nВведите любой символ, чтобы продолжить, или \"завершить\", чтобы перейти к подсчету", name, Formatter.complexFormat(price));
System.out.println();
scanner.nextLine();
if(scanner.nextLine().equalsIgnoreCase("завершить")) break;
}
}
public void calculate(){
double sum = 0.00;
System.out.println("У вас " + goods.size() + Formatter.declension(goods.size()) + ":");
for(int i = 0; i < goods.size(); i++) {
Good good = goods.get(i);
sum = sum + good.price;
System.out.println((i+1) + ". " + good.name);
}
System.out.println("Общая сумма составляет " + Formatter.complexFormat(sum));
System.out.println("Каждый человек должен заплатить " + Formatter.complexFormat(sum / peoples) + "\nВведите любой символ, чтобы завершить программу");
while (true) {
scanner.nextLine();
break;
}
}
}