-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNameSurferDataBase.java
More file actions
71 lines (59 loc) · 1.92 KB
/
Copy pathNameSurferDataBase.java
File metadata and controls
71 lines (59 loc) · 1.92 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
/*
* File: NameSurferDataBase.java
* -----------------------------
* This class keeps track of the complete database of names.
* The constructor reads in the database from a file, and
* the only public method makes it possible to look up a
* name and get back the corresponding NameSurferEntry.
* Names are matched independent of case, so that "Eric"
* and "ERIC" are the same names.
*/
import java.util.*;
import java.io.*;
import acm.util.ErrorException;
public class NameSurferDataBase implements NameSurferConstants {
private HashMap<String, String> nameDataBase = new HashMap<String, String>();
/* Constructor: NameSurferDataBase(filename) */
/**
* Creates a new NameSurferDataBase and initializes it using the
* data in the specified file. The constructor throws an error
* exception if the requested file does not exist or if an error
* occurs as the file is being read.
*/
public NameSurferDataBase(String filename) {
// You fill this in //
try {
BufferedReader br = new BufferedReader(new FileReader(filename));
while(true){
String line = br.readLine();
if (line == null) break;
NameSurferEntry oneName = new NameSurferEntry(line);
String name = oneName.getName().toLowerCase();
if(!nameDataBase.containsKey(name)){
nameDataBase.put(name, line);
}
}
br.close();
} catch (IOException e) {
throw new ErrorException(e);
}
}
/* Method: findEntry(name) */
/**
* Returns the NameSurferEntry associated with this name, if one
* exists. If the name does not appear in the database, this
* method returns null.
*/
public NameSurferEntry findEntry(String name) {
// You need to turn this stub into a real implementation //
name = name.toLowerCase();
if(nameDataBase.containsKey(name)){
String data = nameDataBase.get(name);
NameSurferEntry nameData = new NameSurferEntry(data);
return nameData;
}
else{
return null;
}
}
}