Array of contacts (address book)
Hi,
I'm creating a Java Swing application that lets the user enter the usual details one would expect in an address book then click the save button. The current JPanel will then be hidden while another JPanel is shown. This second panel is used for viewing all the contacts added to the address book so will have buttons to flick forward and backward through the book. I have the GUI part done for the new contact JPanel but I'm struggling in getting what is entered, stored into an array for use later when viewing the contacts. I have numerous classes but the ones I think are needed for help with the array is my GUI class and contacts class files so I will include some code snippets for visual aid as a means to point me in the right direction. I thank you in advance for any help provided :-).
GUI Class -
Code :
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == saveButton){
addContactPanel.setVisible(false);
viewAllPanel.setVisible(true);
}
if (e.getSource() == quitButton){
System.exit(0);
}
}
public static void main(String[] args) {
GUIClass form = new GUIClass();
form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
form.setVisible(true);
}
}
Contacts class -
Code :
import java.io.*;
public class contacts implements Serializable
{
private contacts list[];
private int current = 0;
private int count;
private int maxSize;
private contacts outputFile;
public contacts(int max)
{
list = new contacts[max];
maxSize = max;
count = 0;
}
public void add (contacts c)
{
if (!isFull() && !isIn(p))
{
list[count] =p;
count++;
}
else
{
System.out.println("Contact list is full!.");
System.out.println("");
}
}
public boolean isFull()
{
return count == maxSize;
}
public boolean isEmpty()
{
return count==0;
}
public boolean isIn(contacts c)
{
for (int index = 0; index < count; index++)
{
if(list[index].equals(p))
{
return true;
}
}
return false;
}
public int getCount()
{
return count;
}
public contacts currentRecord()
{
return list[current];
}
public void incrementCurrentPointer()
{
this.current++;
if (current>=count)
{
current=0;
}
}
public void decrementCurrentPointer()
{
this.current--;
if(current<0)
{
current=count-1;
}
}
public void displayAll()
{
for (int index=0; index < count; index++)
System.out.println(list[index]);
}
}
I also have a class with the following code in which was provided for me but I am not entirely sure of its purpose. If anyone could also explain this to me I would be forever grateful.
Code :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.Serializable;
public class MainForm extends JFrame implements ActionListener, Serializable
{
contacts book = new contacts(50);
public MainForm()
{
super("Address Book");
contact contact1= new Person ("Ryan", 'M', new Date (2 ,05, 58));
contact contact2= new Person ("David", 'M', new Date (17 ,09, 71));
book.add(contact1);
book.add(contact2);
}
public void actionPerformed(ActionEvent e) {
}
}
Re: Array of contacts (address book)
Hrmm. Is this forum still alive or is it full of people with questions but nobody here to answer them? :(
Re: Array of contacts (address book)
Quote:
Originally Posted by
smush
Hrmm. Is this forum still alive or is it full of people with questions but nobody here to answer them? :(
People are here, but your original post was looking for general advice rather than a specific question - the latter of which generates responses much quicker. Now back to the problem at hand...
The contacts class is a bit strange, in that it seems recursive in nature and creates some possibly unused arrays (unless you want to create a web-like contact program where you can add contacts to contacts, that's a while different story). I'd recommend either a) making a new class (named say ContactList) that stores your list of contacts or b) creating an instance list in another class to do so. In addition, as opposed to using an array to store the list, you can use a resizable storage object like ArrayList or OrderedHashSet.
Quote:
I also have a class with the following code in which was provided for me but I am not entirely sure of its purpose. If anyone could also explain this to me I would be forever grateful.
Looks like an incomplete GUI interface to me...extends from a JFrame and implements ActionListener, if you are unfamiliar with any of this see Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
Re: Array of contacts (address book)
I know I need to use an ArrayList but the problem is I need to get what is entered into the 'New contact' form into an array then access that array on another JPanel but I do not know how I go about adding a contact along with their details into an array and which class to put it in. Does it go in the GUI class? MainForm class or Contacts class?
Re: Array of contacts (address book)
I knew this forum was poor by how the website isn't correctly set-up didn't think it was going to be this bad. I don't think you have enough returning visitors to make a success of this. Save your money and close it down, there are many other more successful forums on the internet already. :-)
Re: Array of contacts (address book)
Recommended reading for smush:
How To Ask Questions The Smart Way
Good questions get answered.
db
Re: Array of contacts (address book)
Quote:
Originally Posted by
smush
I knew this forum was poor by how the website isn't correctly set-up didn't think it was going to be this bad. I don't think you have enough returning visitors to make a success of this. Save your money and close it down, there are many other more successful forums on the internet already. :-)
You didn't give a very specific question, you asked people to explain your own code. ~X(
Re: Array of contacts (address book)
Yes I did. I said I need an ArrayList but don't know where it should go and how would I get started....
Re: Array of contacts (address book)
Quote:
Originally Posted by
Darryl.Burke
I asked nicely and just wished for help but when people are too full of themselves to help new comers then a forum fails in it's duty.
Re: Array of contacts (address book)
As with any forum of this sort most people who contribute do so out of their own spare time. It should be respected not considered a right. This for free. Fails in its duty? I don't recall when it was this forum's duty or any contributor's duty to spend their free time trying to help. This is a resource...whether it helps you or not, pisses you off or not, solves your problems or not: its just a resource.
Now, I'm a nice guy and despite the tension being thrown around still willing to help you with your problem. Here's how I would do it: Remove the array from your contacts class its confusing and recursive (in fact I'd rename it Contact so the plural lends no confusion). In your GUI class - which I assume is a JFrame - create an ArrayList of type Contact. You can directly add or remove from the List in your actionPerformed method. If you have other GUI objects that rely on access to the ArrayList, either a) pass a reference of the List to those Object so they can access it or b) make those inner classes of 'GUI class' (in which case they have direct access)...All these suggestions - in my free time after a long day at work, not my duty.
Re: Array of contacts (address book)
Thank you Copeg. At least someone helps around here.
Re: Array of contacts (address book)
Quote:
Originally Posted by
copeg
As with any forum of this sort most people who contribute do so out of their own spare time. It should be respected not considered a right. This for free. Fails in its duty? I don't recall when it was this forum's duty or any contributor's duty to spend their free time trying to help. This is a resource...whether it helps you or not, pisses you off or not, solves your problems or not: its just a resource.
Now, I'm a nice guy and despite the tension being thrown around still willing to help you with your problem. Here's how I would do it: Remove the array from your contacts class its confusing and recursive (in fact I'd rename it Contact so the plural lends no confusion). In your GUI class - which I assume is a JFrame - create an ArrayList of type Contact. You can directly add or remove from the List in your actionPerformed method. If you have other GUI objects that rely on access to the ArrayList, either a) pass a reference of the List to those Object so they can access it or b) make those inner classes of 'GUI class' (in which case they have direct access)...All these suggestions - in my free time after a long day at work, not my duty.
I agree with your idea