Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
66 lines (57 loc) · 2.2 KB
/
Copy pathCalculator.java
File metadata and controls
66 lines (57 loc) · 2.2 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
import java.util.ArrayList;
import java.util.Scanner;
public class Calculator {
static Scanner scanner = new Scanner(System.in);
static double sum;
static ArrayList<String> allProducts = new ArrayList<>();
public static void addProduct() {
while (true) {
System.out.println("Введите название товара, или \"завершить\" для выхода.");
String productName = scanner.next();
if (productName.equalsIgnoreCase("Завершить")) {
break;
}
System.out.println("Введите цену товара.");
while (true) {
try {
double productPrice = new Scanner(System.in).nextDouble();
if (productPrice <= 0) {
throw new RuntimeException();
}
Product product = new Product(productName, productPrice);
sum += product.price;
allProducts.add(product.name);
break;
} catch (RuntimeException e) {
System.out.println("Введите корректное значение цены числом больше 0");
}
}
}
}
public static void showAllProducts() {
if (allProducts.isEmpty()) {
System.out.println("Нет добавленных продуктов");
}
System.out.println("Добавленные товары:");
for (int i = 0; i < allProducts.size(); i++) {
System.out.println(allProducts.get(i));
}
}
public static double returnSum() {
return sum;
}
public static String declineRuble(double amount) {
long rubles = (long) amount;
long lastDigit = rubles % 10;
long lastTwoDigits = rubles % 100;
if (lastTwoDigits >= 11 && lastTwoDigits <= 14) {
return "рублей";
} else if (lastDigit == 1) {
return "рубль";
} else if (lastDigit >= 2 && lastDigit <= 4) {
return "рубля";
} else {
return "рублей";
}
}
}