Thursday, June 5, 2008

Employee Database and Maintenance project in Java

This is my Project about Employee Database information and Maintenance using RandomAccessFile. This program performs the functions like reading, writing, adding records, deleting records, searching a particular record, navigating between records and so on.

Program Description and How it Works:

All classes in the java.io packages either allow us to perform only read operations or only write operations. None of these classes is designed to perform both read and write operations on the disk file as they are derived from either just InputStream or OutputStream and Java does not permit multiple inheritance. But, a special class is designed and made available called RandomAccessFile to declare an object that allows you to access any section of the file using the method called seek(), which allows you to move the file pointer to any place in the file. There is also a method to know where the pointer is currently pointing called getFilePointer().

Here is the Program Code for Employee Details:

import java.math.BigDecimal;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.io.*;
import java.awt.Graphics;

public class TestRAFFrame extends Frame
{
int empid;
String fname;
String lname;
String dept;
String desig;
float basic;
float da;
float hra;
float pf;
float netsal;

RandomAccessFile raf;
char mode;

Label lempno = new Label ("Emp# : ");
Label lfname = new Label ("First Name : ");
Label llname = new Label ("Last Name : ");
Label ldept = new Label ("Department : ");
Label ldesig = new Label ("Designation : ");
Label lbasic = new Label ("Basic : ");
Label lda = new Label ("D.A. : ");
Label lhra = new Label ("H.R.A. : ");
Label lpf = new Label ("P.F. : ");
Label lnetsal = new Label ("Net Salary : ");

TextField tempno = new TextField(5);
TextField tfname = new TextField(20);
TextField tlname = new TextField(15);
Choice cdept = new Choice();
Choice cdesig = new Choice();
TextField tbasic = new TextField(5);
TextField tda = new TextField(5);
TextField thra = new TextField(5);
TextField tpf = new TextField(5);
TextField tnetsal = new TextField(6);

Button add = new Button("Add");
Button edit = new Button("Edit");
Button delete = new Button ("Delete");
Button display = new Button ("Display");
Button cancel = new Button ("Cancel");
Button exit = new Button ("Exit");

static TestRAFFrame win;
long pos=OL;
public static void main(String args[]) throws IOException
{
win = new TestRAFFrame();
}
public TestRAFFrame() throws IOException
{
super ("Employee Details");
pack();
show();
showStage();
raf = new RandomAccessFile("Emp.dat","rw");
addWindowListener (new WindowEventHandler());
}
public void showStage()
{
setLayout (new BorderLayout());

Panel upper = new Panel();
Panel lower = new Panel();
Panel inlower = new Panel();

lower.setLayout(new FlowLayout(FlowLayout.CENTER));
inlower.setLayout(new GridLayout(1,6));
inlower.add(add);
inlower.add(edit);
inlower.add(delete);
inlower.add(display);
inlower.add(cancel);
inlower.add(exit);
lower.add(inlower);
upper.setLayout(new GridLayout(5,4));

cdept.addItem("Account");
cdept.addItem("Admin");
cdept.addItem("Production");
cdept.addItem("Sales");

cdesig.addItem("Manager");
cdesig.addItem("Officer");
cdesig.addItem("Engineer");
cdesig.addItem("Clerk");

tempno.setBackground(Color.white);
tfname.setBackground(Color.white);
tlname.setBackground(Color.white);
tbasic.setBackground(Color.white);
tda.setBackground(Color.white);
thra.setBackground(Color.white);
tpf.setBackground(Color.white);
tnetsal.setBackground(Color.white);

upper.add(lempno);
upper.add(tempno);
upper.add(ldept);
upper.add(cdept);
upper.add(lfname);
upper.add(tfname);
upper.add(llname);
upper.add(tlname);
upper.add(ldesig);
upper.add(cdesig);
upper.add(lbasic);
upper.add(tbasic);
upper.add(lda);
upper.add(tda);
upper.add(lhra);
upper.add(thra);
upper.add(lpf);
upper.add(tpf);
upper.add(lnetsal);
upper.add(tnetsal);

tempno.setEditable(false);
tfname.setEditable(false);
tlname.setEditable(false);
tbasic.setEditable(false);
tda.setEditable(false);
thra.setEditable(false);
tpf.setEditable(false);
tnetsal.setEditable(false);

tfname.addFocusListener (new HandleTextField());
tlname.addFocusListener (new HandleTextField());
tempno.addFocusListener (new HandleTextField());
tbasic.addFocusListener (new HandleTextField());

tbasic.addFocusListener (new HandleNumbers());
add.addActionListener (new ButtonHandler());
edit.addActionListener (new ButtonHandler());
delete.addActionListener (new ButtonHandler());
display.addActionListener (new ButtonHandler());
cancel.addActionListener (new ButtonHandler());
exit.addActionListener (new ButtonHandler());

add ("North", upper);
setBackground (Color.darkGray);
setForeground (Color.black);

add ("Center", lower);
Font font = new Font ("SansSerif", Font.PLAIN,11);
setFont(font);
}

class ButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent ae)
{
String cmd = ae.getActionCommand();
if (cmd.equals ("Add"))
{
mode='a';
add.setLabel ("Save");
tempno.setEditable (true);
tfname.setEditable (true);
tlname.setEditable (true);
tbasic.setEditable (true);
tda.setEditable (true);
thra.setEditable (true);
tpf.setEditable (true);
tnetsal.setEditable (true);

ClearDetails();
tempno.requestFocus();
}
if (cmd.equals("Edit));
{
mode='e';
add.setLabel ("Save");

InputBox inputEmpid = new InputBox(win, "Search Employee");
inputEmpid.setSize(300,100);
input Empid.setLocation(200,50);
inputEmpid.show();

tlname.setEditable(true);
tbasic.setEditable(true);
tlname.requestFocus();
}
if (cmd.equals("Delete"))
{
try
{
RandomAccessFile raftemp = new RandomAccessFile ("Temp.dat","rw");
raf.seek(OL);
while (raf.getFilePointer() < raf.length())
{
ReadFromFile();
System.out.println(empid);
System.out.println(Integer.parseInt(tempno.getText()));
if (empid ! = Integer.parseInt(tempno.getText()))
{
raftemp.write Int(empid);
raftemp.writeUTF(fname);
raftemp.writeUTF(lname);
raftemp.writeUTF(dept);
raftemp.writeUTF(desig);
raftemp.writeFloat(basic);
raftemp.writeFloat(da);
raftemp.writeFloat(hra);
raftemp.writeFloat(pf);
raftemp.writeFloat(netsal);
}
}
raf.close();
raftemp.close();
File f1= new File ("emp.dat");
File f2= new File ("temp.dat");

f1.delete();
f2.renameTo(f1);
f1=null;
f2=null;
raf=new RandomAccessFile ("emp.dat","rw");
}
catch(IOException ioe)
{
}
}
if (cmd.equals("Display"))
{
InputBox inputEmpid = new InputBox (win, "Search Employee");
inputEmpid.setSize(300,100);
inputEmpid.setLocation(200,50);
inputEmpid.show();
}
if (cmd.equals ("Save"))
{
tempno.setEditable(false);
tfname.setEditable(false);
tlname.setEditable(false);
tbasic.setEditable(false);
tda.setEditable(false);
thra.setEditable(false);
tpf.setEditable(false);
tnetsal.setEditable(false);

if (mode == 'a')
{
add.setLabel("Add");
try
{
raf.seek(OL);
int tempid = Integer.parseInt (tempno.getText());
int flag = 0;
while (raf.getFilePointer() < raf.length())
{
pos = raf.getFilePointer();
ReadFromFile();
if (empid == tempid)
{
DialogBox db = new DialogBox(win, "Search Result", "Duplicate Entry made... Please Reenter Employee ID");
db.pack();
db.setSize(200,100);
db.setLocation(200,50);
db.show();
ClearDetails();
flag=1;
}
}

if (flag==0)
{
raf.seek(raf.length());
empid = Integer.parseInt(tempno.getText());
fname = tfname.getText();
lname = tlname.getText();
dept = cdept.getSelectedItem();
desig = cdesig.getSelectedItem();
basic = Float.parseFloat(tbasic.getText());
da = Float.parseFloat(tda.getText());
hra = Float.parseFloat(thra.getText());
pf = Float.parseFloat(tpf.getText());
netsal = Float.parseFloat(tnetsal.getText());

WriteToFile();
}
}
catch(IOException e)
{
} }
if (mode == 'e')
{
try{
add.setLabel ("Add");
raf.seek(pos);
WriteToFile();
}
catch(IOException ioe)
{
} }
}
if (cmd.equals("Cancel"))
{
ClearDetails();
}
if (cmd.equals("Exit"))
{
System.exit(0);
}
}
public void ReadFromFile() throws IOException
{
empid = raf.readInt();
fname = raf.readUTF();
lname = raf.readUTF();
dept = raf.readUTF();
desig = raf.readUTF();
basic = raf.readFloat();
da = raf.readFloat();
hra = raf.readFloat();
pf = raf.readFloat();
netsal = raf.readFloat();
}
public void WriteToFile() throws IOException
{
raf.write Int(empid);
raf.writeUTF(fname);
raf.writeUTF(lname);
raf.writeUTF(dept);
raf.writeUTF(desig);
raf.writeFloat(basic);
raf.writeFloat(da);
raf.writeFloat(hra);
raf.writeFloat(pf);
raf.writeFloat(netsal);
}
public void ClearDetails()
{
tempno.setText("");
tfname.setText("");
tlname.setText("");
tbasic.setText("");
tda.setText("");
thra.setText("");
tpf.setText("");
tnetsal.setText("");
}
}
class WindowEventHandler extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
class HandleTextField implements FocusListener
{
public void focusGained (FocusEvent fe)
{
TextField textField = (TextField) fe.getSource();
textField.setBackground(Color.blue);
textField.setForeground(Color.white);
}
public void focusLost(FocusEvent fe)
{
TextField textField = (TextField) fe.getSource();
String text = textField.getText();
textField.setText(text.toUpperCase());
textField.setBackground(Color.white();
textField.setForeground(Color.black);
}
}
class HandleNumbers implements FocusListener
{
public void focusGained (FocusEvent fe)
{
}
public void focusLost (FocusEvent fe)
{
TextField textField = (TextField) fe.getSource();
String text = textField.getText();

DecimalFormat df2 = new DecimalFormat("0.00");
basic = Float.parseFloat (text);
da = basic * 0.6F;
hra = basic * 0.35F;
pf = basic * 0.12F;
netsal = basic + da + hra - pf;

tda.setText(df2.format(da));
thra.setText(df2.format(hra));
tpf.setText(df2.format(pf));
tnetsal.setText(df2.format(netsal));
}
}
class InputBox extends Dialog
{
Label lempid = new Label ("Employee ID: ");
TextField vempid = new TextField(4);
Button search = new Button ("Search");

public InputBox(Frame parent, String title)
{
super(parent, title, true);
this.setLayout(new FlowLayout(FlowLayout.CENTER));
this.add(lempid);
this.add(vempid);
this.add(search);
search.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int tempid = 0;
try{
raf.seek(OL);
tempid = Integer.parseInt(vempid.getText());
while(raf.getFilePointer() < raf.length())
{
pos = raf.getFilePointer();
empid = raf.readInt();
fname = raf.readUTF();
lname = raf.readUTF();
dept = raf.readUTF();
desig = raf.readUTF();
basic = raf.readFloat();
da = raf.readFloat();
hra = raf.readFloat();
pf = raf.readFloat();
netsal = raf.readFloat();

if (empid == tempid)
{
Float vbasic = new Float(basic);
Float vda = new Float(da);
Float vhra = new Float(hra);
Float vpf = new Float(pf);
Float vnetsal = new Float(netsal);
Integer vempid = new Integer (empid);
tempno.setText(vempid.toString());

tfname.setText(fname);
tlname.setText(lname);
cdept.select(dept);
cdesig.select(desig);
tbasic.setText(vbasic.toString());
tda.setText(vda.toString());
thra.setText(vhra.toString());
tpf.setText(vpf.toString());
tnetsal.setText(vnetsal.toString());
break;
}
}
}
catch(IOException ioe)
{
}
finally
{
dispose();
}
}
};
}
}
class DialogBox extends Dialog
{
Button Dismiss = new Button("Dismiss");
Label lblmessage = new Label ("Sample");
public DialogBox(Frame parent, String title, String message)
{
super(parent, title,true);
Panel p1 = new Panel();
Panel p2 = new Panel();

this.setLayout(new BorderLayout());
lblmessage.setText(message);
p1.add(lblmessage);
p2.add(Dismiss);

this.add("North", p1);
this.add("Center", p2);
Dismiss.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dispose();
}
}
};
}
}
}

No comments:

Post a Comment