📄 ssalary.java
字号:
package file1;
/*
* 功能描述:员工的个人工资查询及结算入口
* @Author:黄顺武
* Version:1.3
*/
import java.awt.*;
import sun.jdbc.rowset.*;
import java.util.Date;
import javax.swing.*;
import java.sql.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class SSalary extends JPanel implements ActionListener, ItemListener {
private JLabel head = new JLabel("员工职称:");
private JLabel name = new JLabel("员工名字:");
private JLabel identityNo = new JLabel("身份证号:");
private JLabel month = new JLabel("查询月份:");
private String[] months = { "一月份", "二月份", "三月份", "四月份", "五月份", "六月份",
"七月份", "八月份", "九月份", "十月份", "十一月份", "十二月份" };
private JLabel result = new JLabel("本月工资:");
private JLabel punishment = new JLabel("本月罚款金额:");
private JLabel moneyPayed = new JLabel("已支付金额:");
private JLabel stillNotPay = new JLabel("未支付金额:");
private JLabel payThisTime = new JLabel("本次支付金额:");
private JTextField stillNotPayTF = new JTextField(10);
private JButton query = new JButton("查询");
private JButton pay = new JButton("本月工资结算");
private JComboBox headBox = new JComboBox();
private JComboBox nameBox = new JComboBox();
private JTextField identityNoTF = new JTextField(10);
private JComboBox monthBox = new JComboBox(months);
private JTextField resultTF = new JTextField(10);
private JTextField punishmentTF = new JTextField(10);
private JTextField moneyPayedTF = new JTextField(10);
private JTextField thisTimeTF = new JTextField(10);
private JPanel p1 = new JPanel();
private GetDate getD = null;
private Formatter format = null;
private IdentityDate identityDate=new IdentityDate();//实例化对两个日期进行相关性判断的类对象
private String identity[] = null;
private String[] eIDS = null;
private int year = 0;
private float totalPunishment = 0;// 存储查询月份的罚款金额
public SSalary() {
String returnValue = getHeads();
if (returnValue == null) {
return;
}
getD = new GetDate();
format = new Formatter();
Date d = new Date();
String index = getD.getDate(format.valueConverted(d));
if (index == null) {
return;
}
year = getD.getYear();
monthBox.setSelectedIndex(-1);
punishmentTF.setEditable(false);
moneyPayedTF.setEditable(false);
resultTF.setEditable(false);
identityNoTF.setEditable(false);
stillNotPayTF.setEditable(false);
p1.setLayout(new GridLayout(10, 2, 5, 20));
p1.add(head);
p1.add(headBox);
p1.add(name);
p1.add(nameBox);
p1.add(identityNo);
p1.add(identityNoTF);
p1.add(month);
p1.add(monthBox);
p1.add(result);
p1.add(resultTF);
p1.add(punishment);
p1.add(punishmentTF);
p1.add(moneyPayed);
p1.add(moneyPayedTF);
p1.add(stillNotPay);
p1.add(stillNotPayTF);
p1.add(payThisTime);
p1.add(thisTimeTF);
p1.add(query);
p1.add(pay);
this.add(p1);
query.setBorder(null);
pay.setBorder(null);
pay.setBackground(Color.LIGHT_GRAY);
query.setBackground(Color.LIGHT_GRAY);
query.addActionListener(this);
pay.addActionListener(this);
headBox.addItemListener(this);
nameBox.addItemListener(this);
}
private String getHeads() {
try {
DBConnection con = new DBConnection();
String queryStr = "select hName from Head";
CachedRowSet crs = con.getResultSet(queryStr);
int count = 0;
while (crs.next()) {
count++;
}
if (count == 0) {
JOptionPane.showMessageDialog(null, "请您先添加员工职称!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
crs.beforeFirst();
while (crs.next()) {
headBox.addItem(crs.getString(1));
}
headBox.setSelectedIndex(-1);
} catch (SQLException sqle) {
sqle.printStackTrace();
return null;
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
return null;
}
return "success";
}
public void itemStateChanged(ItemEvent ie) {
if (ie.getSource() == headBox) {
if (ie.getStateChange() == ItemEvent.SELECTED) {
resultTF.setText("");
String headSelected = (String) headBox.getSelectedItem();
if (headSelected == null) {
return;
}
String queryStr = "select ID,name,IdentityNo from Employee where head='"
+ headSelected + "'";
CachedRowSet crs = null;
int count = 0;
try {
DBConnection con = new DBConnection();
crs = con.getResultSet(queryStr);
while (crs.next()) {
count++;
}
if (count == 0) {
JOptionPane.showMessageDialog(null, "没有该职称的员工!", "",
JOptionPane.INFORMATION_MESSAGE);
if (nameBox.getItemCount() > 0) {
nameBox.removeAllItems();
}
if (!identityNoTF.getText().trim().equals("")) {
identityNoTF.setText("");
}
return;
}
eIDS = new String[count];
identity = new String[count];
if (nameBox.getItemCount() > 0) {
nameBox.removeAllItems();
}
crs.beforeFirst();
count = 0;
while (crs.next()) {
eIDS[count] = String.valueOf(crs.getInt(1));
nameBox.addItem(crs.getString(2).trim());
identity[count] = crs.getString(3).trim();
count++;
}
nameBox.setSelectedIndex(-1);
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
}
if (ie.getSource() == nameBox) {
if (ie.getStateChange() == ItemEvent.SELECTED) {
int index = nameBox.getSelectedIndex();
if (index == -1) {
return;
}
identityNoTF.setText(identity[index]);
}
}
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == query) {
if (!pay.isEnabled()) {
pay.setEnabled(true);
}
String head = (String) headBox.getSelectedItem();
String name = (String) nameBox.getSelectedItem();
String identity = identityNoTF.getText().trim();
String monthTo = year + "年" + monthBox.getSelectedItem();
if (head == null || name == null || monthTo == null) {
JOptionPane.showMessageDialog(null, "员工职称,名字,月份都不能为空!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
if (identity.equals("")) {
JOptionPane.showMessageDialog(null, "员工的身份证号不能为空!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String queryStr = "select* from Head where hName='" + head + "'";
CachedRowSet crs = null;
int count = 0;
try {
DBConnection con = new DBConnection();
crs = con.getResultSet(queryStr);
while (crs.next()) {
count++;
}
if (count == 0) {
JOptionPane.showMessageDialog(null, "数据库中没有该职称!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
crs.beforeFirst();
float total = 0;
while (crs.next()) {
crs.getString(1);
float salary = Float.valueOf(crs.getFloat(2));
String type = crs.getString(3).trim();
float onceMoney = Float.valueOf(crs.getFloat(4));
if (type.equals("按年发放")) {
if (monthTo.indexOf("十二月份") != -1) {
total = salary + onceMoney;
} else {
total = salary;
}
}
if (type.equals("按季发放")) {
if (monthTo.indexOf("三月份") != -1
|| monthTo.indexOf("六月份") != -1
|| monthTo.indexOf("九月份") != -1
|| monthTo.indexOf("十二月份") != -1) {
total = salary + onceMoney;
} else {
total = salary;
}
}
if (type.equals("按月发放")) {
total = salary + onceMoney;
}
}
crs = null;
queryStr = "select moneyPayed from SalaryPay,Employee where eID=Employee.ID and IdentityNo='"
+ identity + "' and monthNo='" + monthTo + "'";
crs = con.getResultSet(queryStr);
float payed = 0;
if (crs.next()) {
payed = Float.valueOf(crs.getFloat(1));
}
crs = null;
queryStr = "select nnPunishment,occureTime from NNPunishment,nWorkRecord,Employee"
+ " where nnTypeID=nTypeID and eTitle='"
+ head
+ "' and eID=Employee.ID and IdentityNo='"
+ identity
+ "'";
crs = con.getResultSet(queryStr);
while (crs.next()) {
float punishment = crs.getFloat(1);
String occured = crs.getString(2);
identityDate.setFirstDate(occured.trim());
int month = monthBox.getSelectedIndex() + 1;
identityDate.setSecondDate(year+"-"+month);
if (identityDate.isInTheSameMonth()==0) {//两个日期在同一个月份
totalPunishment += punishment;
}
}
resultTF.setText(String.valueOf(total));
punishmentTF.setText(String.valueOf(totalPunishment));
moneyPayedTF.setText(String.valueOf(payed));
stillNotPayTF.setText(String.valueOf(total - payed
- totalPunishment));
if (total - payed == Float.valueOf(0)) {
pay.setEnabled(false);
}
} catch (SQLException sqle) {
sqle.printStackTrace();
return;
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null,
"月薪,未支付金额,每次的奖金派发金额都必须为数字!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
if (ae.getSource() == pay) {
try {
float thisTime = Float.valueOf(thisTimeTF.getText().trim());
float stillNotPay = Float.valueOf(stillNotPayTF.getText().trim());
float payed = Float.valueOf(moneyPayedTF.getText().trim());
if (thisTime > stillNotPay) {
JOptionPane.showMessageDialog(null, "本次支付金额不能大于未支付金额!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
if (stillNotPay == 0) {
pay.setEnabled(false);
}
if (thisTime == 0) {
JOptionPane.showMessageDialog(null, "本次支付金额不能为零!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
if (stillNotPay != 0 && thisTime <= stillNotPay) {
if (monthBox.getSelectedIndex() == -1) {
JOptionPane.showMessageDialog(null, "请选择月份!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
if (nameBox.getSelectedIndex() == -1) {
JOptionPane.showMessageDialog(null, "请选择名字!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String monthTo = year + "年" + monthBox.getSelectedItem();
String queryStr = "select* from SalaryPay,Employee where eID=Employee.ID and name='"
+ (String) nameBox.getSelectedItem()
+ "' and monthNo='" + monthTo + "'";
DBConnection con = new DBConnection();
CachedRowSet crs = con.getResultSet(queryStr);
String update = null;
if (crs.next()) {
int id = crs.getInt(1);
update = "update SalaryPay set moneyPayed=moneyPayed+"
+ thisTime + " where ID=" + id;
} else {
int index = nameBox.getSelectedIndex();
String eID = eIDS[index];
update = "insert into SalaryPay values(" + eID + ",'"
+ monthTo + "'," + thisTime + ")";
}
con.addSql(update);
con.doDML();
stillNotPayTF.setText(String
.valueOf(stillNotPay - thisTime));
moneyPayedTF.setText(String.valueOf(payed + thisTime));
}
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null,
"本月工资,本月未支付金额,本次支付金额都必须为数字!", "",
JOptionPane.INFORMATION_MESSAGE);
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -