Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
83 lines (77 loc) · 3.11 KB
/
Copy pathMain.java
File metadata and controls
83 lines (77 loc) · 3.11 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
79
80
81
82
83
import java.util.Scanner;
public class Main {
private static final Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
Calculator.inputPersons();
Calculator.addProducts();
Calculator.payPeople();
}
public static class Calculator {
static double totalPrice = 0;
static String names = "";
static int persons = 0;
public static void inputPersons() {
while (true) {
System.out.println("How many persons?");
if (scan.hasNextInt()) {
persons = scan.nextInt();
if (persons <= 1) {
System.out.println("Amount of persons must be integer > 1. Try again.");
continue;
}
break;
} else {
System.out.println("Amount of persons must be integer > 1. Try again.");
scan.nextLine();
continue;
}
}
}
public static void addProducts() {
while (true) {
System.out.println("Product name: ");
String name = scan.next() + scan.nextLine();
System.out.println("Price: ");
if (scan.hasNextDouble()) {
double price = scan.nextDouble();
if (price < 0) {
System.out.println("Price must be positive double! Try again");
continue;
}
totalPrice += price;
names += name + "\n";
} else {
System.out.println("Price must be positive double! Try again");
scan.nextLine();
continue;
}
System.out.println("Product " + name + " successfully added! Enough? (yes - any symbol/no - n): ");
String answer = scan.next();
if (answer.equalsIgnoreCase("n")) {
continue;
} else {break;}
}
}
public static void payPeople() {
int ruble = (int) Math.floor(totalPrice);
int payPeople = (int) Math.floor(totalPrice/persons);
System.out.println("Вы заказали следующие продукты:\n" + names + "на сумму: " + totalPrice + " " + Calculator.payPeoples(ruble));
System.out.println("Каждый из " + persons + " человек заплатит по " + String.format("%.2f", totalPrice/persons) + " " + Calculator.payPeoples(payPeople));
}
static String payPeoples(int ruble) {
if (ruble % 100 / 10 == 1) {
return "рублей";
} else {
switch (ruble % 10) {
case 1 :
return "рубль";
case 2:
case 3:
case 4:
return "рубля";
default: return "рублей";
}
}
}
}
}