实际问题是 开发一个客户端服务器应用程序 来计算员工的工资。有些员工是长期员工,有些员工是临时员工。
长期员工有固定的年薪,并且可以享受病假和年假。
临时员工按实际工作时间按小时计酬。
员工的工资是每两周发放一次。
客户端的应用是收集员工的信息,并将其发送到服务器,服务器将计算工资总额,已付税款和净工资。
这些信息将被发回客户端显示。
客户端界面GUI,允许用户选择长期或临时。如果是长期的,那么用户需要输入员工的姓名,身份证号码,年薪和下拉列表,默认为无,但可以选择年假或病假。如果选择其中之一,那么用户将被问及有多少天。
如果是临时工,则输入员工的姓名、身份证号、工资标准和工作时间。计算税金的公式对这两类雇员都是一样的。这些数字是每两周的。
第一个700元是免税的,超过700元的任何收入到1500元的任何收入都要按每美元19分的价格纳税,超过1500元的任何金额都要按每美元38分的价格纳税。申请包括数据验证。这应该在数据被发送到服务器之前完成。所有数字条目应大于0,临时工的工作时间必须是5小时或以上且小于72小时。应该向用户显示一条信息,然后用户应该能够重新输入数据。
我已经尽力了,有没有人或谁能把这个真正的简单化,因为我已经花了几个小时,几个小时,更多的时间,它有问题。
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: Permin:
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);
}
}
}
}
我不太清楚是什么问题,但我看到的一个问题是你的条件。
if (e.getSource() == calculate) {
...
} else if (e.getSource() == clear) { //change it to else if since every click only comes from one source.
...
} else if (e.getSource() == exit)
你是... calculate
, clear
和 exit
变量?如果没有,那么就不应该是这样的。
听起来很奇怪,但我曾经遇到过这样的问题,如果你在.equals()上使用==,那么你的IF语句中就会发生一些奇怪的事情。
尝试。
if (e.getSource().equals(calculate)) {
我被告知,==是非常弱的,只适用于比较int值。在大多数其他情况下,你应该总是使用.equals()
对不起,但我不能写评论回复,因为我对这个网站太陌生了.它看起来像在你的actionPerformed for calculate它检查验证,但仍然附加数据。我想你可能需要将验证代码包裹在数据的追加中.从我所看到的,你检查用户输入是否被输入,并相应地提示用户,如果他们没有输入,然后在所有检查之后,它只是以任何方式追加。