Question: JAVA PROGRAM - GUI - error checking needed to be added to the program The program is complete however please add error checking for when
JAVA PROGRAM - GUI - error checking needed to be added to the program
The program is complete however please add error checking for when you are adding a person or editing a Person. Please add so that in the joption pane dialog you can hit CANCEL or the X button and it will return you to the main program .
Would not let me upload a file so here are the classes. Thank you!
////// START ADDRESS BOOK CLASS ////////
package addressBook;
import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.FileInputStream; import java.util.Scanner; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame;
public class AddressBook { private static ArrayList
people.add(Carlos); people.add(Jimmy); people.add(John); people.add(Dora); people.add(Luis); people.add(Ken); people.add(Bill); people.add(Troy); people.add(Dane); people.add(Kelly); //people.remove(6); } final JTableSorting frame = new JTableSorting(people); frame.show(); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent event) { SerializableWrite(frame.getList()); frame.dispose(); System.exit(0); } }); SerializableWrite(frame.getList()); }//end of main
public static void SerializableWrite(List
////////// START OF PERSON CLASS /////////////
package addressBook;
import java.io.Serializable;
import java.util.ArrayList;
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private int index;
private String firstName;
private String lastName;
private String middleName = "";
private String note= "";
private String group= "";
private ArrayList
private ArrayList
addr; // array list that holds address objectsprivate ArrayList
public Person(String firstName, String middleName, String lastName, String note,String group,telephone telephoneNum, address address,email emails) {
this.firstName = firstName;
this.lastName = lastName;
this.middleName = middleName;
this.note = note;
this.group=group;
this.emails=new ArrayList
this.emails.add(emails);
this.addr = new ArrayList
();this.addr.add(address);
this.telephoneNums = new ArrayList
this.telephoneNums.add(telephoneNum);
}
public Person(String firstName, String lastName,address address,telephone telephoneNum) {
this.firstName = firstName;
this.lastName = lastName;
this.addr = new ArrayList
();this.addr.add(address);
this.telephoneNums = new ArrayList
this.telephoneNums.add(telephoneNum);
}// constructor First and last name, address. note, telNum,
public Person(String firstName, String lastName,ArrayList
address,telephone telephoneNum, ArrayList
this.firstName = firstName;
this.lastName = lastName;
this.addr = address;
this.telephoneNums = new ArrayList
this.telephoneNums.add(telephoneNum);
this.emails = emails;
}// constructor First and last name, address. note, telNum,
public Person(String firstName,String middleName,String lastName,String note,String group, ArrayList
address,ArrayList
this.firstName = firstName;
this.lastName = lastName;
this.note = note;
this.group = group;
this.middleName= middleName;
this.addr = address;
this.telephoneNums = telephone;
this.emails = emails;
}// constructor First and last name, address. note, telNum,
public Person(String firstName, String lastName,String middleName,String note,String group) {
this.firstName = firstName;
this.lastName = lastName;
this.middleName = middleName;
this.note = note;
this.group=group;
}// constructor First and last name
public Person(String firstName,String middleName, String lastName,telephone telephoneNum, address address,String group ,
String note, email emails) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
this.group = group;
this.note = note;
this.emails=new ArrayList
this.emails.add(emails);
this.addr = new ArrayList
();this.addr.add(address);
this.telephoneNums = new ArrayList
this.telephoneNums.add(telephoneNum);
}// constructor
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public ArrayList
return telephoneNums;
}
public void setTelephoneNums(ArrayList
this.telephoneNums = telephoneNums;
}
public ArrayList
return emails;
}
public void setemails(ArrayList
this.emails = emails;
}
public ArrayList
getAddress() {return addr;
}
public void setAddress(ArrayList
address) {this.addr = address;
}
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 getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
@Override
public String toString() {
String result = " Person: " + firstName +" "+ middleName + " " + lastName ;
for(address addr : addr)
{
result += addr;
}
for(telephone tel : telephoneNums)
{
result += tel;
}
for(email email : emails)
{
result += email;
}
result += " Group:"+group+" Note:"+ note+" ";
return result;
}
}// END OF PERSON CLASS
/// START OF ADDRESS CLASS ///////
package addressBook;
import java.io.Serializable;
public class address implements Serializable {
private static final long serialVersionUID = 1L;
private String streetName;
private String city;
private String state;
private String county;
private String postalCode;
private String addressType;
public address(String streetName, String city, String state, String county, String postalCode,String addressType) {
super();
this.addressType=addressType;
this.streetName = streetName;
this.city = city;
this.state = state;
this.county = county;
this.postalCode = postalCode;
}// Constructor for address
public String getStreetName() {
return streetName;
}
public void setStreetName(String streetName) {
this.streetName = streetName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCounty() {
return county;
}
public void setCounty(String county) {
this.county = county;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getAddressType() {
return addressType;
}
public void setAddressType(String addressType) {
this.addressType = addressType;
}
@Override
public String toString() {
return " "+addressType + " Address: "+ streetName +","+ city +" ,"+ state +"," + postalCode +", "+ county +" County";
}
}/// END OF ADDRESS CLASS
/////// START OF EMAIL CLASS /////////
package addressBook;
import java.io.Serializable;
public class email implements Serializable {
private static final long serialVersionUID = 1L;
private String emailType;
private String emails;
public email(String emailType, String emails) {
super();
this.emailType = emailType;
this.emails = emails;
}
public String getEmailType() {
return emailType;
}
public void setEmailType(String emailType) {
this.emailType = emailType;
}
public String getEmail() {
return emails;
}
public void setEmail(String emails) {
this.emails = emails;
}
@Override
public String toString() {
return " "+emailType+" Email: " + emails +"";
}
}/// END OF EMAIL CLASS
/////// START OF TELEPHONE CLASS //////////
package addressBook;
import java.io.Serializable;
public class telephone implements Serializable {
private static final long serialVersionUID = 1L;
private String telephoneNums;
private String telephoneType;
public telephone(String telephoneNums,String telephoneType) {
super();
this.telephoneNums = telephoneNums;
this.telephoneType = telephoneType;
}
public String getTelephoneNums() {
return telephoneNums;
}
public void setTelephoneNums(String telephoneNums) {
this.telephoneNums = telephoneNums;
}
public String getTelephoneType() {
return telephoneType;
}
public void setTelephoneType(String telephoneType) {
this.telephoneType = telephoneType;
}
@Override
public String toString() {
return " "+telephoneType+" Number " + telephoneNums + "";
}
}// END OF TELEPHONE CLASS
///// START OF JTABLESORTING CLASS ////////
package addressBook;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
public class JTableSorting extends JFrame {
private static final long serialVersionUID = 1L;
private JTable table;
private personTableModel model;
public JTableSorting(final ArrayList
super("Address Book ");
model = new personTableModel(people);
table = new JTable(model);
////////////////////////////////////////////////////////////////
table.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent event){
if (event.getClickCount()==1){
JTable target = (JTable)event.getSource();
int row = target.getSelectedRow();
row = table.convertRowIndexToModel(row);
int column = target.getSelectedColumn();
int result = JOptionPane.showOptionDialog(null,people.get(row), "Information", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] { "OK", "Edit", "Delete"}, null);
if (result == JOptionPane.CANCEL_OPTION) {
System.out.println("Delete");
model.deletePerson(row);
} else if (result == JOptionPane.NO_OPTION) {
System.out.println("Edit");
editPerson(row);
}
}
}
});
////////////////////// Add Contact button creation and action //////////////////////////////////////////
table.setAutoCreateRowSorter(true);
add(new JScrollPane(table),BorderLayout.CENTER);
JButton button = new JButton("Add Contact");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String firstName;
String lastName;
String middleName = "";
String note= "";
String group= "";
// Email Information //
String emailType;
String email;
///////////////////////////
////Telephone Number info ////
String telephoneNums;
String telephoneType;
///////////////////////////
////Address Information ////
String streetName;
String city;
String state;
String county;
String postalCode;
String addressType;
///////////////////////////
firstName = JOptionPane.showInputDialog(null, "What is the First name?");
middleName= JOptionPane.showInputDialog(null, "What is the Middle name?");
lastName = JOptionPane.showInputDialog(null, "What is the Last name?");
note = JOptionPane.showInputDialog(null, "What note would you like to write for this Person?");
group = JOptionPane.showInputDialog(null, "What group is this person calssified in?");
char conti = 0;
ArrayList
do {
email = JOptionPane.showInputDialog(null, "What is this persons email ?");
emailType = JOptionPane.showInputDialog(null, "Is this a Personal or Buisness email?");
email lastEmail = new email(email,emailType);
emails.add(lastEmail);
String vall = JOptionPane.showInputDialog(null, "Would you like to enter another email? (Yes, No");
if(vall!=null && !vall.isEmpty()){
conti = vall.charAt(0);
conti = Character.toUpperCase(conti);
}
} while (conti == 'Y');
char cont = 0;
ArrayList
addresses = new ArrayList();do {
addressType = JOptionPane.showInputDialog(null, "What Type of address is this going to be? (Home, Buisness");
streetName = JOptionPane.showInputDialog(null, "What is the name of the Street");
city = JOptionPane.showInputDialog(null, "What is the name of the city ? ");
state = JOptionPane.showInputDialog(null, "What is the name of the state?");
county = JOptionPane.showInputDialog(null, "What is the name of the county ?");
postalCode = JOptionPane.showInputDialog(null, "what is the postal code ?");
address lastAddress = new address(streetName,city,state,county,postalCode,addressType);// Creating a new address object and filling it with the info we gathered
addresses.add(lastAddress); // Adding the new address to the array list
String vall = JOptionPane.showInputDialog(null, "Would you like to enter another Address? (Yes , No");
if(vall!=null && !vall.isEmpty()){
cont = vall.charAt(0);
cont = Character.toUpperCase(cont);
}
/*cont = JOptionPane.showInputDialog(null, "Would you like to enter another Address? (Yes , No").charAt(0);
cont = Character.toUpperCase(cont);*/
} while (cont == 'Y');
char contr = 0;
ArrayList
do {
telephoneNums = JOptionPane.showInputDialog(null, "What is this persons Telephone Number?");
telephoneType = JOptionPane.showInputDialog(null, "What type of number is this? (Home, Work, Cell)");
telephone lastTelephone = new telephone (telephoneNums,telephoneType);
telephones.add(lastTelephone);
String vall = JOptionPane.showInputDialog(null, "Would you like to enter another Telephone number? (Yes , No");
if(vall!=null && !vall.isEmpty()){
contr = vall.charAt(0);
contr = Character.toUpperCase(contr);
}
/*contr = JOptionPane.showInputDialog(null, "Would you like to enter another Telephone number? (Yes , No").charAt(0);
contr = Character.toUpperCase(contr);*/
} while (contr == 'Y');
if(!firstName.isEmpty() && !lastName.isEmpty() && !city.isEmpty() && !group.isEmpty()){
Person lastPerson = new Person (firstName,middleName,lastName,note,group,addresses,telephones, emails);
model.addPerson(lastPerson);
}else{
JOptionPane.showMessageDialog(null, "Details are empty. Unable to add contact! Please try again");
}
}
});
add(button, BorderLayout.SOUTH);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private void editPerson(int row) {
String firstName;
String lastName;
String middleName = "";
String note= "";
String group= "";
// Email Information //
String emailType;
String email;
///////////////////////////
////Telephone Number Info ////
String telephoneNums;
String telephoneType;
///////////////////////////
////Address Information ////
String streetName;
String city;
String state;
String county;
String postalCode;
String addressType;
///////////////////////////
Person oldPerson = model.getList().get(row);
firstName = JOptionPane.showInputDialog(null, "What is the First name?", oldPerson.getFirstName());
middleName= JOptionPane.showInputDialog(null, "What is the Middle name?", oldPerson.getMiddleName());
lastName = JOptionPane.showInputDialog(null, "What is the Last name?", oldPerson.getLastName());
note = JOptionPane.showInputDialog(null, "What note would you like to write for this Person?", oldPerson.getNote());
group = JOptionPane.showInputDialog(null, "What group is this person calssified in?", oldPerson.getGroup());
// Edit all emails
ArrayList
ArrayList
for(email Email : allEmails){
email = JOptionPane.showInputDialog(null, "What is this persons email?", Email.getEmail());
emailType = JOptionPane.showInputDialog(null,"Is this a Personal or Buisness Email?", Email.getEmailType());
email lastEmail = new email(email, emailType);
emails.add(lastEmail);
}
char conti = 0;
String vall = JOptionPane.showInputDialog(null, "Would you like to enter another email? (yes,No)");
if(vall!=null && !vall.isEmpty()){
conti = vall.charAt(0);
conti = Character.toUpperCase(conti);
}
//conti = .charAt(0);
//conti = Character.toUpperCase(conti);
while (conti == 'Y') {
email = JOptionPane.showInputDialog(null, "What is this persons email?");
emailType = JOptionPane.showInputDialog(null,"Is this a Personal or Buisness Email?");
email lastEmail = new email(email, emailType);
emails.add(lastEmail);
vall = JOptionPane.showInputDialog(null, "Would you like to enter another email? (yes,No)");
if(vall!=null && !vall.isEmpty()){
conti = vall.charAt(0);
conti = Character.toUpperCase(conti);
}
/*conti = JOptionPane.showInputDialog(null, "Would you like to enter another email? (yes, No)").charAt(0);
conti = Character.toUpperCase(conti);*/
}
//Edit all addresses
ArrayList
allAddress = oldPerson.getAddress();ArrayList
addresses = new ArrayList();for(address Address : allAddress){
addressType = JOptionPane.showInputDialog(null, "What Type of address is this going to be? (Home, Buisness)", Address.getAddressType());
streetName = JOptionPane.showInputDialog(null, "What is the name of the Street", Address.getStreetName());
city = JOptionPane.showInputDialog(null, "What is the name of the city ? ", Address.getCity());
state = JOptionPane.showInputDialog(null, "What is the name of the state?", Address.getState());
county = JOptionPane.showInputDialog(null, "What is the name of the county ?", Address.getCounty());
postalCode = JOptionPane.showInputDialog(null, "what is the postal code ?", Address.getPostalCode());
address lastAddress = new address(streetName,city,state,county,postalCode,addressType);
addresses.add(lastAddress);
}
char cont = 0;
vall = JOptionPane.showInputDialog(null, "Would you like to enter another Address? (Yes , No");
if(vall!=null && !vall.isEmpty()){
cont = vall.charAt(0);
cont = Character.toUpperCase(cont);
}
/*cont = JOptionPane.showInputDialog(null, "Would you like to enter another Address? (Yes , No").charAt(0);
cont = Character.toUpperCase(cont);
*/
while (cont == 'Y') {
addressType = JOptionPane.showInputDialog(null, "What Type of address is this going to be? (Home, Buisness)");
streetName = JOptionPane.showInputDialog(null, "What is the name of the Street");
city = JOptionPane.showInputDialog(null, "What is the name of the city ? ");
state = JOptionPane.showInputDialog(null, "What is the name of the state?");
county = JOptionPane.showInputDialog(null, "What is the name of the county ?");
postalCode = JOptionPane.showInputDialog(null, "what is the postal code ?");
address lastAddress = new address(streetName,city,state,county,postalCode,addressType);
addresses.add(lastAddress);
vall = JOptionPane.showInputDialog(null, "Would you like to enter another Address? (Yes , No");
if(vall!=null && !vall.isEmpty()){
cont = vall.charAt(0);
cont = Character.toUpperCase(cont);
}
/*cont = JOptionPane.showInputDialog(null, "Would you like to enter another Address? (Yes , No").charAt(0);
cont = Character.toUpperCase(cont);*/
}
// Edit telephone numbers
ArrayList
ArrayList
for(telephone Telephone : allTelephoneNums){
telephoneNums = JOptionPane.showInputDialog(null, "What is this persons Tel Number?", Telephone.getTelephoneNums());
telephoneType = JOptionPane.showInputDialog(null, "What type of number is this? (Home, Work, Cell", Telephone.getTelephoneType());
telephone lastTele = new telephone(telephoneNums, telephoneType);
telephones.add(lastTele);
}
char contr = 0;
vall = JOptionPane.showInputDialog(null, "Would you like to enter another Telephone number? (Yes , No");
if(vall!=null && !vall.isEmpty()){
contr = vall.charAt(0);
contr = Character.toUpperCase(contr);
}
/*contr = JOptionPane.showInputDialog(null, "Would you like to enter another Telephone number? (Yes , No").charAt(0);
contr = Character.toUpperCase(contr);*/
while (contr == 'Y') {
telephoneNums = JOptionPane.showInputDialog(null, "What is this persons Tel Number?");
telephoneType = JOptionPane.showInputDialog(null, "What type of number is this? (Home, Work, Cell");
telephone lastTele = new telephone(telephoneNums, telephoneType);
telephones.add(lastTele);
vall = JOptionPane.showInputDialog(null, "Would you like to enter another Telephone number? (Yes , No");
if(vall!=null && !vall.isEmpty()){
contr = vall.charAt(0);
contr = Character.toUpperCase(contr);
}
/*contr = JOptionPane.showInputDialog(null, "Would you like to enter another Telephone number? (Yes , No").charAt(0);
contr = Character.toUpperCase(contr);
*/
}
if(!firstName.isEmpty() && !lastName.isEmpty() && !group.isEmpty()){
Person lastPerson = new Person (firstName,middleName,lastName,note,group,addresses,telephones, emails);
model.editPerson(lastPerson, row);
}else{
JOptionPane.showMessageDialog(null, "Details are empty. Unable to edit contact! Please try again");
}
}
public List
return model.getList();
}
}///////// END OF JTABLE SORTING CLASS /////////////////////
///////////// START OF PERSON TABLE MODEL CLASS ///////////////
package addressBook;
import javax.swing.table.*; import java.util.*;
public class personTableModel extends AbstractTableModel { private static final long serialVersionUID = 1L; private static final int COLUMN_NO = 0; private static final int COLUMN_FNAME = 1; private static final int COLUMN_LNAME = 2; private static final int COLUMN_CITY = 3; private static final int COLUMN_GROUP = 4; private String[] columnNames = {"NO.", "First Name", "Last Name", "City", "Group"}; private List
@Override public int getRowCount() { // TODO Auto-generated method stub return listPeople.size(); } @Override public Object getValueAt(int rowIndex, int columnIndex) { Person Person = listPeople.get(rowIndex); Object retVal = null ; switch (columnIndex){ case COLUMN_NO: retVal = Person.getIndex(); break; case COLUMN_FNAME: retVal = Person.getFirstName(); break; case COLUMN_LNAME: retVal = Person.getLastName(); break; case COLUMN_CITY : retVal = Person.getAddress().get(0).getCity(); break; case COLUMN_GROUP : retVal = Person.getGroup(); break; default: throw new IllegalArgumentException("Invalid Column index"); }// end of switch return retVal; }// end of Get value at @Override public String getColumnName(int columnIndex){ return columnNames[columnIndex]; } @Override public Class> getColumnClass(int columnIndex){ if(listPeople.isEmpty())return Object.class; else return getValueAt(0,columnIndex).getClass(); } @Override public void setValueAt(Object value, int rowIndex, int columnIndex){ Person Person = listPeople.get(rowIndex); if(columnIndex == COLUMN_NO){ Person.setIndex((int)value); } } public String[] getColumnNames(){ return columnNames; } public void addPerson(Person person){
person.setIndex(listPeople.size()+1); listPeople.add(person); this.fireTableDataChanged(); } public void deletePerson(int row){
listPeople.remove(row); this.fireTableDataChanged(); }
public List
public void editPerson(Person lastPerson, int row) { lastPerson.setIndex(row+1); listPeople.set(row, lastPerson); this.fireTableDataChanged(); } }// END OF PERSON TABLE MODEL CLASS
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
