The Artima Developer Community
Sponsored Link

Java Answers Forum
Address Book

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
AgentCyan

Posts: 9
Nickname: sulfricyan
Registered: Feb, 2003

Address Book Posted: May 5, 2003 12:44 AM
Reply to this message Reply
Advertisement
This is the current code that i have working for my address book. Everything works except for the DeteleAction, SaveAction, and OpenAction. If anyone could reply with some help sometime today it would be greatly appreciated that so i can hand something in on time for once, lol...

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javabook.*;
import java.awt.event.ActionEvent;
 
public class AddressBook
{
	// Use these class variables to store values for searching
	private static Person person[] = new Person[50];
	JTextField lastName;
	JTextField firstName;
	JTextField phone;
	JTextField email;
	JTextField city;
	JTextField state;
	JTextField zip;
	private int i;
 
	public AddressBook()
	{
		buildGui();
	} // end AddressBook constructor
 
	public Component buildGui()
	{
 
		final JLabel firstLabel = new JLabel("First Name");
		final JLabel lastLabel = new JLabel("Last Name");
		final JLabel phoneLabel = new JLabel("Phone Number");
		final JLabel emailLabel = new JLabel("E-mail Address");
		final JLabel cityLabel = new JLabel("City");
		final JLabel stateLabel = new JLabel("State");
		final JLabel zipLabel = new JLabel("Zip");
		final JButton add = new JButton(new AddAction());
		final JButton delete = new JButton(new DeleteAction());
		final JButton search = new JButton(new SearchAction());
		final JButton save = new JButton(new SaveAction());
		final JButton open = new JButton(new OpenAction());
		final JButton view = new JButton("View Address Book");
		firstName = new JTextField(15);
		lastName = new JTextField(15);
		phone = new JTextField(15);
		email = new JTextField(15);
		city = new JTextField(15);
		state = new JTextField(15);
		zip = new JTextField(15);
		JPanel pane1 = new JPanel();
		pane1.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		pane1.setLayout(new GridLayout(10, 2, 10, 10));
		pane1.add(firstLabel);
		pane1.add(firstName);
		pane1.add(lastLabel);
		pane1.add(lastName);
		pane1.add(phoneLabel);
		pane1.add(phone);
		pane1.add(emailLabel);
		pane1.add(email);
		pane1.add(cityLabel);
		pane1.add(city);
		pane1.add(stateLabel);
		pane1.add(state);
		pane1.add(zipLabel);
		pane1.add(zip);
		pane1.add(add);
		pane1.add(delete);
		pane1.add(search);
		pane1.add(view);
		pane1.add(save);
		pane1.add(open);
 
		return pane1;
 
		// put addressbook gui componenets in here
		// create frame/panels etc..
		// example
	} // end buildGui
 
	public static void main(String args[])
	{
	try
		{
			UIManager.setLookAndFeel(
			UIManager.getCrossPlatformLookAndFeelClassName());
		}
		catch (Exception e)
		{
		}
 
		//Create the top-level container and add contents to it.
		JFrame frame = new JFrame("AddressBook");
		AddressBook app = new AddressBook();
		Component contents = app.buildGui();
		frame.getContentPane().add(contents, BorderLayout.CENTER);
		//Finish setting up the frame, and show it.
 
		frame.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
		frame.pack();
		frame.setVisible(true);
	} // end main method
	private int search(String s)
	{
		int i= 0;
		//put search array here - return the value of index of found item or null address
		while (person[i] != null && !person[i].getLastName().equals(s))
		{
			i++;
		}
		return i;
	}
 
	private class SearchAction extends AbstractAction
	{
		public SearchAction()
		{
			putValue(NAME, "Search");
			putValue(SHORT_DESCRIPTION, "Search");
		}
		public void actionPerformed(ActionEvent e)
		{
			i = search(lastName.getText());
			if(person[i] == null)
			{
				JOptionPane.showMessageDialog(null, lastName.getText() + " Person Not Found");
				lastName.setText(" ");
			}
			else
			{
				firstName.setText(person[i].getFirstName());
				lastName.setText(person[i].getLastName());
			 	phone.setText(person[i].getPhone());
			 	email.setText(person[i].getEmail());
			 	city.setText(person[i].getCity());
			 	state.setText(person[i].getState());
			 	phone.setText(person[i].getPhone());
			 	zip.setText(person[i].getZip());
			}
		}
	}
	private class AddAction extends AbstractAction
	{
 
		public AddAction()
		{
			putValue(NAME, "Add");
			putValue(SHORT_DESCRIPTION, "Add");
		}
		public void actionPerformed(ActionEvent e)
		{
			int i = 0;
			while (person[i] != null)
				i++;
			if (person[i] == null)
			{
				person[i] = new Person(firstName.getText(),lastName.getText(),phone.getText(),
				email.getText(),city.getText(),state.getText(),zip.getText());
				firstName.setText(" ");
				lastName.setText(" ");
				phone.setText(" ");
				email.setText(" ");
				city.setText(" ");
				state.setText(" ");
				zip.setText(" ");
				JOptionPane.showMessageDialog(null, person[i].getFirstName() + " " + person[i].getLastName() + " Was Added!");
				i++;
			}
		}
	}
	private class DeleteAction extends AbstractAction
	{
		public DeleteAction()
		{
			putValue(NAME, "Delete");
			putValue(SHORT_DESCRIPTION, "Delete");
		}
		public void actionPerformed(ActionEvent e)
		{
			int i = 0;
			i = search(lastName.getText());
			while (!person[i].getLastName().equals(lastName) && person[i] != null)
				i++;
			if(person[i].getLastName().equals(lastName))
			{
				firstName.setText(" ");
				lastName.setText(" ");
				phone.setText(" ");
				email.setText(" ");
				city.setText(" ");
				state.setText(" ");
				zip.setText(" ");
				person[i].setFirstName(null);
				person[i].setLastName(null);
				person[i].setEmail(null);
				person[i].setCity(null);
				person[i].setState(null);
				person[i].setPhone(null);
				person[i].setZip(null);
			}
			// put code for deleting here
		}
	}
	private class SaveAction extends AbstractAction
	{
		public SaveAction()
		{
			putValue(NAME, "Save");
			putValue(SHORT_DESCRIPTION, "Save");
		}
		public void actionPerformed(ActionEvent e)throws IOException, ClassNotFoundException
		{
			MainWindow mainWindow;
			mainWindow = new MainWindow("");
			FileDialog fileBox = new FileDialog(mainWindow, "Save", FileDialog.SAVE);
			fileBox.setVisible(true);
 
			File outFile = new File("person.dat");
			FileOutputStream outFileStream = new FileOutputStream(outFile);
			ObjectOutputStream outObjectStream = new ObjectOutputStream(outFileStream);
			outObjectStream.writeObject(person);
			outObjectStream.close();
	  	}
   	}
 
   	private class OpenAction extends AbstractAction
   	{
		public OpenAction()
		{
			putValue(NAME, "Open");
			putValue(SHORT_DESCRIPTION, "Open");
		}
		public void actionPerformed(ActionEvent e)throws IOException, ClassNotFoundException
		{
			MainWindow mainWindow;
			mainWindow = new MainWindow("");
			FileDialog fileBox = new FileDialog(mainWindow, "Open", FileDialog.LOAD);
			fileBox.setVisible(true);
			File inFile = new File("person.dat");
			FileInputStream inFileStream = new FileInputStream(inFile);
			ObjectInputStream inObjectStream = new ObjectInputStream(inFileStream);
			inObjectStream.readObject();
			inObjectStream.close();
		}
	}
 
} // end AddressBook class
 
class Person
{
	private String firstName;
	private String lastName;
	private String email;
	private String city;
	private String state;
	private String phoneNumber;
	private String zip;
 
	public Person()
	{
	}
 
	//My constructor for the person class
	public Person(String a, String b, String c,
	String e, String f, String g, String h)
	{
		setFirstName(a);
		setLastName(b);
		setEmail(c);
		setCity(e);
		setState(f);
		setPhone(g);
		setZip(h);
	}
 
	//These are my methods to try to get and send the text you enter
 
	public String getFirstName()
	{
		return firstName;
	}
 
	public void setFirstName(String firstNam)
	{
		firstName = firstNam;
	}
 
	public String getLastName()
	{
		return lastName;
	}
 
	public void setLastName(String lastNam)
	{
		lastName = lastNam;
	}
 
	public String getEmail()
	{
		return email;
	}
 
	public void setEmail(String mail)
	{
		email = mail;
	}
 
	public String getCity()
	{
		return city;
	}
 
	public void setCity(String city1)
	{
		city = city1;
	}
 
	public String getState()
	{
		return state;
	}
 
	public void setState(String stat)
	{
		state = stat;
	}
 
	public String getPhone()
	{
		return phoneNumber;
	}
 
	public void setPhone(String phoneNumb)
	{
		phoneNumber = phoneNumb;
	}
 
	public String getZip()
	{
		return zip;
	}
 
	public void setZip(String zip1)
	{
		zip = zip1;
	}
}

Topic: Pause in JAVA Previous Topic   Next Topic Topic: Please help with arrays

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use