forked from Yandex-Practicum/Java-Module-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.java
More file actions
67 lines (58 loc) · 2.33 KB
/
List.java
File metadata and controls
67 lines (58 loc) · 2.33 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
import java.util.Scanner;
public class List {
static double totalPrice = 0.00f;
static String listProduct = "";
public static void requestProduct() {
Scanner scanner = new Scanner(System.in);
while(true) {
System.out.println("Введите название товара");
String name = scanner.next();
listProduct = listProduct.concat(name + ";\n");
System.out.println("Введите цену товара");
double price = checkFailPrice();
totalPrice += price;
System.out.println("Товар успешно добавлен!\nЕсли хотите добавить еще товар введите любой символ.\nДля завершения создания списка напините \"Завершить\".");
String result = scanner.next();
if(result.equalsIgnoreCase("Завершить")){
break;
}
}
}
public static String whichRub(double num){
int sum = (int)num;
sum = sum % 100;
if(10<sum && sum<15){
return "рублeй";
}
int sum1 = sum % 10;
switch(sum1){
case 1: return "рубль";
case 2:
case 3:
case 4: return "рубля";
}
return "рублeй";
}
public static void makeList() {
double priceForOne = totalPrice / People.persons;
String end = "Добавленные товары:\n%s%.2f %s должен заплатить каждый человек.";
System.out.println(String.format(end, listProduct, priceForOne,whichRub(priceForOne)));
}
public static double checkFailPrice() {
while(true) {
Scanner scanner = new Scanner(System.in);
boolean hasDouble = scanner.hasNextDouble();
if(hasDouble) {
double trueDouble = scanner.nextDouble();
if (trueDouble > 0) {
return trueDouble;
} else {
System.out.println("Ошибка! Введите корректную цену.");
}
}
else {
System.out.println("Ошибка! Введите корректную цену.");
}
}
}
}