Repository navigation
Expand file tree
/
Copy pathAssistant.java
More file actions
78 lines (74 loc) · 3.55 KB
/
Copy pathAssistant.java
File metadata and controls
78 lines (74 loc) · 3.55 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
import java.util.Scanner;
public class Assistant {
static Scanner scanner = new Scanner(System.in);
static int numberOfPeople;
// подсчет количество человек
public void calculatePersons() {
System.out.println("На скольких человек необходимо разделить счёт?");
//количество человек
while (true) {
if (scanner.hasNextInt()) {
numberOfPeople = scanner.nextInt();
if (numberOfPeople > 1) {
break;
} else {
System.out.println("Ошибка. Введите корректное количество гостей.");
}
} else {
System.out.println("Ошибка. Введите число.");
scanner.next();
}
}
}
// добавление товара и его стоимости в калькулятор
public void addProduct(Calculator calculator) {
while (true) {
// товар
System.out.println("Введите название товара:");
String product = scanner.next();
// стоимость
System.out.println("Введите стоимость товара:");
double price;
String addOne = "";
if (scanner.hasNextDouble()) {
price = scanner.nextDouble();
if (price > 0) {
calculator.setProducts(product);
calculator.setTotalPrice(price);
System.out.println("Товар успешно добавлен.\nДобавить ещё один товар?\nЕсли все товары добавлены введите - Завершить.");
addOne = scanner.next();
} else {
System.out.println("Ошибка.Введите положительное значение.");
}
if (addOne.equalsIgnoreCase("Завершить")) {
break;
}
} else {
System.out.println("Ошибка. Товар не добавлен. Введите корректное значение стоимости товара.");
scanner.next();
}
}
}
// вывод результатов
public void outputResult(Calculator calculator) {
System.out.print("Добавленные товары:");
// вывод товаров
System.out.println(calculator.getProducts());
// счет каждого посетителя
double scoreVisitor = calculator.getTotalPrice() / Assistant.numberOfPeople;
// склонение в зависимости от целочисленной части счета
String currency = "";
// округление значения
int roundScore = (int) Math.floor(scoreVisitor);
if (roundScore == 1 || (roundScore % 10 == 1 && (roundScore / 10) % 2 == 0)) {
currency = "рубль";
} else if ((roundScore > 1 && roundScore <= 4) || ((roundScore % 10 > 1 && roundScore % 10 <= 4) && (roundScore / 10) % 2 == 0)) {
currency = "рубля";
} else if (roundScore > 4) {
currency = "рублей";
}
// вывод счета клиента
String formatAnswer = String.format("%.2f %s", scoreVisitor, currency);
System.out.println(formatAnswer);
}
}