forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
53 lines (48 loc) · 1.72 KB
/
Calculator.java
File metadata and controls
53 lines (48 loc) · 1.72 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
import java.util.Scanner;
public class Calculator {
double sum = 0;
String names = "";
int count;
Scanner scan = new Scanner(System.in);
public int countOfGuest() {
System.out.println("Введите количество человек, которые будут делить счёт:");
while (true) {
if (scan.hasNextInt()) {
count = scan.nextInt();
if (count <= 1) {
System.out.println("Введено отрицательное значение или 1. Повторите ввод.");
} else {
return count;
}
} else {
System.out.println("Введено не корректное значение. Повторите ввод.");
}
scan.nextLine();
}
}
public void add(String name, double cost) {
sum += cost;
this.names = this.names + name + "\n";
System.out.println("Товар успешно добавлен.");
}
public void costFromOne(int count){
System.out.println("Добавленные товары: \n" + names );
sum /= count;
System.out.println("Каждый человек должен заплатить: \n" + String.format("%.2f", sum) + formOfRuble());
}
public String formOfRuble(){
if ((int)(sum % 100 / 10) == 1){
return " рублей.";
}
switch ((int) (sum % 10)){
case 1:
return " рубль.";
case 2:
case 3:
case 4:
return " рубля.";
default:
return " рублей.";
}
}
}