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
138 lines (125 loc) · 5.48 KB
/
Main.java
File metadata and controls
138 lines (125 loc) · 5.48 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
CurrencyFormatter myCurrencyFormatter = new CurrencyFormatter();
System.out.println("На сколько людей разделить сумму?: ");
Calculator myCalculator = new Calculator(myCurrencyFormatter.nextPositiveIntegerHigherThan1FromSystem(scanner));
String stopWord = "";
while (true) {
String itemName;
System.out.println("Введите наименование вашего товара: ");
itemName = scanner.nextLine();
System.out.println("Имя товара принято");
System.out.println("Введите стоимость вашего товара: ");
myCalculator.addNewItemToTheList(itemName, myCurrencyFormatter.nextPositiveDoubleHigherThan0FromSystem(scanner));
System.out.println("Введите \"Завершить\", если закончили с составлением списка товаров, введите любой другой текст, если хотите ввести еще товар:");
stopWord = scanner.nextLine();
if (stopWord.equalsIgnoreCase("Завершить")) {
break;
}
}
System.out.println("Добавление товаров завершено\nДобавленные товары:");
myCalculator.showItems();
System.out.println("Сумма которую должен заплатить каждый человек:" + myCurrencyFormatter.showPrice(myCalculator.returnCheck()));
scanner.close();
}
}
class Calculator {
Integer amountOfPeople;
Double totalMoney;
ArrayList<Item> itemList = new ArrayList<>();
Calculator(Integer people) {
amountOfPeople = people;
totalMoney = 0.0;
}
void addNewItemToTheList(String name, Double price) {
itemList.add(new Item(name, price));
System.out.println("Товар добавлен в список");
totalMoney += price;
}
void showItems() {
for (int i = 0; i < itemList.size(); i++) {
System.out.println((i + 1) + "-товар: " + itemList.get(i).name);
}
}
double returnCheck() {
return totalMoney / amountOfPeople;
}
}
class CurrencyFormatter {
String showPrice(double price) {
int integerPartOfPrice = (int) price;
int changeValue = (int) ((price * 100) % 100); //валюты меньше копейки нет
String currencyPronunciation = " рублей ";
String changePronunciation = " копеек";
if ((integerPartOfPrice % 10) == 1) {
currencyPronunciation = " рубль ";
}
if (((integerPartOfPrice % 10) > 1) && ((integerPartOfPrice % 10) <= 4)) {
currencyPronunciation = " рубля ";
}
if (10 < ((integerPartOfPrice % 100)) && ((integerPartOfPrice % 100) < 15)) {
currencyPronunciation = " рублей ";
}
if ((changeValue % 10) == 1) {
changePronunciation = " копейка";
}
if (((changeValue % 10) > 1) && ((changeValue % 10) <= 4)) {
changePronunciation = " копейки";
}
if ((10 < changeValue) && (changeValue < 15)) {
changePronunciation = " копеек";
}
return (" " + integerPartOfPrice + currencyPronunciation + changeValue + changePronunciation);
}
Integer nextPositiveIntegerHigherThan1FromSystem(Scanner sn) {
String myLine;
Integer myNumber = null;
boolean incorrectInput = true;
while (incorrectInput) {
myLine = sn.nextLine();
try {
myNumber = Integer.parseInt(myLine);
} catch (NumberFormatException e) {
System.err.println("Неадекватное значение. Вводите только целые числа больше 1: " + e.getMessage());
continue;
}
if (myNumber > 1) {
incorrectInput = false;
} else {
System.err.println("Неадекватная величина целого числа. Вводите только целые числа больше 1: " + myLine);
}
}
return myNumber;
}
Double nextPositiveDoubleHigherThan0FromSystem(Scanner sn) {
String myLine;
Double myNumber = null;
boolean incorrectInput = true;
while (incorrectInput) {
myLine = sn.nextLine();
try {
myNumber = Double.parseDouble(myLine);
if (myNumber > 0.0) {
incorrectInput = false;
} else {
System.err.println("Неадекватная величина дробного числа. Вводите только дробные числа больше 0: " + myLine);
}
} catch (NumberFormatException e) {
System.err.println("Неадекватное значение. Вводите только дробные числа больше 0: " + e.getMessage());
continue;
}
}
return myNumber;
}
}
class Item {
String name;
Double price;
Item(String itemName, Double itemPrice) {
name = itemName;
price = itemPrice;
}
}