Repository navigation
Expand file tree
/
Copy pathCalc.java
More file actions
63 lines (55 loc) · 2.33 KB
/
Copy pathCalc.java
File metadata and controls
63 lines (55 loc) · 2.33 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
import java.util.Scanner; //вторая проверка
public class Calc {
public static int howManyPersons() {
Scanner scanner = new Scanner(System.in);
int numberOfPersons = 0;
System.out.println("На скольких людей разделить счет?");
while(true){
numberOfPersons = scanner.nextInt();
if(numberOfPersons > 1) {
scanner.close();
break;
}
System.out.println("Некорректный ввод. Введите количество персон");
}
return numberOfPersons;
}
public static String getProduct() {
String encoding = System.getProperty("console.encoding", "utf-8");
String priceList;
priceList = "Добавленные товары:\n";
String product = "";
float price = 0f;
float sum = 0f;
Scanner scanner = new Scanner(System.in, encoding);
while (true) {
System.out.println("Введите название товара");
product = scanner.nextLine();
System.out.println("Введите стоимость товара");
price = scanner.nextFloat();
scanner.nextLine();
priceList += String.format("%s - %.2f\n", product, price);
sum += price;
System.out.println("Товар успешно добавлен, если хотите завершить введите\"завершить\"");
if(scanner.nextLine().toLowerCase().equals("завершить")){
String ruble = wordRuble(sum);
sum = (sum / howManyPersons());
priceList += String.format("Каждый должен заплатить %.2f %s\n ", sum, wordRuble(sum));
break;
}
}
return priceList;
}
public static String wordRuble(float ruble){
int intRuble = (int) ruble;
if (intRuble >= 11 && intRuble <= 19)
return "рублей";
if (intRuble % 10 >= 2 && intRuble % 10 <= 4)
return "рубля";
else if (intRuble % 10 == 1)
return "рубль";
else if (intRuble % 10 >= 5 && intRuble % 10 <= 9 || intRuble % 10 == 0)
return"рублей";
return null;
}
}