Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
47 lines (41 loc) · 2.21 KB
/
Copy pathCalculator.java
File metadata and controls
47 lines (41 loc) · 2.21 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
import java.util.Scanner;
public class Calculator {
static String exit = "Завершить"; // Вынес в статическую константу "Завершить"
public static void calc(int people) {
String dish;
String allDishes = "";
float price;
float sumOfOrder = 0.0f;
String close;
while (true) {
Scanner scSecond = new Scanner(System.in); // Оставил один сканнер
System.out.println("Введите название блюда!");
dish = scSecond.nextLine();
System.out.println("Введите стоимость этого блюда в формате 'руб.коп'");
if (scSecond.hasNextFloat()) {
price = scSecond.nextFloat();
if (price > 0) {
sumOfOrder = sumOfOrder + price;
allDishes = allDishes + dish + "\n";
String sufForOne = Finish.finish(price);
String anotherOne = "Вы добавили: %s за %.2f%s\n";
System.out.printf(anotherOne, dish, price, sufForOne);
System.out.println("Хотите добавить блюдо? Введите любую букву или напишите:'Завершить', если блюд больше не осталось");
close = scSecond.next();
if (close.equalsIgnoreCase(exit)) {
break;
}
} else {
System.out.println("Сумма указана неверно");
}
} else {
System.out.println("Сумма указана неверно, попробуйте внести блюдо снова");
}
}
String suf = Finish.finish(sumOfOrder);
float forThePerson = sumOfOrder / people;
String lastSuf = Finish.finish(forThePerson);
System.out.println("Вы купили:\n" + allDishes + "За " + String.format("%.2f", sumOfOrder) + suf);
System.out.println("Каждый должен заплатить по: " + String.format("%.2f", forThePerson) + lastSuf);
}
}