Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
125 lines (112 loc) · 4.01 KB
/
Copy pathMain.java
File metadata and controls
125 lines (112 loc) · 4.01 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static double getDouble (Scanner scanner) {
try {
return scanner.nextDouble();
}
catch (InputMismatchException e) {
scanner.next();
return 0;
}
}
public static int getInt (Scanner scanner) {
try {
return scanner.nextInt();
}
catch (InputMismatchException e) {
scanner.next();
return 0;
}
}
public static class Formatter {
void printWithRubles (double x) {
int cutPoint = (int) Math.floor(x);
int rest = cutPoint % 10;
int rest1 = cutPoint % 100;
if (rest1 >= 10 && rest1 <= 19) {
System.out.printf("%.2f Рублей", x);
}
else if (rest == 0 || rest >= 5) {
System.out.printf("%.2f Рублей", x);
}
else if (rest == 1) {
System.out.printf("%.2f Рубль", x);
}
else if (rest >= 2) {
System.out.printf("%.2f Рубля", x);
}
}
}
public static class Product {
String name;
double price;
Product(String productName, double productPrice) {
name = productName;
price = productPrice;
}
}
public static class Calculator {
String productNames = "";
double productPrices = 0;
void addProduct(Product p) {
productPrices = productPrices + p.price;
productNames = productNames.concat(p.name).concat("\n");
System.out.println("Товар успешно добавлен!");
}
double divideByAll (int guests) {
return productPrices / guests;
}
}
public static int getGuests(Scanner scanner) {
int guests;
while (true) {
System.out.println("На сколько человек необходимо разделить счёт?");
guests = getInt(scanner);
if (guests > 1) {
break;
} else if (guests == 1) {
System.out.println("нет смысла ничего считать и делить");
} else {
System.out.println("Это некорректное значение для подсчёта");
}
}
return guests;
}
public static void getProducts(Scanner scanner, Calculator calc) {
while (true) {
System.out.println("Введите наименование товара или 'Завершить'");
String productName = scanner.next();
if (productName.equalsIgnoreCase("завершить")) {
break;
}
else {
while (true) {
System.out.println("Введите стоимость товара");
double productPrice = getDouble(scanner);
if (productPrice > 0) {
Product newProduct = new Product(productName, productPrice);
calc.addProduct(newProduct);
break;
}
else {
System.out.println("Это некорректное значение для подсчёта");
}
}
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int guests = getGuests(scanner);
Calculator calc = new Calculator();
getProducts(scanner, calc);
System.out.println("Добавленные товары:");
System.out.println(calc.productNames);
double result = calc.divideByAll(guests);
System.out.print("Каждый должен заплатить по: ");
Formatter f = new Formatter();
f.printWithRubles(result);
scanner.close();
}
}