Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
82 lines (76 loc) · 4.16 KB
/
Copy pathCalculator.java
File metadata and controls
82 lines (76 loc) · 4.16 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
import java.util.Scanner;
public class Calculator {
String nameOfTheDish, dots;
float priceOfTheDish, totalPriceOfDishes;
int priceOfItemInt;
StringBuilder totalListOfItems = new StringBuilder("Добавленные товары:");
Scanner scanner = new Scanner(System.in);
public float enterListOfItems(){
System.out.println("\n==============================================================================================================================================");
System.out.println("Введите названия блюд и их стоимость. Стоимость блюд должна вводиться в формате рубли,копейки. \n\n Для завершения ввода неаберите \"Завершить\"");
System.out.println("==============================================================================================================================================");
while(true){
System.out.println("\nВведите наименование блюда или команду \"Завершить\" для завершения ввода:");
nameOfTheDish = scanner.nextLine();
if (nameOfTheDish.equalsIgnoreCase("завершить")){
break;
}
totalListOfItems.append("\n" + nameOfTheDish);
System.out.println("\nВведите стоимость блюда:");
while(true)
{
if(scanner.hasNextFloat())
{
priceOfTheDish = Float.parseFloat(scanner.nextLine().replace(',','.'));
if(priceOfTheDish>=0){
totalPriceOfDishes += priceOfTheDish;
priceOfItemInt = (int) priceOfTheDish;
dots = ".".repeat(50 - nameOfTheDish.length());
totalListOfItems.append(dots + "." + priceOfItemInt + " руб " + (int) ((priceOfTheDish * 100 - priceOfItemInt * 100)) + " коп");
System.out.println("\nБлюдо успешно добавлено!");
break;
}
else{
System.out.println("\nСтоимость блюда должнабыть больше или равна 0 попробуйте снова!");
}
}
else
{
if(scanner.nextLine().equalsIgnoreCase("завершить")){
System.out.println("\nДля завершения ввода добавьте цену последнего товара и введите \"Завершить\"!");
}
System.out.println("Стоимость блюда должнабыть числом в формате рубли,копейки попробуйте снова!");
}
}
}
System.out.println(totalListOfItems);
return totalPriceOfDishes;
}
public void calculationForEachGuest (int guestAmount ,float totalAmount)
{
float amountForeachGuest;
String templateOfOutPut = "Каждый гость должен заплатить: %.2f";
amountForeachGuest = totalAmount / guestAmount;
if (11<=(int)amountForeachGuest % 100 && (int)amountForeachGuest % 100<=14){
System.out.println(String.format(templateOfOutPut, amountForeachGuest) + " рублей");
}
else{
switch ((int)amountForeachGuest % 10){
default:{
System.out.println(String.format(templateOfOutPut, amountForeachGuest) + " рублей");
break;
}
case (1) : {
System.out.println(String.format(templateOfOutPut, amountForeachGuest) + " рубль");
break;
}
case (2) :
case (3) :
case (4) : {
System.out.println(String.format(templateOfOutPut, amountForeachGuest) + " рубля");
break;
}
}
}
}
}