Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
150 lines (123 loc) · 5.58 KB
/
Copy pathMain.java
File metadata and controls
150 lines (123 loc) · 5.58 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
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static float totalCost = 0;
public static void main(String[] args) {
int quantity = firstInput();
Map receipt = secondInput();
thirdOutput(quantity, receipt);
}
//Функция первой части.
//На сколько людей будем делить счет
static int firstInput() {
Scanner in = new Scanner(System.in);
System.out.println("На скольких человек необходимо разделить счет?");
boolean proverka = true;
int quantity = 0;
while (proverka) {
if (in.hasNextInt()) {
quantity = in.nextInt();
if (quantity <= 1) {
System.out.println("Вы ошиблись, вы ввели меньше 2, а именно: " + quantity + "\n Пожалуйста повторите ввод.");
} else {
proverka = false;
}
} else {
System.out.println("Вы ошиблись, вы ввели " + in.next() + " вместо целого числа. \n Пожалуйста повторите ввод.");
}
}
return quantity;
}
//Функция второй части
//Что было заказано и по какой стоимости, с точностью до копеек.
static Map secondInput(){
boolean checkCost = true;
boolean checkEnter = true;
float cost = 0;
// int count = 0;
String product = "";
Map<String,Float> receipt = new HashMap<String,Float>();
System.out.println("Введите заказанное блюдо-товар.");
while (checkEnter){
Scanner in = new Scanner(System.in);
//Товар
product = in.nextLine(); // Товар может быть с пробелом в имени
if(product.toLowerCase().equals("завершить")){
checkEnter = false;
break;
}
//Стоимость
System.out.println("Введите стоимость до копеек(.00).");
while (checkCost) {
if(!(in.hasNextFloat())){
System.out.println("Вы ввели " + in.next() + " это не похоже на стоимость.\n Пожалуйста повторите ввод.");
}
else {
cost = Math.round(in.nextFloat()*100)/100.0f;
if(cost > 0){
checkCost = false;
}
else{
System.out.println("Вы ввели " + cost + ", это неправильная стоимость.\n Пожалуйста повторите ввод.");
}
}
}
//Добавим в массив
receipt.put(product, cost);
totalCost += cost;
System.out.println("Вы добавили в чек " + product + " по стоимости " + cost);
System.out.println("Если вы хотите завершить ввод - наберите \"Завершить\" или введите новый товар.");
checkCost = true;
// count += 1;
// if(count == 5){
// break;
// }
}
return receipt;
}
//Процедура третей части
static void thirdOutput(int quantity, Map receipt){
float perCost;
int intPerCost;
String sRubOne = "рубль";
String sRubls = "рубля";
String sMoreRubls = "рублей";
System.out.println("Добавленные товары:");
Iterator<Map.Entry<String, Float>> iterator = receipt.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String, Float> entry = iterator.next();
String product = entry.getKey();
Float cost = entry.getValue();
// System.out.println(String.format(text,product,cost));
System.out.println(product + " стоит " + cost);
}
perCost = totalCost / quantity;
if (perCost * (double)(quantity) < totalCost){
perCost += 0.01f;
}
intPerCost = (int) Math.ceil(perCost);
System.out.println(String.format("Общая стоимость счета " + totalCost + ", каждый должен внести %.2f %s или %d %s", perCost, GetRubls(intPerCost), intPerCost, GetRubls(intPerCost)));
}
public static String GetRubls(int num){
int preLastDigit = num % 100/10;
if(preLastDigit == 1){
return "рублей";
}
switch(num % 10){
case 1:
return "рубль";
case 2:
case 3:
case 4:
return "рубль";
default:
return "рублей";
}
}
}
// Напоминалка "Калькулятор счета"
// 1. Задаем вопросы в консоли
// 2. Считаем пользовательский ввод
// 3. Показываем список товаров и показываем сумму каждого из друзей