Buy Now To Create PDF Without Trial Watermark!!: Address Book - V. 3.0

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Buy Now to Create PDF without TrialNovember

Lab for CS 150 Watermark!!


10, 2010

Address Book – V. 3.0


Problem:
We want to build an address book that is capable of storing name, address & phone number of a person.
Address book provides functionality in the form of a JOptionPane based menu. The feature list includes:
· Add – to add a new person record
o Add normal persons
o Add Colleagues
o Add Best Friends
· Delete – to delete an existing person record by name
· Search – to search a person record by name
· Exit – to exit from application
· Now, our all data (person’s records) will also be persistent; i.e; stored in a text file.

You have already implemented most of it. The remaining, we try to implement today.

Step 1:
//PersonInfo.java class
import javax.swing.*;
class PersonInfo
{
protected String name;
protected String address;
protected String phoneNum;
protected String status;

public PersonInfo(String n, String a, String p, String s)


{
name =n;
address = a;
phoneNum = p;
status=s;
}
public void print()
{
JOptionPane.showMessageDialog(null, "name: " + name + "\naddress: "
+address+ "\nphone no:" +phoneNum);
}
public void setName(String name)
{
this.name = name;
}
public void setAddress(String address)
{
this.address = address;
}
public void setPhoneNum(String phoneNum)
{
this.phoneNum =phoneNum;
}
public String getName()
1

Created by eDocPrinter PDF Pro!!


Buy Now to Create PDF without TrialNovember
Lab for CS 150 Watermark!!
10, 2010

{
return name;
}
public String getAddress()
{
return address;
}
public String getPhoneNum()
{
return phoneNum;
}

public String getStatus() {


return status;
}

public void setStatus(String status) {


this.status = status;
}
}
Changes in the above class are bold. Reason for doing these changes: __________________________
________________________________________________________________________________

________________________________________________________________________________

Any other changes, we have made, but are not bold, please specify here: ________________________
________________________________________________________________________________

________________________________________________________________________________

Step2:
We do not need to make any changes in the following two classes:

//Colleague.java class
class Colleague extends PersonInfo
{
private String organization;
private String designation;

public Colleague(String n, String a, String p,String o,String d,String s)


{
super(n,a,p,s);
organization=o;
designation=d;

}
public void print()
{
super.print();
JOptionPane.showMessageDialog(null,"Organization: " + organization + "\nDesignation: "
+designation);
}
public String getDesignation() {
return designation;
2

Created by eDocPrinter PDF Pro!!


Buy Now to Create PDF without TrialNovember
Lab for CS 150 Watermark!!
10, 2010

}
public String getOrganization() {
return organization;
}
public void setOrganization(String organization) {
this.organization = organization;
}
public void setDesignation(String designation){
this.designation = designation;
}
}
Any changes in the above class: _________________________________________________________

__________________________________________________________________________________

__________________________________________________________________________________

//Favorites.java class
import javax.swing.*;
class Favorites extends PersonInfo
{
private String dateOfBrith;

public Favorites(String n, String a, String p,String dob,String s)


{
super(n,a,p,s);
dateOfBrith = dob;
}
public void print()
{
super.print();
JOptionPane.showMessageDialog(null,"Date of Birth: " + dateOfBrith);
}
public String getDateOfBirth() {
return dateOfBrith;
}
public void setDateOfBirth(String dateOfBrith) {
this.dateOfBrith = dateOfBrith;
}
}
Any changes in the above class: _________________________________________________________

__________________________________________________________________________________

__________________________________________________________________________________

Step 3:
We also need to make some changes in AddessBook class. Modified class is given below:

//AddressBook.java class
import java.io.*;
import javax.swing.*;
import java.util.*;

Created by eDocPrinter PDF Pro!!


Buy Now to Create PDF without TrialNovember
Lab for CS 150 Watermark!!
10, 2010

class AddressBook {

ArrayList persons;

public AddressBook() {
persons = new ArrayList();
//getting all data from file to arraylist
this.loadPersons();
}

public void addPerson() {


String name = JOptionPane.showInputDialog("Enter name");
String add = JOptionPane.showInputDialog("Enter address");
String pNum = JOptionPane.showInputDialog("Enter phone no");

PersonInfo p = new PersonInfo(name, add, pNum, "normal");


persons.add(p);
}

public void addColleague() {


String name = JOptionPane.showInputDialog("Enter name");
String add = JOptionPane.showInputDialog("Enter address");
String pNum = JOptionPane.showInputDialog("Enter phone no");
String org = JOptionPane.showInputDialog("Enter Organization");
String dest = JOptionPane.showInputDialog("Enter Designation");

Colleague p = new Colleague(name, add, pNum, org, dest, "colleague");


persons.add(p);
}

public void addFavorites() {


String name = JOptionPane.showInputDialog("Enter Name:");
String add = JOptionPane.showInputDialog("Enter Address:");
String pNum = JOptionPane.showInputDialog("Enter phone No:");
String dob = JOptionPane.showInputDialog("Enter Date of Birth:");

Favorites p = new Favorites(name, add, pNum, dob, "favorite");


persons.add(p);
}

public void searchPerson(String n) {


for (int i = 0; i < persons.size(); i++) {
PersonInfo p = (PersonInfo) persons.get(i);
if (n.equals(p.getName())) {
p.print();
}//end if
}//end for
}//end method

public void deletePerson(String n) {


for (int i = 0; i < persons.size(); i++) {
PersonInfo p = (PersonInfo) persons.get(i);
if (n.equals(p.getName())) {
persons.remove(i);
JOptionPane.showMessageDialog(null, n + " delete successfully");
4

Created by eDocPrinter PDF Pro!!


Buy Now to Create PDF without TrialNovember
Lab for CS 150 Watermark!!
10, 2010

}//end if
}//end for
}//end method

//persistance for persons data


public void savePersons() {

try {

PersonInfo p;
String line;

FileWriter fw = new FileWriter("persons.txt");


PrintWriter pw = new PrintWriter(fw);

for (int i = 0; i < persons.size(); i++) {


p = (PersonInfo) persons.get(i);
line = p.getStatus() + "," + p.getName() + "," + p.getAddress() + "," +
p.getPhoneNum();

//if the object is Favorite Person


if (p.status.equals("favorite")) {
Favorites p1 = (Favorites) persons.get(i);
line = line + "," + p1.getDateOfBirth();
} //if the object is collige Person
else if (p.status.equals("colleague")) {
Colleague p1 = (Colleague) persons.get(i);
line = line + "," + p1.getDesignation() + "," + p1.getOrganization();
}
//write the final line formate on file
pw.println(line);
}
pw.flush();
//closing connnections
pw.close();
fw.close();

} catch (IOException ioEx) {


System.out.println(ioEx);
}
}
//loading persons from text file

public void loadPersons() {

String tokens[] = null;


String status, name, add, ph;
PersonInfo p = null;

try {
FileReader fr = new FileReader("persons.txt");
BufferedReader br = new BufferedReader(fr);

//reading first line


String line = br.readLine();
5

Created by eDocPrinter PDF Pro!!


Buy Now to Create PDF without TrialNovember
Lab for CS 150 Watermark!!
10, 2010

while (line != null) {

tokens = line.split(",");

status = tokens[0];
name = tokens[1];
add = tokens[2];
ph = tokens[3];
if (status.equals("normal")) {
p = new PersonInfo(name, add, ph, status);
} else if (status.equals("favorite")) {
String date = tokens[4];
p = new Favorites(name, add, ph, date, status);
} else if (status.equals("colleague")) {
String org = tokens[4];
String desg = tokens[5];
p = new Colleague(name, add, ph, org, desg, status);

}
//add all types of perosns to list
persons.add(p);
//reading next line
line = br.readLine();
}//end while

} catch (IOException ioEx) {


System.out.println(ioEx);
}
}//end loadperson
}
Changes in the above class are bold. Reason for doing these changes: __________________________
________________________________________________________________________________

________________________________________________________________________________

Any other changes, we have made, but are not bold, please specify here: ________________________
________________________________________________________________________________

________________________________________________________________________________

________________________________________________________________________________

Step 4:
//Test.java class
import javax.swing.*;
class Test
{
public static void main (String args[])
{
AddressBook ab = new AddressBook();

String input, inputPerson, s;


int ch,chPerson;
6

Created by eDocPrinter PDF Pro!!


Buy Now to Create PDF without TrialNovember
Lab for CS 150 Watermark!!
10, 2010

while (true)
{
input = JOptionPane.showInputDialog("Enter 1 to Add Person \n Enter 2 to
Search Person \n Enter 3 to Delete Person \n Enter 4 to Exit");
ch = Integer.parseInt(input);
switch (ch)
{
case 1:
inputPerson = JOptionPane.showInputDialog("Enter 1 to Add
Normal Person \n Enter 2 to Favorite Person \n Enter 3 to Add Collegue \n Enter 4 to Back");
chPerson = Integer.parseInt(inputPerson);
switch (chPerson)
{
case 1:
ab.addPerson();
break;
case 2:
ab.addFavorites();
break;
case 3:
ab.addColleague();
break;
case 4:
break;
default:
JOptionPane.showMessageDialog(null,"Enter Option 1 to 4");
}
break;
case 2:
s =JOptionPane.showInputDialog("Enter Name to search");
ab.searchPerson(s);
break;
case 3:
s =JOptionPane.showInputDialog("Enter name to delete");
ab.deletePerson(s);
break;
case 4:
ab.savePersons();
System.exit(0);
default:
JOptionPane.showMessageDialog(null,"Enter Option 1 to 4");
}//end switch
}//end while
}//end main
}//end class
Changes in the above class are bold. Reason for doing these changes: __________________________
________________________________________________________________________________

________________________________________________________________________________

Any other changes, we have made, but are not bold, please specify here: ________________________
________________________________________________________________________________

________________________________________________________________________________

Created by eDocPrinter PDF Pro!!

You might also like