Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
78 lines (72 loc) · 3.2 KB
/
Copy pathMain.java
File metadata and controls
78 lines (72 loc) · 3.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
67
68
69
70
71
72
73
74
75
76
77
78
import java.util.Scanner;
import java.lang.Math;
public class Main {
public static void inputProducts(Calculator calculator) {
while (true) {
String productName = "";
System.out.println("Введите наименование товара или команду <Завершить>: ");
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextLine()) {
productName = scanner.nextLine();
if(productName.equalsIgnoreCase("завершить")){
break;
}
while (true) {
Scanner input = new Scanner(System.in);
System.out.println("Введите цену товара: ");
if (input.hasNextDouble()) {
double price = input.nextDouble();
if (price > 0){
calculator.addProduct(new Product(productName, price));
break;
}
}
System.out.println("Вы ввели некорректную цену.");
}
} else {
System.out.println("Вы ввели некорректное наименование.");
continue;
}
}
}
public static void printResult(Calculator calculator, int quantityOfPeople){
if (calculator.getProducts().equals("")){return;}
System.out.println("\n\nДобавленные товары:");
System.out.println(calculator.getProducts());
String sumWithCurrency = returnSumWithCurrency(calculator.getResult(quantityOfPeople));
System.out.println("Каждый участник должен заплатить - " + sumWithCurrency);
}
public static String returnSumWithCurrency(double num){
int number = (int)Math.floor(num);
int preLastDigit = number % 100 / 10;
if (preLastDigit == 1){
return String.format("%.2f", num) + " рублей";
}
switch (number % 10){
case 1:
return String.format("%.2f", num) + " рубль";
case 2:
case 3:
case 4:
return String.format("%.2f", num) + " рубля";
default:
return String.format("%.2f", num) + " рублей";
}
}
public static void main(String[] args) {
while (true){
System.out.println("Введите количество человек, на которых необходимо разделить счет:");
Scanner scanner = new Scanner(System.in);
if(scanner.hasNextInt()){
int quantityOfPeople = scanner.nextInt();
if(quantityOfPeople > 1){
Calculator сalculator = new Calculator();
inputProducts(сalculator);
printResult(сalculator, quantityOfPeople);
break;
}
}
System.out.println("Некорректный ввод.Необходимо ввести целое число больше 1.");
}
}
}