Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
87 lines (79 loc) · 3.49 KB
/
Copy pathMain.java
File metadata and controls
87 lines (79 loc) · 3.49 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.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Main prilozhenie = new Main();
prilozhenie.go();
}
public void go() {
while (true) {
int human = amountOfPeople();
if (human < 1) {
System.out.println("Некорректное значение для подсчета\nВведите корректное значение\n");
} else if (human == 1) {
System.out.println("Сумма поделенная на 1 останется неизменной\n");
} else if (human > 1) {
addOrCheck(human);
}
}
}
private int amountOfPeople() {
try {
System.out.println("На скольких человек необходимо разделить счёт?");
Scanner s = new Scanner(System.in);
return s.nextInt();
} catch (InputMismatchException e) {
System.out.println("Введите число человек\n");
return -1;
}
}
private void addOrCheck(int human) {
Calculator calculator = new Calculator();
while (true) {
System.out.println("Введите название товара или введите 'Завершить 'для завершения");
Scanner m = new Scanner(System.in);
String name = m.nextLine();
if (name.equalsIgnoreCase("Завершить")) {
printCheckAndCalculateSum(human, calculator);
break;
} else {
addProductToCalculator(name, calculator);
}
}
}
private void printCheckAndCalculateSum(int human, Calculator calculator) {
System.out.println("Добавленные товары:");
ArrayList<String> check = calculator.listProducts();
for (String product : check) {
System.out.println(product);
}
double sumOfCheck = calculator.summa();
double sumForPeople = sumOfCheck / human;
int lastDigit = (int) (sumForPeople % 10);
switch (lastDigit) {
case 1:
System.out.printf("\nСумма, которую должен заплатить каждый человек: %.2f рубль\n", sumForPeople);
break;
case 2:
case 3:
case 4:
System.out.printf("\nСумма, которую должен заплатить каждый человек: %.2f рубля\n", sumForPeople);
break;
default:
System.out.printf("\nСумма, которую должен заплатить каждый человек: %.2f рублей\n", sumForPeople);
}
}
private void addProductToCalculator(String name, Calculator calculator) {
try {
System.out.println("Введите цену товара в виде (рубли.копейки)");
Scanner n = new Scanner(System.in);
double price = Double.parseDouble(n.next());
Product product = new Product(name, price);
calculator.add(product);
System.out.println("Товар успешно добавлен в калькулятор\n");
} catch (NumberFormatException exception) {
System.out.println("Введите число типа double\n");
}
}
}