-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhoneBookTester.java
More file actions
53 lines (41 loc) · 1.25 KB
/
PhoneBookTester.java
File metadata and controls
53 lines (41 loc) · 1.25 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
class PhoneEntry {
private String name;
private String phone;
public PhoneEntry(String n, String p) {
name = n; phone = p;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
}
class PhoneBook {
private PhoneEntry[] phoneBook;
public PhoneBook() {
phoneBook = new PhoneEntry[5];
phoneBook[0] = new PhoneEntry( "James Barclay", "(418) 665-1223" );
phoneBook[1] = new PhoneEntry( "Grace Dunbar", "(860) 399-3044" );
phoneBook[2] = new PhoneEntry( "Paul Kratides", "(815) 439-9271" );
phoneBook[3] = new PhoneEntry( "Violet Smith", "(312) 223-1937" );
phoneBook[4] = new PhoneEntry( "John Wood", "(913) 883-2874" );
}
public PhoneEntry search(String targetName) {
for (int j = 0 ; j < phoneBook.length ; j++) {
if (phoneBook[j] != null && phoneBook[j].getName().equals(targetName))
return phoneBook[j];
}
return null;
}
}
public class PhoneBookTester {
public static void main(String[] args) {
PhoneBook pb = new PhoneBook();
PhoneEntry entry = pb.search("Violet Smith");
if (entry != null)
System.out.println(entry.getName() + ": " + entry.getPhone());
else
System.out.println("Name not found");
}
}