-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNameSurferDataBase.java
More file actions
executable file
·65 lines (55 loc) · 1.86 KB
/
NameSurferDataBase.java
File metadata and controls
executable file
·65 lines (55 loc) · 1.86 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
/*
* Name: Rachel Sun
* 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 acm.util.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class NameSurferDataBase implements NameSurferConstants {
// The database is actually a HashMap -
// It reads the data from the file and stores them into the HashMap.
HashMap<String, NameSurferEntry> dataBase = new HashMap<String, NameSurferEntry>();
/*
* The constructor 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) {
// Read file
try {
BufferedReader br = new BufferedReader(new FileReader(filename));
while (true) {
String line = br.readLine();
if (line == null)
break;
// For each line read, generate a new entry
// and put it in the hashmap.
NameSurferEntry newEntry = new NameSurferEntry(line);
dataBase.put(newEntry.getName().toLowerCase(), newEntry);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* 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) {
if (dataBase.containsKey(name.toLowerCase())) {
return dataBase.get(name.toLowerCase());
}
return null;
}
}