forked from Yandex-Practicum/Java-Module-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
90 lines (70 loc) · 3.4 KB
/
Main.java
File metadata and controls
90 lines (70 loc) · 3.4 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
import java.util.Scanner;
public class Main {
private static final Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Делитель счёта v.7. Привет!");
Calculate cal = new Calculate();
cal.inputPersons();
cal.inputItems();
cal.payPeople();
}
static class Calculate{
double price=0;
double total=0;
int persons=0;
String items= "";
int payPerPerson;
String endRuble;
public void inputPersons(){
while(true){
System.out.println("Введите количество человек:");
if (sc.hasNextInt()) {
persons = sc.nextInt();
}if (persons>=2){
break;
}else{
System.out.println("Некорректный ввод. Попробуйте ещё раз.");
sc.nextLine();
}
}
}
public void inputItems(){
while(true){
System.out.println("Введите название товара:");
String item=sc.next()+sc.nextLine();
System.out.println("Введите цену товара:");
if (sc.hasNextDouble()) {
price = sc.nextDouble();
}if (price>2){
total+=price;
items+=item+"\n";
}else{
System.out.println("Неверный формат ввода!");
sc.nextLine();
}
System.out.println("Товар " + item + " добавлен. Добавим ещё? Да/Нет?");
String getMore=sc.next();
if (getMore.equalsIgnoreCase("нет")) {
break;
}else{
}
}
}
public void payPeople(){
System.out.println("Добавленные товары: \n"+items);
payPerPerson=(int)total/persons;
double pppWithoutRemains=(double) payPerPerson; // по другому я просто не знаю как игнорировать копейки при объявлении окончания рубля, поэтому пришлось убрать их вовсе,
endRubles();//другого способа как их заигнорить я не нашёл, так что не судите строго.
System.out.println("Каждый из " + persons + " человек заплатит по " + String.format("%.2f",pppWithoutRemains)+ " " +endRuble+ ".");
}
public void endRubles() {
if (payPerPerson % 10 == 1) {
endRuble = "рубль";
} else if (payPerPerson % 10 == 2 || payPerPerson % 10 == 3 || payPerPerson % 10 == 4) { // копейки всё портят.
endRuble = "рубля";
} else {
endRuble = "рублей";
}
}
}
}