Java Program on Employee Database and Maintenance


Posted Date:     Total Responses: 0    Posted By: Sowmya   Member Level: Diamond   Points/Cash: 50   


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();
}
}
};
}
}
}


Project Feedbacks

Author: Member Level: BronzeRevenue Score: 2 out of 52 out of 5
hi somya
i need projects on java. so please help me. if you dont mind then please help me regularly with my mail id kumarabhinav00@gmail.com. thanks


Author: Member Level: BronzeRevenue Score: 1 out of 5
hi soumya did you have any report file for your your project.


Author: Member Level: BronzeRevenue Score: 2 out of 52 out of 5
I have just downloaded it, and will try if it works, good work , pl help the student with more such projects.


Author: Member Level: BronzeRevenue Score: 2 out of 52 out of 5
Hello Somya,
I need this project for my college. Please contact me @ zoraraja@yahoo.com for the details.


Author: Member Level: BronzeRevenue Score: 5 out of 55 out of 55 out of 55 out of 55 out of 5
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 cus_id;
String cus_name;
String prdt_dtls;
int totl_order;
float totl_price;

RandomAccessFile raf;
char mode;

Label lcus_id = new Label ("Customer# : ");
Label lcus_name = new Label ("Customer Name : ");
Label lprdt_dtls = new Label ("Product Details : ");
Label ltotl_order = new Label ("Total No Products Ordered : ");
Label ltotl_price = new Label ("Total price of the Products Ordered : ");


TextField tcus_id = new TextField(15);
TextField tcus_name = new TextField(20);
Choice cprdt_dtls = new Choice();
TextField ttotl_order = new TextField(15);
TextField ttotl_price = new TextField(15);


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 ("PRODUCT ORDER 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));



cprdt.addItem("Product Id");
cprdt.addItem("Product Name");
cprdt.addItem("Manufacturer");
cprdt.addItem("Product Price")

tcus_id.setBackground(Color.white);
tcus_name.setBackground(Color.white);
ttotl_order.setBackground(Color.white);
ttotl_price.setBackground(Color.whitte);

upper.add(lcus_id);
upper.add(tcus_id);
upper.add(lcus_name);
upper.add(tcus_name);
upper.add(lprdt_dtls);
upper.add(cprdt_dtls);
upper.add(ltotl_order);
upper.add(ttotl_order);
upper.add(ltotl_price);
upper.add(ttotl_price);

tcus_id.setEditable(false);
tcus_name.setEditable(false);
ttotl_order.setEditable(false);
ttotl_price.setEditable(false);


tcus_id.addFocusListener (new HandleTextField());
tcus_name.addFocusListener (new HandleTextField());
ttotl_order.addFocusListener (new HandleTextField());
ttotl_price.addFocusListener (new HandleTextField());

ttotl_price.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");
tcus_id.setEditable (true);
tcus_name.setEditable (true);
ttotl_order.setEditable (true);
ttotl_price.setEditable (true);

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

InputBox inputCus_id = new InputBox(win, "Search Customer");
inputCus_id.setSize(300,100);
input Cus_id.setLocation(200,50);
inputCus_id.show();

tcus_name.setEditable(true);
ttotl_order.setEditable(true);
tcus_name.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(cus_id);
System.out.println(Integer.parseInt(tcus_id.getText()));
if (cus_id ! = Integer.parseInt(tempno.getText()))
{
raftemp.writeInt(cus_id);
raftemp.writeUTF(cus_name);
raftemp.writeUTF(prdt_dtls);
raftemp.writeInt(totl_order);
raftemp.writeFloat(totl_price);
}
}
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 inputCus_id = new InputBox (win, "Search CUstomer");
inputCus_id.setSize(300,100);
inputCus_id.setLocation(200,50);
inputCus_id.show();
}
if (cmd.equals ("Save"))
{
tcus_id.setEditable (False);
tcus_name.setEditable (False);
ttotl_order.setEditable (False);
ttotl_price.setEditable (False);


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

if (flag==0)
{
raf.seek(raf.length());
Cus_id = Integer.parseInt(tcus_id.getText());
cus_name = tcus_name.getText();
prdt_dtls = cprdt_dtls.getSelectedItem();
totl_order = Int.parseFloat(ttotl_order.getText());
totl_price = Float.parseFloat(ttotl_price.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
{
cus_id = raf.readInt();
cus_name = raf.readUTF();
prdt_dtls = raf.readUTF();
totl_order = raf.readInt();
totl_price = raf.readFloat();
}
public void WriteToFile() throws IOException
{
raf.writeInt(cus_id);
raf.writeUTF(cus_name);
raf.writeUTF(prdt_dtls);
raf.writeInt(totl_order);
raf.writeFloat(totl_price);
}
public void ClearDetails()
{
tcus_id.setText("");
tcus_name.setText("");
tprdt_dtls.setText("");
ttotl_order.setText("");
ttotl_price.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");
totl_order= Int.parseInt (text);
totl_price = totl_order * prdt_price ;

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 lcus_id = new Label ("Customer ID: ");
TextField vcus_id = 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(lcus_id);
this.add(vcus_id);
this.add(search);
search.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int tcus_id = 0;
try{
raf.seek(OL);
tcus_id = Integer.parseInt(vcus_id.getText());
while(raf.getFilePointer() < raf.length())
{
pos = raf.getFilePointer();
cus_id = raf.readInt();
cus_name = raf.readUTF();
prdt_dtls = raf.readUTF();
totl_order = raf.readInt();
totl_price = raf.readFloat();

if (cus_id == tcus_id)
{
Integer vtotl_order = new Integer (totl_order);
Float vtotl_price = new Float(totl_price);
Integer vcus_id = new Integer (cus_id);
tcus_id.setText(vcus_id.toString());

tcus_name.setText(cus_name);
cprdt_dtls.select(prdt_dtls);
ttotl_order.setText(vtotl_order.toString());
ttotl_price.setText(vtotl_price.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();
}
}
};
}
}
}

mam this is aditya this program is having 60 errors can you help me out taking out the errors...


Frame.java
Post Feedback
You must Sign In to post a feedback.
Next Project: Telephone Directory Using RMI
Previous Project: Sound Recorder through mic using C language

Return to Project Index

Post New Project


Related Projects

Awards & Gifts
Active Members
TodayLast 7 Daysmore...

Online Members

R Pramod
More...
ISC Technologies, Kochi - India. Copyright © All Rights Reserved.