Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
95 lines (85 loc) · 3.77 KB
/
Copy pathMain.java
File metadata and controls
95 lines (85 loc) · 3.77 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int numberPerson = enterNumberFriends(scanner);
InvoiceCalculator invoiceCalculator = new InvoiceCalculator();
billEntry(scanner, invoiceCalculator);
invoiceCalculator.showList();
formatTotalPrice(invoiceCalculator, numberPerson);
scanner.close();
}
public static int enterNumberFriends(Scanner scanner) {
System.out.print("На сколько друзей разделить счёт: ");
while (true) {
if (scanner.hasNextInt()) {
int numberPeople = scanner.nextInt();
if (numberPeople <= 1) {
System.out.print("Введите корректное значение: ");
} else if (numberPeople > 1) {
return numberPeople;
}
} else if (scanner.hasNextLine()) {
System.out.print("Введите целое число: ");
scanner.nextLine();
}
}
}
public static void billEntry(Scanner scanner, InvoiceCalculator invoiceCalculator) {
while (true) {
String productName = scanner.nextLine();
double price;
System.out.print("Введите наименование товара: ");
while (true) {
if (scanner.hasNextLine()) {
productName = scanner.nextLine();
break;
} else System.out.println("Вы ошиблись, введите наименование товара еще раз");
}
System.out.print("Введите стоимость товара: ");
while (true) {
if (scanner.hasNextDouble()) {
price = scanner.nextDouble();
if (price <= 1) {
System.out.print("Введите корректное значение: ");
} else if (price > 1) {
break;
}
} else if (scanner.hasNextLine()) {
System.out.print("Вы ошиблись, введите стоимость товара еще раз: ");
scanner.nextLine();
}
}
invoiceCalculator.addProduct(new Product(productName, price));
System.out.println("Товар добавлен. Хотите продолжить - введите любой не пробельный символ, иначе - \"Завершить\"");
if (scanner.next().equalsIgnoreCase("Завершить")) {
break;
}
}
}
public static void formatTotalPrice(InvoiceCalculator invoiceCalculator, int numberPerson) {
int roundWhole = (int) Math.floor(invoiceCalculator.totalPrice() / numberPerson);
String formatRuble = "рубль";
int lastDigit = roundWhole % 10;
int twoLastDigits = roundWhole % 100;
if (twoLastDigits > 10 && twoLastDigits < 19) {
formatRuble = "рублей";
} else {
switch (lastDigit) {
case 1:
formatRuble = "рубль";
break;
case 2:
case 3:
case 4:
formatRuble = "рубля";
break;
default:
formatRuble = "рублей";
break;
}
}
String format = String.format("Итого каждому к оплате: %.2f %s", invoiceCalculator.totalPrice() / numberPerson, formatRuble);
System.out.println(format);
}
}