import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class EmployeeEntry extends JFrame implements ActionListener, KeyListener,ItemListener {
JTextField id, last,first,mi,age,branch,hours,pay,items,rate;
JRadioButton male,female;
JComboBox emptype;
JButton add,delete, back;
JPanel p1,p2,p3,p4,p5,p6,p7;
JPanel p1_1,p1_2, p2_1,p2_2, p3_1,p4_1, p4_2,p5_1,p5_2,p6_1,p6_2,p7_1;
JLabel labelMessage;
EmployeeList emplist=new EmployeeList();
Employee empH=new HourlyEmployee();
Employee empP=new PieceWorker();
Employee [] employee;
String gender;
ObjectInputStream instream;
ObjectOutputStream outstream;
public EmployeeEntry(){}
public void addToFile(EmployeeList list){
try{
FileOutputStream fs = new FileOutputStream("employee.dat");
outstream = new ObjectOutputStream(fs);
//write list to file
outstream.writeObject(list);
}
catch(FileNotFoundException fn){
File file = new File("employee.dat");
}
catch(IOException io){
io.printStackTrace();
}
finally{
try{
outstream.close();
}
catch(IOException io){
io.printStackTrace();
}
}
}
public EmployeeList readFile(){
EmployeeList list = new EmployeeList();
try{
FileInputStream is = new FileInputStream("employee.dat");
instream = new ObjectInputStream(is);
list = ( EmployeeList)instream.readObject();
}
catch(FileNotFoundException fn){}
catch(ClassNotFoundException cn){}
catch(IOException io){}
finally{
try{
instream.close();
}
catch(IOException io){}
}
return list;
}
public static void display( EmployeeList list){
Employee [] empl = list.getEmployee();
for(int i = 0; i< list.getCount();i++ ){
System.out.print( empl[i].getIdno() + " ");
System.out.print( empl[i].getLastname() + " ");
System.out.print( empl[i].getFirstname() + " ");
System.out.print( empl[i].getAge() + " ");
System.out.print( empl[i].getCompanyName() + " ");
System.out.println();
}
}
private MainMenu main;
public void instantiate(){
id=new JTextField(10);
last=new JTextField(15);
first=new JTextField(15);
age=new JTextField(3);
mi=new JTextField(2);
branch=new JTextField(20);
hours=new JTextField(10);
pay=new JTextField(12);
items=new JTextField(10);
rate=new JTextField(13);
male=new JRadioButton();
female=new JRadioButton();
String [] combo={"----------------------------------------","Hourly Employee", "Piece Worker"};
emptype=new JComboBox(combo);
add=new JButton("ADD");
delete=new JButton("DELETE");
back=new JButton("BACK TO MAIN");
labelMessage=new JLabel();
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p4=new JPanel();
p5=new JPanel();
p6=new JPanel();
p7=new JPanel();
Container pane = getContentPane();
pane.setLayout(new GridLayout(8,0));
pane.add(p1);
pane.add(p2);
pane.add(p3);
pane.add(p4);
pane.add(p5);
pane.add(p6);
pane.add(p7);
}//end of the instantiate
public EmployeeEntry()
{
super("Employee Entry");
this.instantiate();
FlowLayout flow = new FlowLayout();
flow.setAlignment(FlowLayout.LEFT);
p1.setLayout(new GridLayout(2,2));
p2.setLayout(new GridLayout(1,2));
p3.setLayout(flow);
p4.setLayout(new GridLayout(1,2));
p5.setLayout(new GridLayout(1,2));
p6.setLayout(new GridLayout(1,2));
p7.setLayout(new FlowLayout());
// group.add(male);
// group.add(female);
p1_1=new JPanel();
p1_2=new JPanel();
p2_1=new JPanel();
p2_2=new JPanel();
p3_1=new JPanel();
p4_1=new JPanel();
p4_2=new JPanel();
p5_1=new JPanel();
p5_2=new JPanel();
p6_1=new JPanel();
p6_2=new JPanel();
p7_1=new JPanel();
p4_1.setBackground(Color.LIGHT_GRAY);
p5_1.setBackground(Color.LIGHT_GRAY);
p6_1.setBackground(Color.LIGHT_GRAY);
p4_2.setBackground(Color.LIGHT_GRAY);
p5_2.setBackground(Color.LIGHT_GRAY);
p6_2.setBackground(Color.LIGHT_GRAY);
FlowLayout low = new FlowLayout();
low.setAlignment(FlowLayout.LEFT);
p1_1.setLayout(low);
p1_2.setLayout(low);
p2_1.setLayout(low);
p2_2.setLayout(new FlowLayout());
p4_1.setLayout(low);
p5_1.setLayout(low);
p5_2.setLayout(new FlowLayout());
p6_1.setLayout(low);
p6_2.setLayout(new FlowLayout());
FlowLayout low2 = new FlowLayout();
low2.setAlignment(FlowLayout.RIGHT);
p4_2.setLayout(new FlowLayout());
p3_1.setLayout(low2);
//p1LABEL.setLayout(low2);
// p1.add(p1LABEL);
p7_1.setLayout(new FlowLayout());
p1_1.add(new JLabel("ID NUMBER "));
p1_1.add(id);
p1_1.add(labelMessage);
p1.add(p1_1);
p1_2.add(new JLabel("LASTNAME"));
p1_2.add(last);
p1_2.add(new JLabel("FIRSTNAME"));
p1_2.add(first);
p1_2.add(new JLabel("MI"));
p1_2.add(mi);
p1.add(p1_2);
p2_1.add(new JLabel("AGE"));
p2_1.add(age);
p2.add(p2_1);
p2_2.add(new JLabel("GENDER"));
p2_2.add(male);
p2_2.add(new JLabel("Male"));
p2_2.add(female);
p2_2.add(new JLabel("Female"));
p2.add(p2_2);
p3_1.add(new JLabel("BRANCH"));
p3_1.add(branch);
p3_1.add(new JLabel("Employee Type"));
p3_1.add(emptype);
p3.add(p3_1);
p4_1.add(new JLabel("Hourly Employee"));
p4.add(p4_1);
p4_2.add(new JLabel("Piece Worker"));
p4.add(p4_2);
p5_1.add(new JLabel("hours worked"));
p5_1.add(hours);
p5.add(p5_1);
p5_2.add(new JLabel("no. of items"));
p5_2.add(items);
p5.add(p5_2);
p6_1.add(new JLabel("pay rate"));
p6_1.add(rate);
p6.add(p6_1);
p6_2.add(new JLabel("rate/item"));
p6_2.add(pay);
p6.add(p6_2);
p7_1.add(add);
p7_1.add(delete);
p7_1.add(back);
p7.add(p7_1);
this.addactions();
p4_1.setVisible(false);
p5_1.setVisible(false);
p6_1.setVisible(false);
p4_2.setVisible(false);
p5_2.setVisible(false);
p6_2.setVisible(false);
setSize(590,420);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocation(300,100);
delete.setEnabled(false);
}// end of cronstructor
public void addactions(){
emptype.addActionListener(this);
back.addActionListener(this);
delete.addActionListener(this);
add.addActionListener(this);
mi.addKeyListener(this);
id.addKeyListener(this);
last.addKeyListener(this);
age.addKeyListener(this);
first.addKeyListener(this);
hours.addKeyListener(this);
pay.addKeyListener(this);
items.addKeyListener(this);
rate.addKeyListener(this);
male.addItemListener(this);
female.addItemListener(this);
} //end of the addactions
public void itemStateChanged(ItemEvent item)
{
Object it=item.getSource();
int select=item.getStateChange();
if(it==male)
{
if(select==ItemEvent.SELECTED)
{
gender="MALE";
female.setEnabled(false);
}
else
{
gender=null;
female.setEnabled(true);
}
}
if(it==female)
{
if(select==ItemEvent.SELECTED)
{
//female.setEnabled(true);
gender="FEMALE";
male.setEnabled(false);
}
else
{
gender=null;
male.setEnabled(true);
}
}
}
// to perform the actions of the actionEvent
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==add&&emptype.getSelectedItem()=="----------------------------------------")
{
JOptionPane.showMessageDialog(null,"You must choose any of the employee type");
}
if(e.getSource()==back)
{
main=new MainMenu();
setVisible(false);
}
if(e.getSource()==delete&&emptype.getSelectedItem()=="Hourly Employee")
{
int del2= JOptionPane.showConfirmDialog(null,"Are you sure you want to delete?");
if(del2==JOptionPane.YES_OPTION)
deleteHourly();
}
if(e.getSource()==delete&&emptype.getSelectedItem()=="Piece Worker")
{
int del2= JOptionPane.showConfirmDialog(null,"Are you sure you want to delete?");
if(del2==JOptionPane.YES_OPTION)
deletePiece();
}
if(e.getSource()==delete&&emptype.getSelectedItem()=="----------------------------------------")
{
JOptionPane.showMessageDialog(null,"You must select one type of employee");
}
if(e.getSource()==add&&emptype.getSelectedItem()=="Piece Worker")
{
emptype.setSelectedItem("----------------------------------------");
int del2= JOptionPane.showConfirmDialog(null,"Are you sure you want to add?");
if(del2==JOptionPane.YES_OPTION)
{
addPiece();
}
}
if(e.getSource()==add&&emptype.getSelectedItem()=="Hourly Employee")
{
emptype.setSelectedItem("----------------------------------------");
int del2= JOptionPane.showConfirmDialog(null,"Are you sure you want to add?");
if(del2==JOptionPane.YES_OPTION)
{
addhourly();
}
}
if(emptype.getSelectedItem()=="Hourly Employee")
{
p4_2.setVisible(false);
p5_2.setVisible(false);
p6_2.setVisible(false);
p4_1.setVisible(true);
p5_1.setVisible(true);
p6_1.setVisible(true);
}
if(emptype.getSelectedItem()=="Piece Worker")
{
p4_1.setVisible(false);
p5_1.setVisible(false);
p6_1.setVisible(false);
p4_2.setVisible(true);
p5_2.setVisible(true);
p6_2.setVisible(true);
}
if(emptype.getSelectedItem()=="----------------------------------------")
{
p4_1.setVisible(false);
p5_1.setVisible(false);
p6_1.setVisible(false);
p4_2.setVisible(false);
p5_2.setVisible(false);
p6_2.setVisible(false);
}
} // end of the actionPerformed method
// to perform the keyListener
public void keyPressed(KeyEvent e){
if(idlenght()>0)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
if(!isfound())
{
labelMessage.setForeground(Color.RED);
labelMessage.setText("ID IS NOT FOUND");
last.setText(null);
first.setText(null);
mi.setText(null);
age.setText(null);
branch.setText(null);
hours.setText(null);
rate.setText(null);
male.setSelected(false);
female.setSelected(false);
delete.setSelected(false);
}
if(isfound())
{
delete.setEnabled(true);
for(int x=0;x<emplist.getcount();x++)
{
if(emplist.employee[x] instanceof HourlyEmployee)
{
if(id.getText().equals(emplist.employee[x].getIdno()))
{
items.setText(null);
pay.setText(null);
labelMessage.setForeground(Color.RED);
labelMessage.setText("Id number found at HourlyEmployee");
last.setText(emplist.employee[x].getLastname());
first.setText(emplist.employee[x].getFirstname());
mi.setText(emplist.employee[x].getmi());
age.setText(""+emplist.employee[x].getAge());
branch.setText(emplist.employee[x].getCompName());
rate.setText(""+emplist.employee[x].getPerHour());
hours.setText(""+emplist.employee[x].getHours());
emptype.setSelectedItem("Hourly Employee");
if(emplist.employee[x].getgender().equals("MALE"))
{
male.setSelected(true);
female.setSelected(false);
}
if(emplist.employee[x].getgender().equals("FEMALE"))
{
male.setSelected(false);
female.setSelected(true);
}
}
}
if(emplist.employee[x] instanceof PieceWorker)
{
if(id.getText().equals(emplist.employee[x].getIdno()))
{
rate.setText(null);
hours.setText(null);
labelMessage.setForeground(Color.RED);
labelMessage.setText("Id number found at PieceWorker");
last.setText(emplist.employee[x].getLastname());
first.setText(emplist.employee[x].getFirstname());
mi.setText(emplist.employee[x].getmi());
age.setText(""+emplist.employee[x].getAge());
branch.setText(emplist.employee[x].getCompName());
items.setText(""+emplist.employee[x].getNoItems());
pay.setText(""+emplist.employee[x].getRate());
emptype.setSelectedItem("Piece Worker");
}
if(emplist.employee[x].getgender().equals("MALE"))
{
female.setSelected(false);
male.setSelected(true);
}
if(emplist.employee[x].getgender().equals("FEMALE"))
{
male.setSelected(false);
female.setSelected(true);
}
}
}
}
}
}
}
public void keyReleased(KeyEvent e){
//test if the inputed mi is more than 1 chracter
if(mi.getText().length()>1)
mi.setText(mi.getText().substring(0,1));
// call of the trapping method
trapping();
emptype.setSelectedItem(true);
}
public void keyTyped(KeyEvent e){
}
//end of the keyListener
// to clear all the textFields
public void deleteHourly()
{
for(int x=0;x<emplist.getcount();x++)
{
if(emplist.employee[x] instanceof HourlyEmployee)
{
if(id.getText().equals(emplist.employee[x].getIdno()))
{
emplist.delete(emplist.employee[x]);
JOptionPane.showMessageDialog(null,"Succesfully Deleted");
id.setText(null);
last.setText(null);
first.setText(null);
mi.setText(null);
age.setText(null);
branch.setText(null);
hours.setText(null);
rate.setText(null);
male.setSelected(false);
female.setSelected(false);
labelMessage.setText("");
}
}
}
}
public void deletePiece()
{
for(int x=0;x<emplist.getcount();x++)
{
if(emplist.employee[x] instanceof PieceWorker)
{
if(id.getText().equals(emplist.employee[x].getIdno()))
{
emplist.delete(emplist.employee[x]);
JOptionPane.showMessageDialog(null,"Succesfully Deleted");
id.setText(null);
last.setText(null);
first.setText(null);
mi.setText(null);
age.setText(null);
branch.setText(null);
pay.setText(null);
items.setText(null);
male.setSelected(false);
female.setSelected(false);
}
}
}
}
// to trap if the inputted data is wrong
public void trapping()
{
Person p=new Person();
//to test if mi has a digit
if(p.isThereADigit(mi.getText()))
{
noDigit();
mi.setText(null);
}
//to test if last has a digit
if(p.isThereADigit(last.getText()))
{
noDigit();
last.setText(null);
}
//to test if firt has a digit
if(p.isThereADigit(first.getText()))
{
noDigit();
first.setText(null);
}
Employee emp=new PieceWorker();
//to test if age has a whitespace
for(int x=0;x<age.getText().length();x++)
{
if(Character.isWhitespace(age.getText().charAt(x)))
{
JOptionPane.showMessageDialog(null,"Please don't type a space Character");
age.setText(null);
}
}
// to test if age is duble
for(int x=0;x<age.getText().length();x++)
{
if(age.getText().charAt(x)=='.')
{
JOptionPane.showMessageDialog(null,"double datatype is not allowed");
age.setText(null);
}
}
//to test if age has a letter
if(emp.isLetterFound(age.getText()))
{
noLetter();
age.setText(null);
}
//to test id
for(int x=0;x<id.getText().length();x++)
{
if(Character.isWhitespace(id.getText().charAt(x)))
{
JOptionPane.showMessageDialog(null,"Please don't type a space Character/letter");
id.setText(null);
}
if(emp.isLetterFound(id.getText()))
{
noLetter();
id.setText(null);
}
if(id.getText().length()>6)
{
JOptionPane.showMessageDialog(null,"ID number must contain atleast 6 characters");
id.setText(null);
}
}
//to test if hours has a whitespace or hours is double
for(int x=0;x<hours.getText().length();x++)
{
if(Character.isWhitespace(hours.getText().charAt(x)))
{
JOptionPane.showMessageDialog(null,"Please dont type a space Character");
hours.setText(null);
}
if(emp.isLetterFound(hours.getText()))
{
noLetter();
hours.setText(null);
}
}
//to test if pay has a letter
if(emp.isLetterFound(pay.getText()))
{
noLetter();
pay.setText(null);
}
// to test if items is a double
for(int x=0;x<items.getText().length();x++)
{
if(items.getText().charAt(x)=='.')
{
JOptionPane.showMessageDialog(null,"double datatype is not allowed");
items.setText(null);
}
if(emp.isLetterFound(items.getText()))
{
noLetter();
items.setText(null);
}
}
//to test if rate has a letter
if(emp.isLetterFound(rate.getText()))
{
noLetter();
rate.setText(null);
}
}
public int idlenght()
{
return id.getText().length();
}
public int lastlength()
{
return last.getText().length();
}
public int firstlength()
{
return first.getText().length();
}
public int agelength()
{
return age.getText().length();
}
public int milength()
{
return mi.getText().length();
}
public int branchlength()
{
return branch.getText().length();
}
public int hourslength()
{
return hours.getText().length();
}
public int itemslength()
{
return items.getText().length();
}
public int paylength()
{
return pay.getText().length();
}
public int ratelength()
{
return rate.getText().length();
}
public void fillUpfields()
{
JOptionPane.showMessageDialog(null,"PLEASE FILL UP ALL THE FIELDS FIRST");
}
public void noDigit()
{
JOptionPane.showMessageDialog(null,"DON'T TYPE A DIGIT");
}
public void noLetter()
{
JOptionPane.showMessageDialog(null,"DON'T TYPE A LETTER");
}
public void addPiece()
{
if(idlenght()>0)
{
if(lastlength()>0)
{
if(firstlength()>0)
{
if(milength()>0)
{
if(agelength()>0)
{
if(branchlength()>0)
{
if(itemslength()>0)
{
if(paylength()>0)
{
if(gender!=null)
{
if(isIdAllDigit())
{
if(!emplist.isFull())
{
try
{
String last1=last.getText();
String first1=first.getText();
String mi1=mi.getText();
int age1=Integer.parseInt(age.getText());
String idno1=id.getText();
String position1=" ";
String companyName1=branch.getText();
int items1=Integer.parseInt(items.getText());
double pay1=Double.parseDouble(pay.getText());
empP=new PieceWorker(last1,first1,age1,mi1,gender,idno1,position1,companyName1,0,0,0,items1,pay1);
emplist.add(empP);
labelMessage.setForeground(Color.RED);
labelMessage.setText("AN EMPLOYEE IS ADDED");
empP.display();
emplist.countPE();
JOptionPane.showMessageDialog(null,"SUCCESFULLY ADDED");
id.setText(null);
last.setText(null);
first.setText(null);
mi.setText(null);
age.setText(null);
branch.setText(null);
pay.setText(null);
items.setText(null);
male.setSelected(false);
female.setSelected(false);
}
catch(Exception excp)
{
JOptionPane.showMessageDialog(null,"no. of items must be an integer type,rate/item must be double type and age must be an integer");
}
}
else
{
JOptionPane.showMessageDialog(null,"EmployeeList is Full,\nMaximum number of Employee's are only 5");
}
}
else
{
JOptionPane.showMessageDialog(null,"ID NO. MUST BE DIGITS");
}
}
else
{
JOptionPane.showMessageDialog(null,"Must choose a gender");
}
}
else
{
fillUpfields();
}
}
else
{
fillUpfields();
}
}
else
{
fillUpfields();
}
}
else
{
fillUpfields();
}
}
else
{
fillUpfields();
}
}
else
{
fillUpfields();
}
}
else
{
fillUpfields();
}
}
else
{
fillUpfields();
}
}
public void addhourly()
{
if(idlenght()>0)
{
if(lastlength()>0)
{
if(firstlength()>0)
{
if(milength()>0)
{
if(agelength()>0)
{
if(branchlength()>0)
{
if(hourslength()>0)
{
if(ratelength()>0)
{
if(gender!=null)
{
if(isIdAllDigit())
{
if(!emplist.isFull())
{
try
{
// HourlyEmployee empH=new HourlyEmployee();
String last1=last.getText();
String first1=first.getText();
String mi1=mi.getText();
int age1=Integer.parseInt(age.getText());
String idno1=id.getText();
String position1=" ";
String companyName1=branch.getText();
int hours1=Integer.parseInt(hours.getText());
double rate1=Double.parseDouble(rate.getText());
int over=0;
empH=new HourlyEmployee(last1,first1,age1,mi1,gender,idno1,position1,companyName1,hours1,rate1,over,0,0);
emplist.add(empH);
labelMessage.setForeground(Color.RED);
labelMessage.setText("An Employee is added");
empH.display();
JOptionPane.showMessageDialog(null,"Succesfully added");
id.setText(null);
last.setText(null);
first.setText(null);
mi.setText(null);
age.setText(null);
branch.setText(null);
hours.setText(null);
rate.setText(null);
male.setSelected(false);
female.setSelected(false);
}
catch(Exception excp)
{
JOptionPane.showMessageDialog(null,"HoursWorked must be an integer type,pay rate must be a double type and age must be an integer");
}
}
else
{
JOptionPane.showMessageDialog(null,"EmployeeList is Full,\nMaximum number of Employee's are only 5");
}
}
else
{
JOptionPane.showMessageDialog(null,"Id must be all digits");
}
}
else
{
JOptionPane.showMessageDialog(null,"Must choose a gender");
}
}
else
{
fillUpfields();
}
}
else
{
fillUpfields();
}
}
else
{
fillUpfields();
}
}
else
{
fillUpfields();
}
}
else
{
fillUpfields();
}
}
else
{
fillUpfields();
}
}
else
{
fillUpfields();
}
}
else
{
fillUpfields();
}
}
public boolean isfound()
{
boolean result=false;
for(int x=0;x<emplist.getcount();x++)
{
if(emplist.employee[x] instanceof HourlyEmployee)
{
if(id.getText().equals(emplist.employee[x].getIdno()))
{
result=true;
break;
}
else
{
result=false;
}
}
if(emplist.employee[x] instanceof PieceWorker)
{
if(id.getText().equals(emplist.employee[x].getIdno()))
{
result=true;
break;
}
else
{
result=false;
}
}
}
return result;
}
public boolean isIdAllDigit()
{
boolean result=false;
for(int x=0;x<idlenght();x++)
{
if(Character.isDigit(id.getText().charAt(x)))
{
result=true;
}
else
{
result=false;
break;
}
}
return result;
}
//main
public static void main(String []args){
// new EmployeeEntry();
EmployeeEntry entry = new EmployeeEntry();
Employee s1, s2,s3;
EmployeeList emp = new EmployeeList();
s1 = new Employee( last);
s2 = new Employee( first);
s3 = new Employee( id);
entry.add(s1);
entry.add(s2);
entry.add(s3);
emp.addToFile(entry);
//read data from file
EmployeeList emp = entry.readFile();
display(emp);
}
}
//end of main
// end of the class
Can some one help me how to figured it out...