Problem Statement:
In a company the salary structure is based upon the category and basic pay. The employee table has the following fields:
1. Employee Id 2. Name 3. Employee Category 4. Basic Pay
The salary consist of the components shown below:
Salary Component Category1 Category2 Category3
Dearness Allowance 87% 65% 45%
House Rent Allowance 8% 9% 10%
Other Allowance 0% 0% 2%
Vehicle Allowance Rs 500 Rs 1000 10%
Write a program to query the database for total salary of an employee. Assume the database is available (create database in Oracle thru SQLplus) in a server machine. The input format is has the following format from a client.
Input and Output Requirements:
The client accepts the employee id from the terminal through the applet. This is passed to server which calculates the total salary based on the category. The salary is send back to the client and is displayed on the applet.
Algorithm:
Server Side Connection procedure:
Create a server socket and begin listening
ServerSocket serSocket = new ServerSocket(port)
which creates a server socket and bind it to the port
Call accept() method to get new connections
Socket client = serSocket.accept()
this method returns a connected client socket used to converse with the caller.
Create input and output streams for the socket
DataInputStream inStream= new DataInputStream(client.getInputStream());
DataOutputStream outStream = new
DataOutputStream(client.getOutputStream());
Conduct the conversation based on agreed protocol
Close client streams and socket
Go back to step 2 or continue step7
Close the server socket
Client Side Connection procedure:
Create a client socket
Acquire input and output streams for the socket
Conduct the conversation based on agreed protocol
Close client streams and socket
Procedure CalculateSalary(Employeeid)
getdata(Employeeid)
if category = 1 then
da = basic pay * 0.87
hra = basic pay * 0.8
oa = 0
va = 500
elseif category = 2 then
da = basic pay * 0.65
hra = basic pay * 0.9
oa = 0
va = 1000
elseif category = 3 then
da = basic pay * 0.45
hra = basic pay * 0.10
oa = basic pay * 0.02
va = basic pay * 0.10
salary = da + hra + oa + va
return salary
End CalculateSalary
Program:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.Statement; import java.awt.*; import java.awt.event.*; public class emp_sal extends Frame implements ActionListener { Frame fr; TextField id,sal; Button B1; TextArea TA; Label L1,L2,L3; public void setup() { fr = new Frame("Employee Details"); fr.setLayout(new GridLayout(7,7)); fr.setSize(300, 300); id = new TextField(7); sal = new TextField(10); TA = new TextArea("",10,30); B1 = new Button("Find Total Salary"); L1 = new Label("Employee ID : "); L2 = new Label("Details : "); L3 = new Label("Salary : "); fr.add(L1); fr.add(id); fr.add(B1); fr.add(L2); fr.add(TA); fr.add(L3); fr.add(sal); B1.addActionListener(this); fr.show(); } public void actionPerformed(ActionEvent ae) { String ID = id.getText(); TA.setText(""); if(ae.getActionCommand().equals("Find Total Salary")) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection ("jdbc:odbc:myds","system","manager"); Statement stmt = con.createStatement(); String query = "Select * from employee WHERE emp_id ='" + ID + "'"; ResultSet rs = stmt.executeQuery(query); ResultSetMetaData rmdt = rs.getMetaData(); int cols = rmdt.getColumnCount(); if(rs.next()) { for(int i = 1 ; i <= cols; i++) TA.setText(TA.getText() + rmdt.getColumnName(i)+ " : " + rs.getString(i)+ " \n "); } query = "Select * from employee WHERE emp_id ='" + ID + "'"; rs = stmt.executeQuery(query); rmdt = rs.getMetaData(); cols = rmdt.getColumnCount(); if(rs.next()) { String cat = rs.getString("emp_cat"); String b_pay = rs.getString("basic_pay"); if(cat.equalsIgnoreCase("1")) { double tot_sal = (.87 * Double.parseDouble(b_pay)) + (.8 * Double.parseDouble(b_pay)) + 0 + 500; sal.setText(Double.toString(tot_sal)); } if(cat.equalsIgnoreCase("2")) { double tot_sal = (.65 * Double.parseDouble(b_pay)) + (.9 * Double.parseDouble(b_pay)) + 0 + 1000; sal.setText(Double.toString(tot_sal)); } if(cat.equalsIgnoreCase("3")) { double tot_sal = (.45* Double.parseDouble(b_pay)) + (.10* Double.parseDouble(b_pay)) + 0 + (.02 * Double.parseDouble(b_pay)); sal.setText(Double.toString(tot_sal)); } } } catch(Exception e) { System.out.println(e.getMessage()); } } } public void closeApp() { fr.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } }); } public static void main(String[] args) { emp_sal emp = new emp_sal(); emp.setup(); emp.closeApp(); } } |
Output: