Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
62 lines (50 loc) · 2.46 KB
/
Copy pathCalculator.java
File metadata and controls
62 lines (50 loc) · 2.46 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
import java.util.ArrayList;
import java.util.Scanner;
public class Calculator {
public Calculator(int totalPersons) {
this.totalPersons = totalPersons;
}
static ArrayList<Product> products = new ArrayList<>();
static int totalPersons;
static double calculate(){
double sum = 0;
for (Product product:products) {sum += product.price;}
return sum;
}
static void addProducts(){
Scanner scanner = new Scanner(System.in);
System.out.println("Здравствуйте!");
System.out.println("Сделайте заказ (для завершения введите 'завершить'): ");
String productName;
double productPrice;
double totalPrice = 0;
Declension roubles;
do {
System.out.print("Наименование товара: ");
productName = scanner.nextLine();
if (!productName.equalsIgnoreCase("завершить")) {
System.out.print("Цена товара: ");
while (!scanner.hasNextDouble()) {
System.out.println("Некорректное значение. Повторите ввод");
scanner.next();
}
productPrice = scanner.nextDouble();
scanner.nextLine();
if (productPrice > 0) {
Product product = new Product(productName, productPrice);
roubles = new Declension(product.price);
System.out.println("Товар '" + product.name + "' добавлен в счет, стоимость: " + String.format("%.2f", product.price)+roubles.getDeclension());
products.add(product);
totalPrice = calculate();
roubles = new Declension(totalPrice);
System.out.println("Добавлено товаров на сумму "+String.format("%.2f", totalPrice) + roubles.getDeclension());
} else {
System.out.println("Введите цену товара больше нуля");
}
}
} while (!productName.equalsIgnoreCase("завершить"));
double result = totalPrice/totalPersons;
roubles = new Declension(result);
System.out.println("Каждый из вас должен заплатить по "+String.format("%.2f", result) +roubles.getDeclension());
}
}