Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
75 lines (65 loc) · 2.64 KB
/
Copy pathCalculator.java
File metadata and controls
75 lines (65 loc) · 2.64 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
import java.util.Locale;
import java.util.Scanner;
public class Calculator {
String listOfProducts;
double productPrice;
public Calculator() {
listOfProducts = "";
productPrice = 0.0;
}
public void addProduct() { //метод добавления товаров
while (true) {
Scanner in = new Scanner(System.in).useLocale(Locale.US);
System.out.println("Введите название товара: ");
String productName = in.nextLine();
if (productName.equalsIgnoreCase("Завершить")) {
break;
}
listOfProducts = productName + "\n";
System.out.println("Введите стоимость в формате: 'рубли.копейки' [10.45, 11.40]");
double price = inputPrice();
productPrice += price;
System.out.println("Товар успешно добавлен! Хотите добавить ещё один товар? Если да, то продолжайте писать ваш список. Если нет, то напишите \"Завершить\"");
}
}
public double inputPrice() {
while (true) {
Scanner in = new Scanner(System.in).useLocale(Locale.US);
if (in.hasNextDouble()) {
double price = in.nextDouble();
if (price >= 0) {
return price;
} else {
System.out.println("Стоимость не может быть отрицательная");
}
} else {
System.out.println("Некорректный ввод");
}
}
}
public static String getRubleAddition(double price)
{
int num = (int)Math.floor(price);
int preLastDigit = num % 100 / 10;
if (preLastDigit == 1)
{
return "рублей";
}
switch (num % 10)
{
case 1:
return "рубль";
case 2:
case 3:
case 4:
return "рубля";
default:
return "рублей";
}
}
public void printInfo(int persona) {
System.out.println("Добавленные товары: \n" + listOfProducts);
System.out.printf("Стоимость товаров для каждого по отдельности: %.2f %s \n", productPrice / persona, getRubleAddition(productPrice / persona));
System.out.printf("Общая стоимость товаров: %.2f %s \n" , productPrice, getRubleAddition(productPrice));
}
}