-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay12.java
More file actions
85 lines (71 loc) · 2.53 KB
/
Day12.java
File metadata and controls
85 lines (71 loc) · 2.53 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
74
75
76
77
78
79
80
81
82
83
84
85
// ---------------------------------------------------
// Author : Benjamin Kataliko Viranga
// Community : Stunt Business
// Community website : www.stuntbusiness.com
//
// 30 Days - Q&A Java basic
// Day 12 : Dictionary (key,value) using java Hashmap
// Day 12 | IG : https://www.instagram.com/benjivrik/
// ----------------------------------------------------
// what would be the output of this program ?
import java.util.Scanner;
import java.util.HashMap;
public class Day12
{
/**
* what is HashMap ?
*
* Internet > Object that stores items as key/value pairs
*
*/
public static void main(String[] args)
{
// create the HasMap object
HashMap<String,String> items = new HashMap<String,String>();
// adding items in your HashMap object
items.put("water","10$");
items.put("biscuits","2$");
items.put("chocolate","4$");
items.put("rice","3$");
items.put("wine","5$");
Scanner sc = new Scanner(System.in);
// get user name
System.out.println("\nHey, what is your name?");
String name = sc.next();
// welcoming the user
System.out.println(String.format("\nHello %s, I have the following items in my store", name));
// print all the keys and values
for(String key : items.keySet())
{
System.out.printf("> %s : %s", key, items.get(key));
System.out.printf("\n");
}
String stop = "n";
while(stop.equals("n"))
{
System.out.println("\nWhich item price do you want to check ?");
System.out.println("Enter your item name :");
String user_choice = sc.next();
while(!(items.containsKey(user_choice)))
{
System.out.println(
String.format("I do not have the item %s. Please enter a correct item :",user_choice)
);
user_choice = sc.next();
}
System.out.println(
String.format("Thank you. The price for them item %s is %s",user_choice, items.get(user_choice))
);
// ask the user if he wants to continue
System.out.printf("\nDo you wanna stop ? (y/n) :");
stop = sc.next();
if(!stop.equals("y"))
{
stop = "n";
}
System.out.println("\n");
}
sc.close();
System.out.println("End of program.");
}
}