It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Java Forum

Java Address Book program

Java Address Book program

Postby ScottyMalaysia on Sat Apr 19, 2008 7:44 am

For my programming class, I have to write a Java program that works as an address book. I have created a Contact class with methods such as SetName, getName, etc. and an AddressBook class with an array to store the data. The main class has a GUI with four buttons: Add, Search, Delete, and Update. The Add and Search buttons work, but there is a problem with the Delete button - the program won't compile the code that I've written for it, and I can't see why.

Here is the code (I've removed irrelevant bits):

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class AddressBook1_3 extends JFrame implements ActionListener {

(data members declared here)

AddressBook myBook;

public static void main (String[] args) {
AddressBook1_3 book = new AddressBook1_3();
book.setupArray(20);
book.setVisible(true);
}

public void setupArray(int N){
myBook = new AddressBook(N);
}

public AddressBook1_3() {
Container contentPane;

setSize (FRAME_WIDTH, FRAME_HEIGHT);
setResizable(false);
setTitle("SM Address Book Version 1.3");
setLocation(FRAME_X_LOCATION, FRAME_Y_LOCATION);

(GUI objects are placed in a ContentPane here)

addButton.addActionListener(this);
searchButton.addActionListener(this);
updateButton.addActionListener(this);
deleteButton.addActionListener(this);

setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent event) {

if(event.getSource() == addButton) {
addContact();
clear();
}

else if(event.getSource() == updateButton) {
updateContact();
clear();
}

else if(event.getSource() == searchButton) {
searchContact();
clear();
}

else if(event.getSource() == deleteButton) {
//deleteContact();
clear();
}

}

public void addContact() {

Contact person = new Contact();
person.SetName(nmefield.getText());
person.SetAddress(adrsfield.getText());
person.SetPhone(phfield.getText());
person.SetEmail(emfield.getText());
myBook.add(person);

}

public void searchContact() {

Contact contact;

contact = myBook.search(JOptionPane.showInputDialog(null, "Enter name to search for"));

if (contact == null) {
JOptionPane.showMessageDialog(null, "Contact not found.");

} else {
/*nmefield.setText(contact.getName());
adrsfield.setText(contact.getAddress());
phfield.setText(contact.getPhone());
emfield.setText(contact.getEmail());*/

JOptionPane.showMessageDialog(null,"Name: " + contact.getName() + "\n" + "Address: " + contact.getAddress() + "\n" + "Phone: " + contact.getPhone() + "\n" + "E-Mail: " + contact.getEmail());

}
}

public void updateContact() {

// Not sure how to do this
JOptionPane.showMessageDialog(null, "Coming Soon!");
}

public void deleteContact() {

Contact contact;

contact = myBook.search(nmefield.getText());

if (contact == null) {
JOptionPane.showMessageDialog(null, "Error: Contact Not Found.");
} else {
boolean success = myBook.delete(contact);

if (success) {
contact = myBook.search(nmefield.getText());

if (contact == null) {
JOptionPane.showMessageDialog(null, "Contact Deleted");
} else {
JOptionPane.showMessageDialog(null, "Error: Contact is still there");
}
} else {
JOptionPane.showMessageDialog(null, "Error: Deletion has a problem");
}
}
}




public void clear() {

nmefield.setText("");
adrsfield.setText("");
phfield.setText("");
emfield.setText("");
}
}

When this code is compiled, the following error message is shown:

AddressBook1_3.java:175: delete(java.lang.String) in AddressBook cannot be applied to (Contact)
boolean success = myBook.delete(contact);
^

What is the problem here?

And about the updateContact method: I know how to get the values from the JTextFields, but how do I change the address, phone or email values without having to create a new contact with the same name but different values? (which comes back to the delete function)

If necessary, I can post the Contact and AddressBook classes as well.
ScottyMalaysia
 
Posts: 2
Joined: Sat Apr 12, 2008 5:30 am

Who is online

Users browsing this forum: No registered users and 1 guest