Repository navigation
Expand file tree
/
Copy pathFormatter.java
More file actions
89 lines (75 loc) · 3.36 KB
/
Copy pathFormatter.java
File metadata and controls
89 lines (75 loc) · 3.36 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
import java.util.Scanner;
public class Formatter {
private final Scanner scanner;
public Formatter(Scanner scanner) {
this.scanner = scanner;
}
public int scanNumberOfPersons() {
int numberOfPersons;
System.out.println("На скольких человек необходимо разделить счёт?");
String messageError = "Количество человек неверное, введите количество больше или равно 2";
while (true) {
if (!scanner.hasNextInt()) {
System.out.println(messageError);
getString();
} else {
numberOfPersons = scanner.nextInt();
if (numberOfPersons < 2){
System.out.println(messageError);
getString();
} else {
break;
}
}
}
getString(); // Очистка буфера ввода после Чтение числового значения из ввода
return numberOfPersons;
}
public String getString(){
return scanner.nextLine();
}
public double getProductPrice(String msgFirst, String msgRightPrice){
double price;
System.out.println(msgFirst + " " + msgRightPrice);
String msgErrorPrice = "Вы ввели некорректный формат стоимости.";
while (true) {
if (!scanner.hasNextDouble()){
System.out.println(msgErrorPrice + "\n" + msgRightPrice);
getString();
} else {
price = scanner.nextDouble();
price = Math.round(price * 100.0) / 100.0;
if (price <= 0.00){
System.out.println(msgErrorPrice + "\n" + msgRightPrice + " и больше 0");
} else {
break;
}
}
}
getString(); // Очистка буфера ввода после Чтение числового значения из ввода
return price;
}
public boolean checkExitCommand(String command) {
return command.equalsIgnoreCase("завершить");
}
public double getDividedPrice(double totalPrice, int numberOfPersons) {
double dividedPrice = totalPrice / numberOfPersons;
dividedPrice = Math.round(dividedPrice * 100.0) / 100.0; //Округление до 2 знаков после запятой
return dividedPrice;
}
public String getCurrencyDecline(double amount){
String currencyName;
int lastNumberBeforePoint = (int) (Math.floor(amount) % 10); // единицы числа
int nextToLastNumberBeforePoint = (int) (Math.floor(amount) / 10 % 10); // десятки числа
if (nextToLastNumberBeforePoint == 1) { // если число вышло от 10 до 19, то падеж будет как у 0 рублей
lastNumberBeforePoint = 0;
}
switch (lastNumberBeforePoint) {
case 1 -> currencyName = "рубль";
case 2, 3, 4 -> currencyName = "рубля";
case 5, 6, 7, 8, 9, 0 -> currencyName = "рублей";
default -> currencyName = "руб";
}
return currencyName;
}
}