/**
 * 编写一个Person类,包含序号、姓名、年龄、性别等4个属性,再编写一个Application程序
 * ,采用ObjectOutputStream类Person类的若干个对象实例输出到外部设备的文件上,再采用
 * ObjectInputStream类从外部设备的文件上读取这些对象实例并显示到图形用户界面上。
 * */

/**
 * @author renwenchao
 * @version 1/6/2011
 * */

import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.util.*;

public class javaHomeWork5_2 extends JFrame implements ActionListener{

	Button saveButton;
	Button openButton;
	Button showButton;
	
	JPanel panel;
	JTextArea textArea;
	
	String str="";
	
	Person person1;
	Person person2;
	Person person3;
	Person[] persons;
	
	
	/**
	 * 构造函数
	 * */
	javaHomeWork5_2(){
		setTitle("renwenchao");
		setSize(500 , 500);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		panel=new JPanel();
		textArea=new JTextArea();
		JScrollPane js=new JScrollPane(textArea);
		
		openButton=new Button("OpenFile");
		saveButton=new Button("SaveFile");
		showButton=new Button("ShowFile");
		
		panel.setLayout(new FlowLayout());
		panel.add(openButton);
		panel.add(saveButton);
		panel.add(showButton);
		//panel.add(js);
		panel.add(textArea);
		add(panel);
		
		
		openButton.addActionListener(this);
		saveButton.addActionListener(this);
		showButton.addActionListener(this);
		
		 person1=new Person("A",12, 123);
		 person2=new Person("B",12, 123);
		 person3=new Person("C",12, 123);
		 
		 persons=new Person[3];
		 persons[0]=person1;
		 persons[1]=person2;
		 persons[2]=person3;
	}
	
	/**
	 * 处理Button事件函数
	 * */
	public void actionPerformed(ActionEvent event){
		if(event.getSource()==openButton){
			openFile();
		}
		
		if(event.getSource()==saveButton){
			try {
				saveFile();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(event.getSource()==showButton){
			try {
				showFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 打开文件函数
	 * */
	public void openFile(){
		
	}
	
	/**
	 * 保存文件函数
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 * */
	public void saveFile() throws FileNotFoundException, IOException{
		
	
		
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("employee.dat"));
        out.writeObject(persons);
        out.close();
		
	}
	
	
	/**
	 * 在文本域现实内文件内容
	 * @throws ClassNotFoundException 
	 * @throws IOException 
	 * */
	public void showFile() throws IOException, ClassNotFoundException{
		
		ObjectInputStream in = new ObjectInputStream(new FileInputStream("employee.dat"));
         Person[] newPerson = (Person[]) in.readObject();
        
         for(Person per: persons){
        	 str+=per.toString();
        	 str+="\n";
         }
         
         textArea.setText(str);
         in.close();
	}
	
	
	/**
	 * 主函数
	 * */
	public static void main(String[] args){
		new javaHomeWork5_2();
	}
	
}






/**
 * Person类
 * */
 
class Person implements Serializable{
	
	private String name;
	private int age;
	private double selary;
	
	public Person(){
		
	}
	
	Person(String name, int age, double selary){
		this.name=name;
		this.age=age;
		this.selary=selary;
	}
	
	public String getName(){
		return name;
	}
	
	public int getAge(){
		return age;
	}
	
	public double getSelary(){
		return selary;
	}
}