This repository was archived by the owner on Mar 25, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
81 lines (70 loc) · 3.24 KB
/
Copy pathMain.java
File metadata and controls
81 lines (70 loc) · 3.24 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
import org.w3c.dom.ls.LSOutput;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Calculator calculator = new Calculator();
System.out.print("На сколько человек разделить счет? ");
int numberOfPersons;
while (true) {
if (sc.hasNextInt()) {
numberOfPersons = sc.nextInt();
if (numberOfPersons > 1) {
System.out.println();
sc.nextLine();
break;
} else {
System.out.print("Число должно быть больше 1. Попробуйте еще раз: ");
sc.nextLine();
}
} else {
System.out.print("Необходимо ввести целое число. Попробуйте еше раз: ");
sc.nextLine();
}
}
while (true) {
Product p = new Product();
System.out.print("Введите наименование товара: ");
p.setName(sc.nextLine());
System.out.print("Введите цену товара в формате \"12,34\": ");
while (true) {
if (sc.hasNextDouble()) {
p.setPrice(sc.nextDouble());
if (p.getPrice() < 0) {
System.out.print("Цена товара должна быть не меньше 0. Попробуйте еще раз: ");
sc.nextLine();
} else {
System.out.println();
break;
}
} else {
System.out.print("Цена товара должна быть в формате \"12,34\". Попробуйте еще раз: ");
sc.nextLine();
}
}
calculator.addProduct(p);
System.out.print("Если хотите продолжить ввод, введите любой текст. Если хотите завершить ввод товаров, введите \"Завершить\": ");
if (sc.next().equalsIgnoreCase("завершить")) {
System.out.println();
break;
} else {
System.out.println();
sc.nextLine();
}
}
System.out.println("Полный список товаров:");
System.out.println(calculator.allProducts);
System.out.printf("Общая стоимость товаров: %s%n", sumWithCurrency(calculator.totalPrice));
System.out.printf("Каждый должен по %s%n", sumWithCurrency(calculator.calculateShare(numberOfPersons)));
}
private static String sumWithCurrency(double sum) {
int digit = (int) sum % 10;
if ((sum > 5 && sum <= 20) || digit == 0 || digit > 4) {
return String.format("%.2f рублей", sum);
} else if (digit == 1) {
return String.format("%.2f рубль", sum);
} else {
return String.format("%.2f рубля", sum);
}
}
}