Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
134 lines (122 loc) · 4.98 KB
/
Copy pathMain.java
File metadata and controls
134 lines (122 loc) · 4.98 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Console.console();
}
}
class Product{
private String name;
private double price;
Product(String name, double price){
this.name = name;
this.price = price;
}
public String getName(){
return name;
}
public double getPrice() {
return price;
}
}
class Console{
static int countHuman;
static Scanner scanner = new Scanner(System.in);
static void console() {
System.out.println("Привет! я могу помочь рассчитать ваши расходы");
System.out.println("Для начала мне нужно знать сколько человек в компании.");
while(true){
System.out.println("Введи сколько вас собралось: ");
String isCount = scanner.next();
try {
countHuman = Integer.parseInt(isCount);
if(countHuman<1){
System.out.println("Попробуй пересчитать");
}
else if (countHuman>1){
System.out.println("Итак вас "+countHuman+" Далее тебе нужно ввести название товара и его стоимость\nКогда закончишь вводить товар напиши слово 'Завершить' и я посчитаю сколько необходимо заплатить каждому");
new Calculator(countHuman);
break;
}
else {
System.out.println("Похоже, что твои друзья не смогли сегодня прийти. Придётся самому за всё платить");
break;
}
}
catch (NumberFormatException ime){
System.out.println("Попробуй ввести число");
}
}
}
}
class Calculator{
static private int humans;
static List<Product> listProduct = new ArrayList<>();
static Scanner scanner = new Scanner(System.in);
Calculator(int humans){
this.humans = humans;
chooseProduct();
}
static void chooseProduct() {
String name = "start";
System.out.println("Если у цены товара есть дробная часть отдели её символом '.' Например: '1.45'");
while (!name.equalsIgnoreCase("Завершить")){
System.out.println("Введи название товара: ");
name = new String(scanner.next());
if(name.equalsIgnoreCase("Завершить")){
break;
}
while(true){
System.out.println("Введи цену на товар: ");
String isPrice = scanner.next();
try {
double price = Double.parseDouble(isPrice);
if (price > 0) {
listProduct.add(new Product(name, price));
System.out.println("Товар добавлен");
break;
}
else {
System.out.println("Кажется ты что-то перепутал. Цена не может быть отрицательной попробуй ввести её ещё раз");
}
}
catch (NumberFormatException ime){
System.out.println("Попробуй ввести цену на товар заного, согласно требованиям");
}
}
}
scanner.close();
String prod;
System.out.println("Добавленные товары: ");
for(Product product: listProduct){
prod = product.getName()+" - "+String.format("%.2f", product.getPrice())+Formater.formater(product.getPrice());
System.out.println(prod);
}
System.out.println("Итого с каждого по: "+ String.format("%.2f",calculate(humans))+Formater.formater(calculate(humans)));
}
static double calculate(int humans){
double sum = 0;
for(Product prices:listProduct){
sum =sum + prices.getPrice();
}
return sum/humans;
}
}
class Formater{
static String formater(double price){
int priceRound = (int) Math.floor(price);
if(priceRound%10==1 && priceRound/10!=1 || priceRound == 1){
return " рубль";
}
else if(priceRound%10>1 && priceRound%10<5 && priceRound/10!=1 || priceRound>1 && priceRound<5){
return " рубля";
}
else{
return "рублей";
}
}
}