11 Replies - 5458 Views - Last Post: 05 April 2010 - 05:32 AM Rate Topic: -----

#1 smush  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 14-November 09

Array of contacts (address book)

Posted 29 March 2010 - 10:42 PM

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 -
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 -
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.
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) {
	
	
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Array of contacts (address book)

#2 smush  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 14-November 09

Re: Array of contacts (address book)

Posted 30 March 2010 - 05:57 PM

Bump :sad2:
Was This Post Helpful? -1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Array of contacts (address book)

Posted 30 March 2010 - 09:20 PM

So what is your problem ?
What error(s) do you get ?
A compile time or at run time ?
What does your Person class (that must extends contacts) looks like ?
Was This Post Helpful? 0
  • +
  • -

#4 smush  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 14-November 09

Re: Array of contacts (address book)

Posted 30 March 2010 - 10:35 PM

Well 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?

My Person class looks like this:
import java.io.Serializable;

public class Person implements Serializable 
{

	private static int counter;
	public static int count() {return counter;}
	
	protected String name;
	protected String gender;
	protected Date dateOfBirth;
	protected String address;
	protected String postcode;
	
	protected String phoneNo;
	protected int indexNumber;
	
	public Person()
	{
		name = "Unknown";
		setGender('U');
		dateOfBirth = new Date();
		address ="Unknown";
		postcode = "Unknown";
		phoneNo="Unknown";
		indexNumber = counter;
		counter++;
	}
	
	public Person(String nme, char sex, Date dob)
	{
		this();
		name=nme;
		setGender(sex);
		dateOfBirth = new Date(dob);
	}
	
	public Person(Person other)
	{
		this(other.name, other.getGender().charAt(0), other.dateOfBirth);
		address = other.address;
		postcode = other.postcode;
		phoneNo = other.phoneNo;
	}
	
	public void copy(Person other)
	{
		name = other.name;
		gender = other.gender;
		dateOfBirth.copy(other.dateOfBirth);
		postcode = other.postcode;
		address = other.address;
		phoneNo = other.phoneNo;
	}
	
	public String getName()
	{
		return name;
		
	}
	
	public String getGender()
	{
		return gender;
	}
	
	public Date getDateOfBirth()
	{
		return dateOfBirth;
		
	}
	
	public String getAddress()
	{
		return address;
		
	}
	
	public String getPostcode()
	{
		return postcode;
	}
	
	public String getPhoneNo()
	{
		return phoneNo;
		
	}

	public int getIndexNumber()
	{
		return indexNumber;
	}

	public void setName(String nme)
	{
		name = nme;
	}

	public void setGender(char sex)
	{
		if (sex == 'm' || sex == 'M')
			gender = "male";
		else if (sex == 'f' || sex == 'F')
			gender = "female";
		else
			gender = "unknown";
		
	}

	public void setDOB(Date dob)
	{
		dateOfBirth.copy(dob);
	}

	public void setAddress(String addr)
	{
		address = addr;
	}
	
	public void setPostcode(String pcode)
	{
		postcode = pcode;
	}

	public void setPhone(String phone)
	{
		phoneNo = phone;
	}

	/**
	 * override the object toSrting() method
	 */

	public String toString()
	{
		return "   Name: " + name + "\n" +
		"   Gender: " + gender + "\n" +
		"   Date of Birth: " + dateOfBirth + "\n" +
		"   Address: " + address + "\n" +
		"   Postcode: " + postcode + "\n" +
		"   Phone Number: " + phoneNo + "\n" +
		"   Index Number: " + indexNumber + "\n\n";
		
	}


}


Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2650
  • View blog
  • Posts: 11,179
  • Joined: 20-September 08

Re: Array of contacts (address book)

Posted 31 March 2010 - 12:23 AM

You should use a Contact class and store instances in a List<Contact> An array is not appropriate as it is inherently of fixed size
Was This Post Helpful? 1
  • +
  • -

#6 smush  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 14-November 09

Re: Array of contacts (address book)

Posted 01 April 2010 - 04:10 PM

View Postg00se, on 30 March 2010 - 11:23 PM, said:

You should use a Contact class and store instances in a List<Contact> An array is not appropriate as it is inherently of fixed size


So I should use an ArrayList? Also, I have been told to store contacts in a class called Store. Store currently contains the following but I do not know where/how to begin grabbing Swing form data and adding it to the ArrayList in this Store class:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Store implements Serializable
{
	private Person list[];
	private int count;
	private int maxSize;
	private int current = 0;
	private Store outputstore;
	
	public Store(int max)
	{
		list = new Person[max];
		maxSize = max;
		count = 0;
	
	}

	public void add (Person p)
	{
		if (!isFull() && !isIn(p))
		{
			list[count] =p;
			count++;
		}
		else
		{
			System.out.println("Store is full.");
			System.out.println("");
		}
	}
	
	public boolean isFull()
	{
		return count == maxSize;
	}
	
	public boolean isEmpty()
	{
		return count==0;
	}
	
	public boolean isIn(Person p)
	{
		for (int index = 0; index < count; index++)
		{
			if(list[index].equals(p))
			{
				return true;
			}
		}
		return false;
	}
	
	public int getCount()
	{
		return count;
	}

	public Person currentRecord()
	{
		return list[current];
	}

	public void fileout2(String fileName)
	{
		try
		{
			ObjectOutputStream oos = null;
			oos = new ObjectOutputStream(new FileOutputStream(fileName));
			oos.writeObject(this);
		}
		catch(IOException e)
		{
			System.out.println("Error outputting file: " + e);
		}
	}
	
	public Store fileIn2(String fileName)
	{
		Object o = null;
		try
		{
			ObjectInputStream ois = null;
			ois = new ObjectInputStream(new FileInputStream(fileName));
			o = ois.readObject();
		}
		
		catch (IOException io)
		{
			System.out.println("Error file input: " + io);
		}
		
		catch (ClassNotFoundException cnf)
		{
			System.out.println("Error file input: " + cnf);
		}
	
		return(Store)o;
	}
	
	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]);
		
	}


}


Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2650
  • View blog
  • Posts: 11,179
  • Joined: 20-September 08

Re: Array of contacts (address book)

Posted 02 April 2010 - 01:10 AM

Quote

So I should use an ArrayList?


Yes. Declare instead

private List<Person> people;


Was This Post Helpful? 0
  • +
  • -

#8 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Array of contacts (address book)

Posted 03 April 2010 - 08:26 AM

It has already been suggested to use an ArrayList in your daniweb post. And, you've already been told that the actionPerformed method in your "form" class is empty. Now, since you haven't showed how you build the "GUI", no one can say which actionPerformed method you're using. However, since you claimed that that class was "given" to you as part of the assignment, I can only assume that you should be using that one.

Now, make some sort of attempt and post that code if it doesn't do what you want. Also, when you post to a new forum a question you have already asked on another forum, then at least provide a link to that thread so that people do not wind up wasting their time repeating suggestions already made.

http://www.daniweb.c...read271779.html
Was This Post Helpful? 1
  • +
  • -

#9 smush  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 14-November 09

Re: Array of contacts (address book)

Posted 04 April 2010 - 06:02 AM

OK never thought to do that. Not that it matters in this situation as the people over at daniweb don't like to help people :smile2:
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Array of contacts (address book)

Posted 04 April 2010 - 06:33 PM

View Postsmush, on 30 March 2010 - 11:35 PM, said:

Well 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?

Really really bad design :)
You are mixing to much JPanel and data
Keep the data array and the GUI as separated as possible
JPanel are to display array data THATS ALL
You might have a JPanel to confirm to add data into an array but this JPanel should be independant of the array state
Was This Post Helpful? 2
  • +
  • -

#11 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2650
  • View blog
  • Posts: 11,179
  • Joined: 20-September 08

Re: Array of contacts (address book)

Posted 04 April 2010 - 11:09 PM

Quote

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?


As we've said several times, an array is not appropriate - you should use a List.

The functionality you describe should mainly reside in the Contacts class. Contacts should contain List<Contact> and have methods for manipulating Contact instances. The GUI will have the form into which you enter information in Contacts, so should have a reference to that class
Was This Post Helpful? 1
  • +
  • -

#12 smush  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 14-November 09

Re: Array of contacts (address book)

Posted 05 April 2010 - 05:32 AM

I see what I need to do now, thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1