Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
95 lines (86 loc) · 4.09 KB
/
Copy pathMain.java
File metadata and controls
95 lines (86 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
import java.util.PrimitiveIterator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
Scanner scanner = new Scanner(System.in);
System.out.print("Введите количество человек, между которыми нужно разделить счёт: ");
int persons;
String input = scanner.next();
// защита от присваивания некорректных значений
while (!isValidInt(input)) {
System.out.print("\nНекорректное число! Количество человек должно быть целым, положительным числом! Попробуйте ещё раз: ");
input = scanner.next();
}
persons = Integer.parseInt(input);
Bill bill = new Bill();
String completion = "";
while (completion.compareToIgnoreCase("Завершить") != 0) {
System.out.print("\nВведите название товара: ");
bill.addToList(scanner.next());
System.out.print("Введите стоимость товара: ");
input = scanner.next();
while (!isValidDouble(input)) {
System.out.print("\nНекорректное число! Стоимость должна быть выражена положительным числом (разделитель целой и дробной части - точка)! Попробуйте ещё раз: ");
input = scanner.next();
}
bill.addToTotal(Double.parseDouble(input));
System.out.print("Товар успешно добавлен в список! Хотите добавить ещё что-нибудь? (Для рассчёта равных долей введите \"Завершить\"): ");
completion = scanner.next();
}
System.out.println("-----------------------------------------------------");
bill.displayTotalList();
System.out.println("----------");
bill.displayTotalCost();
System.out.println(" " + messageEndingHandler(bill.total));
System.out.println("-----------------------------------------------------");
Calculator payment = new Calculator(persons,bill);
System.out.println(" " + messageEndingHandler(payment.billPerPerson));
}
// проработка окончания слова "рубль" в зависимости от суммы
public static String messageEndingHandler(double number) {
String ending;
String message = "рубл";
int preLastDigit = (int) number % 100 / 10;
if (preLastDigit == 1)
{
ending = "ей";
}
switch ((int) number % 10)
{
case 1:
ending = "ь";
break;
case 2:
case 3:
case 4:
ending = "я";
break;
default:
ending = "ей";
break;
}
return message += ending;
}
// проверка значения на целочисленность и положительность
public static boolean isValidInt(String str){
if (str != null && str.matches("[0-9]+") ) {
if (Integer.parseInt(str) <= 0 ) {
return false;
} else
return true;
} else
return false;
}
// проверка значения на вещественность и положительность
public static boolean isValidDouble(String str){
if (str != null && str.matches("[0-9.]+") ) {
if (Double.parseDouble(str) <= 0 ) {
return false;
} else
return true;
} else
return false;
}
}