Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
37 lines (35 loc) · 2.07 KB
/
Copy pathCalculator.java
File metadata and controls
37 lines (35 loc) · 2.07 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
import java.util.Scanner;
//отдельный класс, содержащий логику добавления и подсчёта товаров.
public class Calculator {
static Scanner scanner = new Scanner(System.in);
static String allItems = "";
static double amountItemsPrice = 0;
static double itemPrice = 0;
static String itemPriceString = "";
static void calculate() {
while (true) {
String regex = "(\\d+)?\\.(\\d+)?";
System.out.println("Введите название совместно купленного товара: ");
String itemName = scanner.next();
allItems += itemName + "\n";
while (true) {
System.out.println("Введите цену товара <" + itemName + "> в формате <рубли.копейки>:");
itemPriceString = scanner.next();
if (itemPriceString.matches(regex)) {
itemPrice = Double.parseDouble(itemPriceString);
amountItemsPrice += itemPrice;
break;
} else {
System.out.println("Некорректно введена цена товара <" + itemName + "> Попробуйте ещё раз.");
itemPrice = 0;
}
}
System.out.println("Товар <" + itemName + "> по цене " + Formatter.Formatter(itemPrice) + " добавлен. Общая стоимость всех товаров " + Formatter.Formatter(amountItemsPrice) + ".\nДобавить ещё товар? <любой символ или слово> - добавить ещё товар или <завершить> для окончания ввода: ");
String userAnswer = scanner.next();
if (userAnswer.equalsIgnoreCase("завершить")) {
System.out.println("Ввод товаров завершен. Вы добавили товары на сумму " + Formatter.Formatter(amountItemsPrice));
break;
}
}
}
}