Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
40 lines (36 loc) · 2.06 KB
/
Copy pathCalculator.java
File metadata and controls
40 lines (36 loc) · 2.06 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
import java.util.Scanner;
public class Calculator {
public void product(int numberOfPersons) { // Метод для добавления товара и цены
double sum = 0;
double productPrice;
String productName;
StringBuilder productsList = new StringBuilder("Список товаров :");
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Чтобы завершить добавление товаров на пишите Завершить\nВведите название товара :");
productName = scanner.next();
if (productName.equalsIgnoreCase("Завершить")) { // Для того, что бы завершить добавление товаров необходимо написать "Завершить"
break;
}
System.out.println("Введите стоимость товара :");
if (!scanner.hasNextDouble()) {
System.out.println("Необходимо ввести стоимость товара");
scanner.next();
} else {
productPrice = scanner.nextDouble();
if (productPrice < 0) {
System.out.println("Введите положительное число");
} else {
productsList.append("\n").append(productName).append(" стоимостью ").append(productPrice).append("р");
System.out.println(productsList);
sum += productPrice;
System.out.println("Товар успешно добавлен");
}
}
}
System.out.println("Общая сумма товаров = " + sum);
sum /= numberOfPersons;
Formatter formatter = new Formatter();
formatter.reform(sum); // Вызываем метод, который склоняет падеж
}
}