Write a phone book program that uses the following classes: Individal - Base Cla
ID: 3533048 • Letter: W
Question
Write a phone book program that uses the following classes:
Individal - Base Class
Private data members -last name, first name, phone number
Student - Derived from Individual
Private data members - gpa, hours
Staff - Derived from Individual
Private data memers - position (string), year employeed (int)
The program should be menu driven and allow the user to
1. Create a new phone book entry and place it in an array
2. Retrieve a phone book entry and display on screen
3. Retrieve all names and display to screen and write to file
Have a staff array and student array. 10 positions are sufficient. No particular order is requied.
When phone book enties are displayed all data members will be displayed. Create a friend function that overloads the << operator to output phone book entries.
Explanation / Answer
/**
* An address book class that record a persons
* name, address, telephone number, and email address
*/
public class AddressBookEntry
{
private String name;
private String add;
private int tel;
private String email;
/**
* default constructor
*/
public AddressBookEntry(){
name = "";
add = "";
tel = 0;
email = "";
}
/**
* Creates an AddressBookEntry object with the given
* name, address, telephone number and email adress
*/
public AddressBookEntry(String name, String add,
int tel, String email){
this.name = name;
this.add = add;
this.tel = tel;
this.email = email;
}
/**
* returns the variable name
*/
public String getName(){
return name;
}
/**
* changes the variable name
*/
public void changeName(String name){
this.name = name;
}
/**
* returns the variable add
*/
public String getAddress(){
return add;
}
/**
* changes the variable add
*/
public void changeAddress(String add){
this.add = add;
}
/**
* returns the variable tel
*/
public int getTelNumber(){
return tel;
}
/**
* changes the variable tel
*/
public void changeTelNumber(int tel){
this.tel = tel;
}
/**
* returns the variable email
*/
public String getEmailAdd(){
return email;
}
/**
* changes the variable email
*/
public void changeEmailAdd(String email){
this.email = email;
}
}
Java source code for AddressBook class:
import java.io.*;
/**
* Creates an addresbook that contains 100 AddressBookEntries
*/
public class AddressBook
{
//index of the last entry
private int top = 0;
//constant number that indicates the maximum
//number of entries in the address book
private static final int MAXENTRIES = 100;
//array of Address Book Entries
private AddressBookEntry[] list;
/**
* The main method
*/
public static void main(String[] args){
BufferedReader keyIn = new BufferedReader
(new InputStreamReader
(System.in));
AddressBook addBook = new AddressBook();
String act = "";
while(true){
//displays the optons
System.out.println(" [A] Add entry");
System.out.println("[D] Delete entry");
System.out.println("[V] View all entries");
System.out.println("[U] Update entry");
System.out.println("[Q] Quit");
System.out.print("Enter desired action: ");
try{
//gets the choice
act = keyIn.readLine();
}catch(Exception e){
System.out.println("Error");
}
//checks for the appropriate action for
// his choice
if(act.equals("A")||act.equals("a"))
addBook.addEntry();
else if(act.equals("D")||act.equals("d"))
addBook.delEntry();
else if(act.equals("V")||act.equals("v"))
addBook.viewEntries();
else if(act.equals("U")||act.equals("u"))
addBook.updateEntry();
else if(act.equals("Q")||act.equals("q"))
System.exit(0);
else
System.out.println
("Unknown command");
}
}
/**
* creates the AddressBook
*/
public AddressBook(){
list = new AddressBookEntry[MAXENTRIES];
}
/**
* method for adding an AddressBookEntry to the Adressbook
*/
public void addEntry(){
BufferedReader keyIn = new BufferedReader
(new InputStreamReader
(System.in));
String name = "";
String add = "";
int tel = 0;
String email = "";
if(top == MAXENTRIES){
System.out.println("Address Book is full");
return;
}
//asks the user for the data of the address book
try{
System.out.print("Name: ");
name = keyIn.readLine();
System.out.print("Address: ");
add = keyIn.readLine();
System.out.print("Telephone number: ");
tel = Integer.parseInt(keyIn.readLine());
System.out.print("Email Adress: ");
email = keyIn.readLine();
}catch(Exception e){
System.out.println(e);
System.exit(0);
}
AddressBookEntry entry = new AddressBookEntry
(name, add, tel, email);
list[top] = entry;
top++;
}
/**
* method that deletes an AddressBookEntry from the
* Adressbook with the index
*/
public void delEntry(){
BufferedReader keyIn = new BufferedReader
(new InputStreamReader(System.in));
int index = 0;
//checks if the address book is empty
if(top == 0){
System.out.println("Address Book is empty");
return;
}
//asks for the entry which is to be deleted
try{
//shows the current entries on the record book
viewEntries();
System.out.print(" Enter entry number: ");
index = Integer.parseInt(keyIn.readLine())-1;
}catch(Exception e){}
//checks if the index is with in bounds
if(index < 0 || index >= top){
System.out.println("Index Out Of Bounds");
return;
}else{
for( int i=index; i
list[i] = list[i+1];
}
list[top] = null;
top--;
}
}
/**
* method that prints all the entries in the AddressBook
*/
public void viewEntries(){
for(int index = 0; index < top; index++){
System.out.println((index+1)+" Name:"+
list[index].getName());
System.out.println("Address:"+
list[index].getAddress());
System.out.println("Telephone Number:"+
list[index].getTelNumber());
System.out.println("Email Address:"+
list[index].getEmailAdd());
}
}
/**
* method that updates an entry
*/
public void updateEntry(){
BufferedReader keyIn = new BufferedReader(new InputStreamReader
(System.in));
int index = 0;
String name = "";
String add = "";
int tel = 0;
String email = "";
//asks for the entries data
try{
System.out.print("Entry number: ");
index =Integer.parseInt(keyIn.readLine())-1;
System.out.print("Name: ");
name = keyIn.readLine();
System.out.print("Address: ");
add = keyIn.readLine();
System.out.print("Telephone number: ");
tel = Integer.parseInt(keyIn.readLine());
System.out.print("Email Adress: ");
email = keyIn.readLine();
}catch(Exception e){
System.out.println(e);
System.exit(0);
}
//updates the entry
AddressBookEntry entry = new AddressBookEntry
(name, add, tel, email);
list[index] = entry;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.