Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
116 lines (103 loc) · 4.67 KB
/
Copy pathMain.java
File metadata and controls
116 lines (103 loc) · 4.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.util.ArrayList;
import java.util.Scanner;
import java.util.InputMismatchException;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int person;
System.out.println("Доброго времени суток...");
while (true) {
System.out.println("На скольких распределяем счет? :");
// принимаем ввод и проверяем его корректность....
while (true) {
try {
person = scanner.nextInt();
break;
} catch (InputMismatchException ex) {
System.err.println("Некорректный ввод, нужно ввести число...");
scanner.nextLine(); // <----- advance the scanner
}
}
// конец проверки
//person=scanner.nextInt();
if (person<2) {
if (person==1) {System.out.println("На одного делить бессмысленно...");}
else {System.out.println("Это не серьезно....");}
} else {
Bill bill = new Bill();
bill.start(person);
break;
}
}
}
static class Tovar {
public String name;
public float cost;
}
static class Bill{
ArrayList<Tovar> listTovar = new ArrayList<>();
void start(int persons){
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Добавляем товар ? любой символ если да, если нет то 'Завершить'");
String command = scanner.nextLine();
if (command.equalsIgnoreCase("завершить")) {
System.out.println("Ведем расчет...");
calcCost(persons);
break;} else {
System.out.println("Наименование товара :");
String tNam = scanner.nextLine();
System.out.println("Стоимость :");
float tCost;
// Далее нужна проверка на корреткность ввода в принципе и на отрицательное число
while (true) {
try {
tCost=scanner.nextFloat();
if (tCost>0) break; else System.out.println("Стоимость не может быть отрицательной...");
} catch (InputMismatchException ex) {
System.out.println("Некорректный ввод, нужно ввести число...");
scanner.nextLine();
}
}
scanner.nextLine();
addTovar(tNam,tCost);}
}
}
void addTovar(String nam, float cos){
Tovar tovar=new Tovar();
tovar.name=nam;
tovar.cost=cos;
listTovar.add(tovar);
}
void calcCost(int p) {
System.out.println("Вывожу список приобретенного:");
float totalCost=0.00f;
String messTemp;
for (int i=0; i<listTovar.size(); i++) {
Tovar tempTovar = listTovar.get(i);
totalCost=totalCost+tempTovar.cost;
messTemp="Позиция "+i+ " наименование "+tempTovar.name+" стоимость %.2f "+Formatter((int)tempTovar.cost);
System.out.println(String.format(messTemp,tempTovar.cost));
}
messTemp="Итого приобретено на сумму : %.2f"+Formatter((int)totalCost);
System.out.println(String.format(messTemp,totalCost));
messTemp="Сумма на каждого %.2f";
float costPers = totalCost/p;
System.out.println(String.format(messTemp,costPers)+" "+Formatter((int)costPers));
}
String Formatter (int i) {
if ((i==11)||(i==12)||(i==13)||(i==14)) return " рублей";
int tI = i - (i / 10) * 10;
switch (tI) {
case 1:
return " рубль";
case 2:
case 3:
case 4:
return " рубля";
default:
return " рублей";
}
}
}
}