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
50 lines (49 loc) · 1.89 KB
/
Calculator.java
File metadata and controls
50 lines (49 loc) · 1.89 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
import java.util.Scanner;
public class Calculator {
double sum = 0;
String name = "";
int quantity;
Scanner scanner = new Scanner(System.in);
public int quantityOfGuest(){
System.out.println("Введите количество людей для разделения счета: ");
while (true) {
if (scanner.hasNextInt()) {
quantity = scanner.nextInt();
if (quantity<=1){
System.out.println("Введено отрицательное значение или 1.Повторите ввод.");
}else {
return quantity;
}
} else {
System.out.println("Введено не корректное значение.Повторите ввод.");
}
scanner.nextLine();
}
}
public void cheque(String name, double cost) {
if (cost < 1) {
System.out.println("Товар не добавлен.\nТовар не может быть бесплатным.Повторите ввод.");
} else {
sum += cost;
this.name = this.name + name + "\n";
System.out.println("Товар успешно добавлен.");
}
}
public void finalAccount(int quantity){
System.out.println("Добавленные товары: \n" + name );
sum = sum / quantity;
System.out.println("Каждый человек должен : \n" + String.format("%.2f",sum) + price());
}
public String price(){
if ((int)(sum % 100 / 10) == 1){
return " рублей.";
}
if ((int) (sum % 10) == 1){
return " рубль.";
} else if ((int) (sum % 10) >= 2 && (int) (sum % 10) <= 4){
return " рубля.";
} else {
return " рублей.";
}
}
}