-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhoneBook.java
More file actions
48 lines (43 loc) · 1.43 KB
/
PhoneBook.java
File metadata and controls
48 lines (43 loc) · 1.43 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
import java.util.Scanner;
public class PhoneBook {
public class Pbook {
private String name, city, phone;
private int areacode = 0;
Scanner sc;
public Pbook() {
name = new String("Thomas Jefferson");
city = new String("New York");
phone = new String("123-4567");
areacode = 0;
sc = new Scanner(System.in);
}
public void getInput() {
System.out.print("\n\n\nEnter name as first last: ");
name = sc.nextLine();
System.out.print("\nEnter city of residence: ");
city = sc.nextLine();
if (city.equals("Mountain View") || city.equals("Palo Alto") ) {
areacode = 650;
} else if (city.equals("Cupertino") || city.equals("San Jose") ) {
areacode = 408;
}
System.out.print("\nEnter phone number (without area code): ");
phone = sc.nextLine();
}
public void processInputAndPrint() {
int lastname_pos = name.indexOf(" ");
String first = name.substring(0, lastname_pos).trim();
String last = name.substring(lastname_pos, name.length()).trim();
System.out.printf("This phone number will be alphabetized under %s, under %s %s at (%d) %s", last.substring(0, 1), last, first, areacode, phone);
}
}
public static void main(String[] args) {
PhoneBook book = new PhoneBook();
book.run();
}
public void run() {
Pbook book = new Pbook();
book.getInput();
book.processInputAndPrint();
}
}