Flash Games

 FAQ   Search   Members   Groups   User Control Panel      Login 

It is currently Fri Dec 05, 2008 1:38 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Java Address Book program
PostPosted: Sat Apr 19, 2008 8:44 am 
Offline

Joined: Sat Apr 12, 2008 6:30 am
Posts: 2
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group - Flash Games

Welcome to DEVPPL.com
You are not logged in, which means that you can't post in the forums.
Click here to Register

If you are a current member here on DEVPPL, please login below:

User:
Pass:
Log me on automatically each visit: