Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
87 lines (83 loc) · 3.53 KB
/
Copy pathMain.java
File metadata and controls
87 lines (83 loc) · 3.53 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
import java.util.Locale;
import java.util.Scanner;
public class Main {
public static final Scanner scanner = new Scanner(System.in);
static int peopleAmount;
static double sum, price;
static String input = "", totalProducts = "", name, products = "";
public static void main(String[] args) {
scanner.useLocale(Locale.ENGLISH);
amount();
}
public static void amount() {
while (true){
System.out.println("На какое количество человек Вы планируете разделить счёт?");
try {
peopleAmount = Main.scanner.nextInt();
break;
} catch (Exception peopleAmount) {
System.out.println("Некорректное значение! Пожалуйста, повторите ввод");
scanner.nextLine();
}
}
biggerThan();
}
public static class Calculator {
public static void calculator() {
sum = 0.0;
while (true) {
try {
innerLogic();
totalProducts = products;
break;
} catch (Exception e) {
System.out.println("Некорректное значение! Пожалуйста, повторите ввод");
scanner.nextLine();
}
}
scanner.close();
System.out.println("Добавленные товары:" + totalProducts);
Format.format();
}
}
public static class Format {
public static void format() {
double total = Math.floor(sum / peopleAmount);
double totalPrice = sum / peopleAmount;
if (total == 1) {
System.out.printf("Каждый должен заплатить: %.2f рубль", totalPrice);
} else if (total <=4){
System.out.printf("Каждый должен заплатить: %.2f рубля", totalPrice);
} else {
System.out.printf("Каждый должен заплатить: %.2f рублей", totalPrice);
}
}
}
public static void innerLogic() {
while (!input.equalsIgnoreCase("Завершить")) {
System.out.println("Введите название товара");
name = scanner.next();
System.out.println("Введите стоимость товара с копейками\nНапример, 25.10");
price = scanner.nextDouble();
if (price > 0) {
sum += price;
System.out.println(name + " успешно добавлен!\nХотите добавить ещё один товар?\nДля завершения введите: \"Завершить\"");
input = scanner.next();
products += "\n" + name;
} else {
System.out.println("Некорректное значение! Пожалуйста, повторите ввод");
}
}
}
public static void biggerThan (){
if (peopleAmount > 1) {
Calculator.calculator();
} else {
while (peopleAmount <= 1) {
System.out.println("Некорректное значение! Пожалуйста, повторите ввод");
peopleAmount = scanner.nextInt();
}
Calculator.calculator();
}
}
}