-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathRouter.java
More file actions
60 lines (51 loc) · 1.66 KB
/
Router.java
File metadata and controls
60 lines (51 loc) · 1.66 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
package com.dtcc.exams.part2;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Router {
Map<String,String> map=new HashMap<>();
public void add(String path, String controller) {
map.put(path,controller);
}
public Integer size() {
return this.map.size();
}
public String getController(String path) {
String value=null;
for(Map.Entry m:this.map.entrySet()){ //this is how you traverse through the map.(Set)
if(m.getKey().equals(path)){
value= (String) m.getValue();
}
}
return value;
}
public void update(String path, String studentController) {
for(Map.Entry m:this.map.entrySet()) {
if (m.getKey().equals(path)) {
m.setValue(studentController);
}
}
}
public void remove(String path) {
for(Map.Entry m:this.map.entrySet()) {
if (m.getKey().equals(path)) {
map.remove(path);
}
}
}
public String toString()
{
System.out.println("in to string method.");
// Iterator hmIterator=map.entrySet().iterator(); //getting an iterator
// while(hmIterator.hasNext()){
// Map.Entry mapElement=(Map.Entry)hmIterator.next();
// System.out.println(mapElement.getKey() + " -> " + mapElement.getValue());}
String strReturn="";
for(Map.Entry m:this.map.entrySet()) {
String key = (String)m.getKey();
String value=(String)m.getValue();
strReturn+=(key+" -> " + value+"\n");
}
return strReturn;
}
}