forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoods.java
More file actions
56 lines (47 loc) · 2.28 KB
/
Goods.java
File metadata and controls
56 lines (47 loc) · 2.28 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
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Goods {
//Функция второй части
//Что было заказано и по какой стоимости, с точностью до копеек.
static Map goodsInput(){
boolean checkCost = true;
boolean checkEnter = true;
float cost = 0;
String product = "";
Map<String,Float> receipt = new HashMap<String,Float>();
System.out.println("Введите заказанное блюдо-товар.");
while (checkEnter){
Scanner in = new Scanner(System.in);
//Товар
product = in.nextLine(); // Товар может быть с пробелом в имени
if(product.toLowerCase().equals("завершить")){
checkEnter = false;
break;
}
//Стоимость
System.out.println("Введите стоимость до копеек(,00). В качестве разделителя нужно использовать \",\"");
while (checkCost) {
if(!(in.hasNextFloat())){
System.out.println("Вы ввели " + in.next() + " это не похоже на стоимость.\n Пожалуйста повторите ввод.");
}
else {
cost = Math.round(in.nextFloat()*100)/100.0f;
if(cost > 0){
checkCost = false;
}
else{
System.out.println("Вы ввели " + cost + ", это неправильная стоимость.\n Пожалуйста повторите ввод.");
}
}
}
//Добавим в массив
receipt.put(product, cost);
Main.totalCost += cost;
System.out.println("Вы добавили в чек " + product + " по стоимости " + cost);
System.out.println("Если вы хотите завершить ввод - наберите \"Завершить\" или введите новый товар.");
checkCost = true;
}
return receipt;
}
}