Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
119 lines (103 loc) · 4.3 KB
/
Copy pathMain.java
File metadata and controls
119 lines (103 loc) · 4.3 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
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
PrintStream out = null;
Integer peopleInt;
LogicCalc item = new LogicCalc();
try {
out = new PrintStream(System.out, true, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
while(true) {
Scanner scanner = new Scanner(System.in);
out.println("На скольких человек необходимо разделить счёт?");
try {
peopleInt = scanner.nextInt();
if(peopleInt > 1){
break;
}else{
out.println("Ошибка. Нет сысла в счете. Попробуйте заново.\n");
}
}catch (Exception e){
out.println("Ошибка. Введите число.\n");
scanner.reset();
}
}
while(true) {
Scanner scanner = new Scanner(System.in);
String nameItem = "";
out.println("Напишите название товара?");
nameItem = scanner.next();
scanner.reset();
out.println("Напишите стоимость товара? Формат: 10.45, где 10 - руб, 45 - копейки");
if (scanner.hasNextFloat()) {
float cost = scanner.nextFloat();
if(cost > 0) {
item.addItem(nameItem, cost);
out.println("Товар успешно добавлен");
out.println("Хотите ли добавить ещё один товар? Если хотите завершить программу напишите: \"Завершить\"");
String exit = scanner.next();
if (exit.equalsIgnoreCase("Завершить")) {
break;
}
}else{
out.println("Ошибка. Отрицательная или нулевая стоимость товара. Попробуйте снова!\n");
}
} else {
out.println("Извините, но это явно не число. Попробуйте снова!\n");
}
}
ArrayList<String> allItems = item.outputAllItem();
for(int i = 0; i < allItems.size(); i++){
out.println("Добавленные товары: " + allItems.get(i));
}
String rubleSuffix;
Double allcostDouble = item.outputAllCost()/peopleInt;
rubleSuffix = rubleRight(allcostDouble);
String template = "\nКаждый человек должен заплатить по %.2f %s";
out.println(String.format(template, allcostDouble, rubleSuffix));
}
private static boolean rightCost() {
return true;
}
private static String rubleRight(Double allcost){
allcost = Math.floor(allcost);
//String allc = String.valueOf(allcost);
String template = "%.0f";
String allc = String.format(template, allcost);
int lastDigit;
if(allc.length() == 1){
lastDigit = Integer.parseInt(allc);
if(lastDigit == 0){
return "рублей";
}else if(lastDigit == 1){
return "рубль";
}else if(lastDigit > 1 && lastDigit < 5){
return "рубля";
}else{
return "рублей";
}
}else if (allc.length() > 1){
lastDigit = Integer.parseInt(allc.substring(allc.length() - 2));
if(lastDigit < 21){
return "рублей";
}else{
lastDigit = lastDigit % 10;
if(lastDigit == 0){
return "рублей";
}else if(lastDigit == 1){
return "рубль";
}else if(lastDigit > 1 && lastDigit < 5){
return "рубля";
}else{
return "рублей";
}
}
}
return "";
}
}