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
57 lines (54 loc) · 2.23 KB
/
Calculator.java
File metadata and controls
57 lines (54 loc) · 2.23 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
import java.util.Scanner;
public class Calculator {
Scanner scanner = new Scanner(System.in);
double sum;
String goods = "";
String currency;
void addGoods() {
while (true) {
System.out.println("Введите название товара: ");
String addProduct = scanner.next();
goods = goods + addProduct + "\n" ;
getPrice();
System.out.println("Хотите добавить еще один товар? Если нет, введите \"Завершить\"");
String answer = scanner.next();
if (answer.equalsIgnoreCase("Завершить")) {
break;
}
}
}
void getPrice() {
while (true) {
System.out.println("Введите цену товара:");
if (scanner.hasNextDouble()) {
double productPrice = scanner.nextDouble();
if (productPrice > 0) {
sum += productPrice;
System.out.println("Товар успешно добавлен!");
break;
} else {
System.out.println("Ошибка! Цена товара может быть только положительной!");
}
} else {
System.out.println("Ошибка! Введите корректную цену товара!");
scanner.next();
}
}
}
void calculatePayment(int numberOfPersons) {
System.out.println("Добавленные товары:\n" + goods);
double sumPerPerson = sum / numberOfPersons;
String sumToPay = String.format("Сумма к оплате на человека: %.2f", sumPerPerson);
int sumToInt = (int)Math.floor(sumPerPerson);
int secondLastDigit = (sumToInt % 100) / 10;
int lastDigit = sumToInt % 10;
if (lastDigit == 1 && secondLastDigit != 1) {
currency = " рубль.";
} else if (lastDigit <= 4 && lastDigit > 1 && secondLastDigit != 1) {
currency = " рубля.";
}else {
currency = " рублей.";
}
System.out.println(sumToPay + currency);
}
}