The Artima Developer Community
Sponsored Link

Java Answers Forum
Saving & Opening

1 reply on 1 page. Most recent reply: Jun 24, 2003 3:59 PM by Jaycee

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 1 reply on 1 page
AgentCyan

Posts: 9
Nickname: sulfricyan
Registered: Feb, 2003

Saving & Opening Posted: Jun 24, 2003 1:30 PM
Reply to this message Reply
Advertisement
I am creating an address book and I cant get the input/output to work correctly. I know it is saving things to the file but it isnt working correctly because it is saving the errors that im getting instead of the people in my address book. If someone could take a look at the following code and give me a little help it would be greatly appreciated.

//	Import Statements
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
	//	Person Array Declared
	private static Person person[] = new Person[50];
 
	//	Declared TextFields
	JTextField lastName;
	JTextField firstName;
	JTextField phone;
	JTextField email;
	JTextField city;
	JTextField state;
	JTextField zip;
 
	//	Counter Declared
	private int i;
 
	//	AddressBook Constructor Use To Build The GUI Interface
	public AddressBook()
	{
		buildGui();
	} //	End AddressBook constructor
 
	//	GUI Interface
	public Component buildGui()
	{
		//	GUI Lables
		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");
 
		//	GUI Buttons
		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");
 
		//	GUI TextFields
		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);
 
		//	GUI Pannel
		JPanel pane1 = new JPanel();
 
		//	Set GUI Panel Size
		pane1.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		pane1.setLayout(new GridLayout(10, 2, 10, 10));
 
		//	Adds GUI Componets To The GUI Panel;
		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 The Panel To Be Displayed
		return pane1;
 
	} 	//	End buildGui
 
	public static void main(String args[])
	{
		//	GUI Look And Feel
	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);
		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;
		while (person[i] != null && !person[i].getLastName().equals(s))
		{
			i++;
		}
		return i;
	}
 
	//	Search Method
	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());
			}
		}
	}
 
	//	Add Method
	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++;
			}
		}
	}
 
	//	Delete Method
	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);
			}
		}
	}
 
	//	Save Method
	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();
	  	}
   	}
 
	//	Open Method
   	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
 
//	Person Method
class Person
{
	//	Private Variables
	private String firstName;
	private String lastName;
	private String email;
	private String city;
	private String state;
	private String phoneNumber;
	private String zip;
 
	//	Person Constructor
	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);
	}
 
	//	Set And Get Methods
 
	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;
	}
}


Jaycee

Posts: 26
Nickname: jaycee
Registered: Apr, 2003

Re: Saving & Opening Posted: Jun 24, 2003 3:59 PM
Reply to this message Reply
make Person implement java.io.Serializable

Flat View: This topic has 1 reply on 1 page
Topic: Compiling problem with javac.Main.compile Previous Topic   Next Topic Topic: threads

Sponsored Links



Google
  Web Artima.com   

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