Sunday, November 7, 2010

Create a program that will write and read the same object from a file and display the content on the screen

/* Program : 2 
   Create a program that will write and read the same object from a file and display the content on the screen
   Developed by : Malhar Vora
   Developed on : 5-11-2010
   Development Status : Completed and tested
   Email     : vbmade2000@gmail.com
   WebSite   : http://technojungle.co.cc
*******************************************************************************************************************/
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Student implements Serializable
{
 private int id;
 private String name;
 private String course;
  
 Student()
 {
   id=0;
   name="";
   course="";
 }
 
 Student(int sid,String sname,String scourse)
 {
   this.id=sid;
   this.name=sname;
   this.course=scourse;
 }
 
 int getId()
 {
   return this.id;
 }
 
 void setId(int sid)
 {
   this.id=sid;
 }
 
 String getName()
 {
   return this.name;
 }
 
 void setName(String sname)
 {
   this.name=sname;
 }
 
 String getCourse()
 {
   return this.course;
 }
 
 void setCourse(String scourse)
 {
   this.course=scourse;
 }
 
  
}


public class P2 extends JFrame implements ActionListener
{
 
 JLabel lblId=null;
 JLabel lblName=null;
 JLabel lblCourse=null;
 JTextField txtId=null;
 JTextField txtName=null;
 JTextField txtCourse=null;
 JButton btnSave=null;
 JButton btnClear=null;
 JButton btnLoad=null;
 
 
 P2()
 {
   
   lblId = new JLabel("Student ID");
   lblName = new JLabel("Student Name");
   lblCourse = new JLabel("Course");
   txtId = new JTextField(5);
   txtName = new JTextField(20);
   txtCourse = new JTextField(40);
   btnSave  = new JButton("Save");
   btnClear  = new JButton("Clear");
   btnLoad  = new JButton("Load");  
   
   
   
   setTitle("Program 2 - Developed by Malhar Vora");
   setSize(400,500);
   setVisible(true);
   setDefaultCloseOperation(EXIT_ON_CLOSE);
   setLayout(null);
   
   
   lblId.setBounds(50,50,100,50);
   lblName.setBounds(50,90,100,50);
   lblCourse.setBounds(50,130,100,50);
   txtId.setBounds(150,60,60,30);
   txtName.setBounds(150,100,200,30);
   txtCourse.setBounds(150,140,100,30);
   btnSave.setBounds(50,200,80,30);  
   btnClear.setBounds(150,200,80,30);  
   btnLoad.setBounds(250,200,80,30);
   
   btnSave.setMnemonic(KeyEvent.VK_S);  
   btnClear.setMnemonic(KeyEvent.VK_C);  
   btnLoad.setMnemonic(KeyEvent.VK_L);  
   
   this.getContentPane().add(lblId);
   this.getContentPane().add(lblName);
   this.getContentPane().add(lblCourse);
   this.getContentPane().add(txtId);
   this.getContentPane().add(txtName);
   this.getContentPane().add(txtCourse);
   this.getContentPane().add(btnSave);
   this.getContentPane().add(btnClear);
   this.getContentPane().add(btnLoad);
   
   txtId.requestFocus();
   
   btnSave.addActionListener(this); 
   btnClear.addActionListener(this); 
   btnLoad.addActionListener(this); 
   
 }
 
 void saveStudent(Student s)
 {
   FileOutputStream fos=null;
   ObjectOutputStream oos=null;
   try{

     fos = new FileOutputStream("Student.dat");
     oos = new ObjectOutputStream(fos);
     oos.writeObject(s);     
     
   }
   catch(Exception e)
   {
    JOptionPane.showMessageDialog(this,"Error in writing data to file","Error",JOptionPane.ERROR_MESSAGE);
   }
   finally
   {
    try{
      oos.close();
      fos.close();
    }
    catch(Exception e)
    {
     ;
    }
   }
 
 }
 
 Student loadStudent()
 {
  FileInputStream fis=null;
  ObjectInputStream ois=null;
  Student s=null;
  
  try{
   
    fis = new FileInputStream("Student.dat");
    ois = new ObjectInputStream(fis);
    s=(Student)ois.readObject();    
  
  }
  catch(Exception e)
  {
    JOptionPane.showMessageDialog(this,"Error in reading data","Error",JOptionPane.ERROR_MESSAGE);
  }
  finally
  {
    try{
     ois.close();
     fis.close();
    }
    catch(Exception e)
    {
     ;
    }
  }
  return s;
 
 }
 
 public void actionPerformed(ActionEvent ae)
 {
  String cmd = ae.getActionCommand();
  Student s=null;
  
  if(cmd.equals("Save"))
  {
 
   s = new Student();
   s.setId(Integer.parseInt(txtId.getText()));
   s.setName(txtName.getText());
   s.setCourse(txtCourse.getText());
   
   saveStudent(s);
  }
  else if(cmd.equals("Clear"))
  {
   txtId.setText("");
   txtName.setText("");
   txtCourse.setText("");   
  }
  else if(cmd.equals("Load"))
  {
   s=null;
   s=loadStudent();
   txtId.setText("");
   txtName.setText("");
   txtCourse.setText(""); 
   if(s!=null)
   {
    txtId.setText("" + s.getId());   
    txtName.setText(s.getName());
    txtCourse.setText(s.getCourse());
   }
  }
 }
 
 public static void main(String []str)
 {
   P2 p = new P2();
 }

 

}
  
 

No comments:

Post a Comment