forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProducts.java
More file actions
73 lines (63 loc) · 2.52 KB
/
Products.java
File metadata and controls
73 lines (63 loc) · 2.52 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
import java.util.Locale;
import java.util.Scanner;
public class Products {
String prodNames = "";
Double prodCost = 0.0;
Formatter formatter;
Products(Formatter formatter) {
this.formatter = formatter;
}
void addItems() {
int count = 0;
Scanner scanner = new Scanner(System.in);
while (true) {
count++;
// Добавляем имя товара
System.out.println("Для добавления нового товара введите название! Для завершения, введите \"Завершить\".");
if (!addName(scanner, count)) {
break;
}
// Добавляем цену товара
System.out.println("Введите цену в формате \"рубли.копейки\".");
addCost(scanner);
System.out.println("Товар добавлен.\n");
}
}
boolean addName(Scanner scanner, int count) { // Добавление имени товара
String prodName = scanner.next();
if (checkName(prodName)) {
String format = "%d. %s"; // Символ переноса строки в методе addCost()
this.prodNames = this.prodNames + String.format(format, count, prodName);
return true;
} else {
return false;
}
}
void addCost(Scanner scanner) { // Добавление стоимости товара
scanner.useLocale(Locale.ENGLISH);
double prodCost;
while (true) {
if (checkFormat(scanner)) {
prodCost = scanner.nextDouble();
if (checkPositive(prodCost)) {
break;
}
} else {
scanner.next();
}
System.out.println("Ошибка. Введите цену в формате \"рубли.копейки\".");
}
this.prodNames = this.prodNames + " - " + this.formatter.rubFullFormat(prodCost) + "\n"; // Вывод суммы товара для наглядности
this.prodCost = this.prodCost + prodCost;
}
boolean checkName(String name) { // Проверка введенного слова
String constEnd = "завершить";
return !constEnd.equalsIgnoreCase(name);
}
boolean checkFormat(Scanner scanner) {
return scanner.hasNextDouble();
}
boolean checkPositive(double prodCost) {
return prodCost > 0;
}
}