forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
104 lines (94 loc) · 4.11 KB
/
Main.java
File metadata and controls
104 lines (94 loc) · 4.11 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("На скольких человек необходимо разделить счет?");
int people;
while (true) {
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextInt()) {
people = scanner.nextInt();
if (people > 1) {
break;
} else if (people == 1) {
System.out.println("В этом случае нет смысла ничего считать и делить");
} else if (people < 1) {
System.out.println("Это некорректное значение для подсчёта");
}
} else {
System.out.println("Ошибка");
scanner.reset();
}
}
String name;
float price;
float totalPrice = 0;
String allProducts ="";
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println("Введите название товара");
name = scanner.nextLine();
scanner.reset();
System.out.println("Введите стоимость товара");
if (scanner.hasNextFloat()) {
price = scanner.nextFloat();
if (price <= 0) {
System.out.println("Oшибка");
} else {
Scanner scan = new Scanner(System.in);
totalPrice = totalPrice + price;
allProducts = allProducts + " " + name;
System.out.println("Товар успешно добавлен");
System.out.println("Хотите ли вы добавить еще один товар? Если нет, напишите \"Завершить\"");
String complete = scan.nextLine();
scan.reset();
if (complete.equalsIgnoreCase("Завершить")) {
System.out.println("Добавленные товары:" +allProducts);
String conclusion = "Нужно заплатить каждому: %.2f %s";
Ruble declination = new Ruble();
System.out.println(String.format(conclusion, totalPrice/people, declination.declination(totalPrice/people)));
break;
}
}
} else {
System.out.println("Ошибка");
scanner.reset();
}
}
}
}
class Ruble {
String declination(float i) { // totalPrice/people
String form = "%.0f";
String totalStringPrice = String.format(form, i);
int lastNumber;
if (totalStringPrice.length() == 1) {
lastNumber = Integer.parseInt(totalStringPrice);
if (lastNumber == 0) {
return "рублей";
} else if (lastNumber == 1) {
return "рубль";
} else if (lastNumber > 1 && lastNumber < 5) {
return "рубля";
} else {
return "рублей";
}
} else if (totalStringPrice.length() > 1) {
lastNumber = Integer.parseInt(totalStringPrice.substring(totalStringPrice.length() - 2));
if (lastNumber < 21) {
return "рублей";
} else {
lastNumber = lastNumber % 10;
if (lastNumber == 0) {
return "рублей";
} else if (lastNumber == 1) {
return "рубль";
} else if (lastNumber > 1 && lastNumber < 5){
return "рубля";
} else {
return "рублей";
}
}
}
return "Ошибка";
}
}