the actual question is: Develop a client/server application that calculates the pay for employees. Some employees are permanent and others are casual.
Permanent employees have a set annual salary and are eligible for sick leave and annual leave.
Casual employees are paid an hourly rate for the actual hours they worked.
Employees are paid fortnightly.
Client side of the application is to gather information about the employee and send it to the server that will calculate the gross pay, tax paid and net pay.
This information will be sent back to the client to be displayed.
Client side interface GUI that allows the user to choose between permanent or casual. If permanent then the user is asked to enter the employee’s name, id number, annual salary and have drop down list for leave taken that defaults to none but annual or sick can be chosen. If either is chosen then the user will be asked how many days.
If casual then the employee’s name, id number, pay rate and hours worked are entered. The formula to calculate tax is the same for both categories of employees. These figures are per fortnight.
First $700 is tax free anything earned over $700 up to $1500 is taxed at 19c per dollar and any amount over $1500 is taxed 38c per dollar. Application is to include data validation. This should be done before the data is sent to the server. All number entries should be greater than 0 and hours worked for casuals must be five or more and less than 72. A message should be shown to the user and the user should be then able to re-enter the data.
I did the best i could, does anyone or can anyone make this real simple as I have spent hours and hours and hours and more hours and it has issues:
SERVER:
package com.test;
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class PizzaPayServer extends JFrame
{
/**
*
*/
private static final long serialVersionUID = 1L;
//Text area for displaying contents
private JTextArea jta = new JTextArea();
public PizzaPayServer()
{
//Place text area on the frame
getContentPane().setLayout(new BorderLayout());
getContentPane().add(new JScrollPane(jta), BorderLayout.CENTER);
setTitle("PizzaPayServer");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); // It is necessary to show the frame here!
}
public static void main(String[] args)
{
ServerSocket serverSocket;
Socket socket;
DataInputStream inputFromClient;
DataOutputStream outputToClient;
String empname;
int empid;
double hours;
double payrate;
double salary;
double grossPay;
double netPay;
double tax;
PizzaPayServer theServer = new PizzaPayServer();
try
{
// Create a server socket
serverSocket = new ServerSocket(8010);
theServer.jta.append("Server started at " + new Date() + '\n');
//Listen for a connection request
socket = serverSocket.accept();
//Create data input and output streams
inputFromClient = new DataInputStream(socket.getInputStream());
outputToClient = new DataOutputStream(socket.getOutputStream());
while (true)
{
hours=0;
payrate=0;
//Receive employee name from client
empname = inputFromClient.readUTF();
//Receive employee id from client
empid = inputFromClient.readInt();
//Receive payrate from client
//Receive hours from client
tax=0;
netPay=0;
salary=0;
String[] parts = empname.split(",");
String part1 = parts[0];
String part2 = parts[1];
if (part2.equals("c")){
payrate = inputFromClient.readDouble();
hours = inputFromClient.readDouble();
//Compute pay
grossPay = payrate * hours;
if(grossPay<=700)
tax=0;
else
if(grossPay<=1500)
tax=(grossPay-700) *0.19;
else
tax=((grossPay-1500) *0.38) + (800 *0.19);
netPay= grossPay-tax;
//Send pay back to client
outputToClient.writeDouble(netPay);
outputToClient.writeUTF(empname);
outputToClient.writeInt(empid);
theServer.jta.append("Employee Name recieved from client: " + part1 + '\n');
theServer.jta.append("Employee ID recieved from client: " + empid + '\n');
theServer.jta.append("Payrate recieved from client: " + payrate + '\n');
theServer.jta.append("Hours recieved from client: " + hours + '\n');
theServer.jta.append("Pay Calculated and Sent: " + netPay + '\n');
}
//perminPayClient Method
if (part2.equals("p")){
payrate = inputFromClient.readDouble();
//Compute pay
salary=payrate;
if(salary<=700)
tax=0;
else
if(salary<=1500)
tax=(salary-700) *0.19;
else
tax=((salary-1500) *0.38) + (800 *0.19);
netPay= salary/26 -tax;
//Send pay back to client
outputToClient.writeDouble(salary);
outputToClient.writeUTF(empname);
outputToClient.writeInt(empid);
theServer.jta.append("Employee Name recieved from client: " + part1 + '\n');
theServer.jta.append("Employee ID recieved from client: " + empid + '\n');
theServer.jta.append("Salary recieved from client: " + salary + '\n');
theServer.jta.append("Pay Calculated and Sent: " + netPay + '\n');
}
}
}
catch(IOException ex)
{
System.err.println("Catch While");
}
System.exit(0);
}}
MAIN:
package com.test;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.ImageIcon;
public class Main extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
setTitle("Casey Pizza Pay System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 276, 406);
contentPane = new JPanel();
contentPane.setBackground(new Color(255, 255, 153));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JButton btnCasEmp = new JButton("Casual Employee");
btnCasEmp.setFont(new Font("Tahoma", Font.BOLD, 12));
btnCasEmp.setBounds(34, 207, 197, 32);
btnCasEmp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
PizzaPayClient nw = new PizzaPayClient();
nw.setVisible(true); //Opens new screen place order
}
});
contentPane.setLayout(null);
contentPane.add(btnCasEmp);
JButton btnPerEmp = new JButton("Permament Employee");
btnPerEmp.setFont(new Font("Tahoma", Font.BOLD, 12));
btnPerEmp.setBounds(34, 262, 197, 32);
btnPerEmp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
PerminPayClient nw1 = new PerminPayClient();
nw1.setVisible(true); //Opens new screen place order
}
});
contentPane.add(btnPerEmp);
JLabel lblCaseysPizza = new JLabel(" Casey's Pizza");
lblCaseysPizza.setBackground(new Color(255, 255, 153));
lblCaseysPizza.setForeground(Color.RED);
lblCaseysPizza.setFont(new Font("Algerian", Font.BOLD, 24));
lblCaseysPizza.setBounds(24, 11, 207, 32);
contentPane.add(lblCaseysPizza);
JLabel lblNewLabel = new JLabel("New label");
lblNewLabel.setIcon(new ImageIcon("C:\\Users\\Little Beast\\workspace\\PizzaPaySystem\\pizzaplace2.jpg"));
lblNewLabel.setBounds(34, 66, 197, 105);
contentPane.add(lblNewLabel);
JLabel lblGreet = new JLabel(" Pay System v1.0");
lblGreet.setFont(new Font("Algerian", Font.PLAIN, 12));
lblGreet.setBounds(69, 41, 116, 14);
contentPane.add(lblGreet);
JLabel lblSelect = new JLabel("Please Select...");
lblSelect.setForeground(Color.RED);
lblSelect.setFont(new Font("Algerian", Font.PLAIN, 12));
lblSelect.setBounds(82, 182, 103, 14);
contentPane.add(lblSelect);
JButton btnHelp = new JButton("");
btnHelp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
HelpMenu nw3 = new HelpMenu();
nw3.setVisible(true); //Opens new screen place order
}
});
btnHelp.setBackground(Color.PINK);
btnHelp.setIcon(new ImageIcon("C:\\Users\\Little Beast\\workspace\\PizzaPaySystem\\help.png"));
btnHelp.setBounds(34, 325, 197, 32);
contentPane.add(btnHelp);
}
}
Casual:
package com.test;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PizzaPayClient extends JFrame implements ActionListener
{
/**
*
*/
//Variables
private static final long serialVersionUID = 1L;
//Text fields for receiving employee name, employee id, payrate, hours
private JTextField jtfEname = new JTextField();
private JTextField jtfEid = new JTextField();
private JTextField jtfPayRate = new JTextField();
//Buttons for onClick Listeners
private JButton calculate = new JButton("Calculate Pay");
private JButton clear = new JButton("Clear");
private JButton exit = new JButton("Exit");
// IO Streams
private DataOutputStream outputToServer;
private DataInputStream inputFromServer;
private Socket sock;
private LayoutManager FlowLayout;
private final JTextField jtfHours = new JTextField();
private final JTextArea jta = new JTextArea();
private final JLabel lblNewLabel = new JLabel("New label");
public PizzaPayClient()
{
// Panel p to hold the label and text field
jtfHours.setHorizontalAlignment(SwingConstants.LEFT);
jtfHours.setBounds(134, 84, 140, 20);
jtfHours.setColumns(10);
JPanel p = new JPanel();
p.setBackground(new Color(255, 255, 102));
p.setBounds(0, 0, 501, 453);
p.setLayout(FlowLayout);
p.setLayout(null);
p.setLayout(null);
p.setLayout(null);
p.setLayout(null);
p.setLayout(null);
JLabel lblEmpName = new JLabel("Employee Name:");
lblEmpName.setFont(new Font("Tahoma", Font.BOLD, 12));
lblEmpName.setBounds(9, 9, 103, 14);
p.add(lblEmpName);
exit.setFont(new Font("Tahoma", Font.BOLD, 12));
exit.setBounds(376, 126, 108, 23);
p.add(exit);
exit.addActionListener(this);
JLabel lblEmpId = new JLabel("Employee ID:");
lblEmpId.setFont(new Font("Tahoma", Font.BOLD, 12));
lblEmpId.setBounds(10, 37, 102, 14);
p.add(lblEmpId);
jtfEid.setBounds(134, 34, 140, 20);
p.add(jtfEid);
JLabel lblPayRate = new JLabel("Enter PayRate:");
lblPayRate.setFont(new Font("Tahoma", Font.BOLD, 12));
lblPayRate.setBounds(9, 62, 103, 14);
p.add(lblPayRate);
jtfPayRate.setBounds(133, 59, 141, 20);
p.add(jtfPayRate);
jtfPayRate.setHorizontalAlignment(SwingConstants.LEFT);
jtfPayRate.addActionListener(this);
jtfEname.setBounds(134, 6, 140, 20);
p.add(jtfEname);
jtfEname.setHorizontalAlignment(SwingConstants.LEFT);
// Register Listener/s
jtfEname.addActionListener(this);
getContentPane().setLayout(null);
JLabel lblHours = new JLabel("Enter Hours:");
lblHours.setFont(new Font("Tahoma", Font.BOLD, 12));
lblHours.setBounds(9, 87, 103, 14);
p.add(lblHours);
calculate.setFont(new Font("Tahoma", Font.BOLD, 12));
calculate.setBounds(73, 126, 180, 23);
p.add(calculate);
clear.setFont(new Font("Tahoma", Font.BOLD, 12));
clear.setBounds(263, 126, 103, 23);
p.add(clear);
jtfEid.setHorizontalAlignment(SwingConstants.LEFT);
getContentPane().add(p);
p.add(jtfHours);
jta.setLineWrap(true);
jta.setRows(20);
jta.setBounds(10, 160, 480, 282);
p.add(jta);
lblNewLabel.setIcon(new ImageIcon("C:\\Users\\Little Beast\\workspace\\PizzaPaySystem\\pizzaplace2.jpg"));
lblNewLabel.setBounds(298, 9, 192, 106);
p.add(lblNewLabel);
jtfEid.addActionListener(this);
calculate.addActionListener(this);
clear.addActionListener(this);
setTitle("Casual Pay Entry");
setSize(517, 491);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); // It is necessary to show the frame here!
try
{
//Create a socket to connect to the server
sock = new Socket("localhost", 8010);
//Create an input stream to receive data from the server
inputFromServer = new DataInputStream(sock.getInputStream());
//Create an output stream to send data to the server
outputToServer = new DataOutputStream(sock.getOutputStream());
}
catch (IOException ex)
{
jta.append(ex.toString() + '\n');
}
}
/**
* @param args
*/
public static void main(String[] args)
{
new PizzaPayClient();
}
public void actionPerformed(ActionEvent e) {
//String actionCommmand = e.getActionCommand();
if (e.getSource().equals(calculate)) {
try {
//Get the employee name, employee id, payrate, hours from the text fields
String empname = jtfEname.getText();
//If no name is entered display pop up dialog box.
if(empname.length()==0)
JOptionPane.showMessageDialog(null, "Please Enter Your Name");
int empid = 0;
try {
empid = Integer.parseInt(jtfEid.getText().trim());
//If no employee Id is entered display pop up dialog box
} catch(NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "Please Enter Your Id");
}
double payrate = 0;
try {
payrate = Double.parseDouble(jtfPayRate.getText().trim());
//If no payrate is entered display pop up dialog box
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "Please Enter Your Pay Rate");
}
double hours = 0;
try {
hours = Double.parseDouble(jtfHours.getText().trim());
//If hours are less than 5 or over 72 display pop up dialog box
if(hours <5)
JOptionPane.showMessageDialog(null, "Must be 5 hours or greater");
if(hours >72)
JOptionPane.showMessageDialog(null, "Hours must be less than 72");
//If no hours are entered display pop up dialog box
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "Please Enter Your Hours");
}
String oldName=empname;
//send the employee name, employee id, payrate, hours to the server
outputToServer.writeUTF(empname+",c");
outputToServer.writeInt(empid);
outputToServer.writeDouble(hours);
outputToServer.writeDouble(payrate);
outputToServer.flush();
//Get pay from the server
double pay = inputFromServer.readDouble();
empname = inputFromServer.readUTF();
//Display to the text area
jta.append("Name " + oldName + '\n');
jta.append("ID " + empid + '\n');
jta.append("PayRate is " + payrate + '\n');
jta.append("Hours is " + hours + '\n');
jta.append("Total Pay "
+ pay + '\n');
jtfEname.setText("");
jtfEid.setText("");
jtfPayRate.setText("");
jtfHours.setText("");
}
catch (IOException ex) {
System.err.println(ex);
}
}
//Clear the fields - reset
else if (e.getSource() == clear)
{
jta.setText("");
}
else if (e.getSource() == exit)
{
try
{
System.exit(0);
sock.close();
}
catch (Exception ex)
{
System.out.println("Client: exception " + ex);
}
}
}
}
Permin:
package com.test;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PerminPayClient extends JFrame implements ActionListener
{
/**
*
*/
//Variables
private static final long serialVersionUID = 1L;
//Text fields for receiving employee name, employee id, payrate, hours
private JTextField jtfEname = new JTextField();
private JTextField jtfEid = new JTextField();
private JTextField jtfPayRate = new JTextField();
//Buttons for onClick Listeners
private JButton calculate = new JButton("Calculate Pay");
private JButton clear = new JButton("Clear");
private JButton exit = new JButton("Exit");
// IO Streams
private DataOutputStream outputToServer;
private DataInputStream inputFromServer;
private Socket sock;
private LayoutManager FlowLayout;
private final JTextArea jta = new JTextArea();
private final JLabel lblNewLabel = new JLabel("New label");
public PerminPayClient()
{
JPanel p = new JPanel();
p.setBackground(new Color(255, 255, 102));
p.setBounds(0, 0, 501, 453);
p.setLayout(FlowLayout);
p.setLayout(null);
p.setLayout(null);
p.setLayout(null);
p.setLayout(null);
p.setLayout(null);
JLabel lblEmpName = new JLabel("Employee Name:");
lblEmpName.setFont(new Font("Tahoma", Font.BOLD, 12));
lblEmpName.setBounds(9, 9, 103, 14);
p.add(lblEmpName);
exit.setFont(new Font("Tahoma", Font.BOLD, 12));
exit.setBounds(376, 126, 108, 23);
p.add(exit);
exit.addActionListener(this);
JLabel lblEmpId = new JLabel("Employee ID:");
lblEmpId.setFont(new Font("Tahoma", Font.BOLD, 12));
lblEmpId.setBounds(10, 37, 102, 14);
p.add(lblEmpId);
jtfEid.setBounds(134, 34, 140, 20);
p.add(jtfEid);
JLabel lblPayRate = new JLabel("Enter Salary:");
lblPayRate.setFont(new Font("Tahoma", Font.BOLD, 12));
lblPayRate.setBounds(9, 62, 103, 14);
p.add(lblPayRate);
jtfPayRate.setBounds(133, 59, 141, 20);
p.add(jtfPayRate);
jtfPayRate.setHorizontalAlignment(SwingConstants.LEFT);
jtfPayRate.addActionListener(this);
jtfEname.setBounds(134, 6, 140, 20);
p.add(jtfEname);
jtfEname.setHorizontalAlignment(SwingConstants.LEFT);
// Register Listener/s
jtfEname.addActionListener(this);
getContentPane().setLayout(null);
calculate.setFont(new Font("Tahoma", Font.BOLD, 12));
calculate.setBounds(73, 126, 180, 23);
p.add(calculate);
clear.setFont(new Font("Tahoma", Font.BOLD, 12));
clear.setBounds(263, 126, 103, 23);
p.add(clear);
jtfEid.setHorizontalAlignment(SwingConstants.LEFT);
getContentPane().add(p);
jta.setLineWrap(true);
jta.setRows(20);
jta.setBounds(10, 160, 480, 282);
p.add(jta);
lblNewLabel.setIcon(new ImageIcon("C:\\Users\\Little Beast\\workspace\\PizzaPaySystem\\pizzaplace2.jpg"));
lblNewLabel.setBounds(298, 9, 192, 106);
p.add(lblNewLabel);
jtfEid.addActionListener(this);
calculate.addActionListener(this);
clear.addActionListener(this);
setTitle("Casual Pay Entry");
setSize(517, 491);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); // It is necessary to show the frame here!
try
{
//Create a socket to connect to the server
sock = new Socket("localhost", 8010);
//Create an input stream to receive data from the server
inputFromServer = new DataInputStream(sock.getInputStream());
//Create an output stream to send data to the server
outputToServer = new DataOutputStream(sock.getOutputStream());
}
catch (IOException ex)
{
jta.append(ex.toString() + '\n');
}
}
/**
* @param args
*/
public static void main(String[] args)
{
new PerminPayClient();
}
public void actionPerformed(ActionEvent e) {
//String actionCommmand = e.getActionCommand();
if (e.getSource().equals(calculate)) {
try {
//Get the Employee Name, Employee Id, Pay Rate(Salary) from the text fields
String empname = jtfEname.getText();
//If no Employee Name is entered display pop up dialog box.
if(empname.length()==0)
JOptionPane.showMessageDialog(null, "Please Enter Your Name");
int empid = 0;
try {
empid = Integer.parseInt(jtfEid.getText().trim());
//If no Employee Id is entered display pop up dialog box
} catch(NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "Please Enter Your Id");
}
double payrate = 0;
try {
payrate = Double.parseDouble(jtfPayRate.getText().trim());
//If no pay rate is entered display pop up dialog box
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "Please Enter Your Annual Salary");
}
//send the Employee Name, Employee Id, Pay Rate to the server
String oldName=empname;
outputToServer.writeUTF(empname+",p");
outputToServer.writeInt(empid);
outputToServer.writeDouble(payrate);
outputToServer.flush();
//Get pay from the server
double pay = inputFromServer.readDouble();
empname = inputFromServer.readUTF();
//Display to the text area
jta.append("Name " + oldName + '\n');
jta.append("ID " + empid + '\n');
jta.append("Salary is " + payrate + '\n');
jta.append("Total Pay "
+ pay + '\n');
}
catch (IOException ex) {
System.err.println(ex);
}
}
//Clear the fields - reset
else if (e.getSource() == clear)
{
jtfEname.setText("");
jtfEid.setText("");
jtfPayRate.setText("");
jta.setText("");
}
//Button exit function to exit program
else if (e.getSource() == exit)
{
try
{
System.exit(0);
sock.close();
}
catch (Exception ex)
{
System.out.println("Client: exception " + ex);
}
}
}
}