You have been given PhoneRecord .java and PhoneDirectory_Lab .java. Phonedirecto
ID: 3590761 • Letter: Y
Question
You have been given PhoneRecord.java and PhoneDirectory_Lab.java.
Phonedirectory_Lab.java has 2 methods:
displayRecords------- has been given already
addNumber ------you need to write the code for it
You need to complete both the methods. Here is a sample output that we expect to see from your program (of course, if the user’s inputs are different, the results will be different from the sample output). Note: you don't need to handle the case where two or more people have the same phone number. Each method is worth 1 point. Submit only the PhoneDirectory_Lab.java file.
Phone directory commands:
a - Add a new phone number
d - Display All
q – Quit
Enter command (a, d or q): a
Enter first name: nick
Enter last name: keller
Enter phone number: 12345
Enter zip code: 121212
1 record added. Total Records: 1
Enter command (a, d or q): a
Enter first name: sarah
Enter last name: keler
Enter phone number: 12345
Enter zip code: 121212
1 record added. Total Records: 2
Enter command (a, d or q): dl
Command was not recognized; please enter only a, d or q.
Enter command (a, d or q): a
Enter first name: richard
Enter last name: keller
Enter phone number: 1233939
Enter zip code: 121212
1 record added. Total Records: 3
Enter command (a, d or q): d
1. nick keller 12345 121212
2. sarah keler 12345 121212
3. richard keller 1233939 121212
Enter command (a, d or q): a
Enter first name: claire
Enter last name: underwood
Enter phone number: 435
Enter zip code: 20500
1 record added. Total Records: 4
Enter command (a, d or q): d
1. nick keller 12345 121212
2. sarah keler 12345 121212
3. richard keller 1233939 121212
4. claire underwood 435 20500
Explanation / Answer
PhoneRecord.java
public class PhoneRecord {
//Declaring instance variables
private String firstname;
private String lastname;
private String phoneNO;
private int zipcode;
//Parameterized constructor
public PhoneRecord(String firstname, String lastname, String phoneNO,
int zipcode) {
super();
this.firstname = firstname;
this.lastname = lastname;
this.phoneNO = phoneNO;
this.zipcode = zipcode;
}
//getters and setters
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getPhoneNO() {
return phoneNO;
}
public void setPhoneNO(String phoneNO) {
this.phoneNO = phoneNO;
}
public int getZipcode() {
return zipcode;
}
public void setZipcode(int zipcode) {
this.zipcode = zipcode;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return firstname + " " + lastname + " " + phoneNO + " " + zipcode;
}
}
________________
PhoneDirectory_Lab.java
import java.util.ArrayList;
import java.util.Scanner;
public class PhoneDirectory_Lab {
public static void main(String[] args) {
//Declaring variables
String fname, lname, phno;
int zipcode;
char choice;
//Create an ArrayList Which holds PhoneRecord class objects
ArrayList < PhoneRecord > arl = new ArrayList();
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
/* This while loop continues to execute
* until the user enters a valid choice or 'q'
*/
while (true) {
//Dispalying the menu
System.out.println("a - Add a new phone number");
System.out.println("d - Display All");
System.out.println("q – Quit");
//getting the choice entered by the user
System.out.print("Enter command (a, d or q):");
choice = sc.next(".").charAt(0);
//Based on the user choice the corresponding case will executed
switch (choice) {
case 'a':
{
//Getting the input entered by the user
System.out.print("Enter first name: ");
fname = sc.next();
System.out.print("Enter last name:");
lname = sc.next();
System.out.print("Enter phone number:");
phno = sc.next();
System.out.print("Enter zip code: ");
zipcode = sc.nextInt();
//Creating an Phone Record object
PhoneRecord ph = new PhoneRecord(fname, lname, phno, zipcode);
//Adding to the arrayList
arl.add(ph);
System.out.println("1 record added. Total Records: " + arl.size());
continue;
}
case 'd':
{
//Displaying the phoneRecords form the arrayList
for (int i = 0; i < arl.size(); i++) {
System.out.println((i + 1) + "." + arl.get(i).toString());
}
continue;
}
case 'q':
{
break;
}
default:
{
System.out.println("Invalid Choice.");
continue;
}
}
break;
}
}
}
___________________
Output:
a - Add a new phone number
d - Display All
q – Quit
Enter command (a, d or q):a
Enter first name: sarah
Enter last name:keler
Enter phone number:9878675645
Enter zip code: 56789
1 record added. Total Records: 1
a - Add a new phone number
d - Display All
q – Quit
Enter command (a, d or q):a
Enter first name: richard
Enter last name:keller
Enter phone number:9878786767
Enter zip code: 56765
1 record added. Total Records: 2
a - Add a new phone number
d - Display All
q – Quit
Enter command (a, d or q):a
Enter first name: nick
Enter last name:keller
Enter phone number:9889784534
Enter zip code: 56565
1 record added. Total Records: 3
a - Add a new phone number
d - Display All
q – Quit
Enter command (a, d or q):d
1.sarah keler 9878675645 56789
2.richard keller 9878786767 56765
3.nick keller 9889784534 56565
a - Add a new phone number
d - Display All
q – Quit
Enter command (a, d or q):q
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.