📄 esalary.java
字号:
package file1;
/*
* 功能描述:实现员工工资的总体结算,在具体实现上是按照职称的类别来进行结算的
* @Author:黄顺武
* Version:1.3
*/
import java.awt.*;
import sun.jdbc.rowset.*;
import javax.swing.*;
import java.sql.*;
import java.util.Date;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class ESalary extends JPanel implements ActionListener, ItemListener {
private String months[] = { "一月份", "二月份", "三月份", "四月份", "五月份", "六月份",
"七月份", "八月份", "九月份", "十月份", "十一月份", "十二月份" };
private JLabel monthTo = new JLabel("查询月份:", SwingConstants.RIGHT);
private JComboBox monthToBox = new JComboBox(months);
private JButton pay = new JButton("本月工资结算");
private JPanel p1 = new JPanel();
private JTable recordTable = null;
private JScrollPane recScrollPane = null;
private String[] title = { "员工名字", "身份证号", "职称", "本月工资", "已支付金额", "未支付金额",
"非正常出勤罚款金额" };
private int titleNum = 0;
private String[][] data = null;
private GetDate getD = null;
private Formatter format = null;
private IdentityDate identityDate=new IdentityDate();//实例化对两个日期进行相关性判断的类对象
private int year = 0;
private String[] head = null;
private String[] eIDS = null;
public ESalary() {
getD = new GetDate();
format = new Formatter();
Date d = new Date();
String index = getD.getDate(format.valueConverted(d));
if (index == null) {
return;
}
year = getD.getYear();
titleNum = title.length;
String value = doCommon();
if (value == null) {
return;
}
p1.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 0));
p1.add(monthTo);
p1.add(monthToBox);
p1.add(monthToBox);
pay.setBorder(null);
pay.setBackground(Color.LIGHT_GRAY);
pay.setToolTipText("点击结算本月工资!");
p1.add(pay);
pay.addActionListener(this);
monthToBox.addItemListener(this);
monthToBox.setSelectedIndex(-1);
this.setLayout(new BorderLayout(0, 45));
this.add(p1, BorderLayout.NORTH);
this.validate();
}
public void itemStateChanged(ItemEvent ie) {
if (ie.getSource() == monthToBox) {
if (ie.getStateChange() == ItemEvent.SELECTED) {
String indexOfValue = doCommon();
if (indexOfValue == null) {
return;
}
}
}
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == pay) {
String queryStr = "select hName from Head";
CachedRowSet crs = null;
int count = 0;
int key = 0;
try {
DBConnection con = new DBConnection();
crs = con.getResultSet(queryStr);
while (crs.next()) {
count++;
}
if (count == 0) {
JOptionPane.showMessageDialog(null, "请您先添加员工职称!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
int index = monthToBox.getSelectedIndex();
if (index == -1) {
JOptionPane.showMessageDialog(null, "请您选择月份!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String month = year + "年" + monthToBox.getSelectedItem();
for (int var = 0; var < head.length; var++) {
String input = JOptionPane.showInputDialog(null, "请输入本次所有"
+ head[var] + "的结算金额!", "",
JOptionPane.INFORMATION_MESSAGE);
if (input == null) {
return;
}
float get = Float.valueOf(input);
if (get <0) {
JOptionPane.showMessageDialog(null, "结算金额必须为数字且大于等于零!",
"", JOptionPane.INFORMATION_MESSAGE);
var--;
continue;
}
float salary = getTotal(head[var]);
if (salary == -1) {
var--;
continue;
}
if (get > salary) {
JOptionPane.showMessageDialog(null,
"输入金额过大,请根据查询结果进行支付!", "",
JOptionPane.INFORMATION_MESSAGE);
var--;
continue;
}
crs = null;
queryStr = "select ID from Employee where head='"
+ head[var] + "'";
crs = con.getResultSet(queryStr);
count = 0;
while (crs.next()) {
count++;
}
crs.beforeFirst();
String[] eids = new String[count];
count = 0;
while (crs.next()) {
eids[count++] = String.valueOf(crs.getInt(1));
}
crs = null;
int temp = count;
count = 0;
for (; count < temp; count++) {
queryStr = "select nnPunishment from NNPunishment,nWorkRecord"
+ " where nnTypeID=nTypeID and eID="
+ eids[count]
+ " and eTitle='"
+ head[var]
+ "'";
crs = con.getResultSet(queryStr);
while (crs.next()) {
salary -= crs.getFloat(1);
}
crs = null;
queryStr = "select ID,moneyPayed from SalaryPay where eID="
+ eids[count] + " and monthNo='" + month + "'";
crs = con.getResultSet(queryStr);
String update = null;
if (crs.next()) {
int id = crs.getInt(1);
float moneyPayed = Float.valueOf(crs.getFloat(2));
if (get + moneyPayed > salary) {
key = 1;
update = "update SalaryPay set moneyPayed="
+ salary + " where ID=" + id;
} else {
update = "update SalaryPay set moneyPayed=moneyPayed+"
+ get + " where ID=" + id;
}
} else {
update = "insert into SalaryPay values("
+ eids[count] + ",'" + month + "'," + get
+ ")";
}
con.addSql(update);
con.doDML();
}
doCommon();
}
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "结算金额必须为数字且大于等于零!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
if (key != 0) {
JOptionPane.showMessageDialog(null,
"结算成功,但本次结算金额大于某些员工的未支付金额,这些员工的本次结算金额已被调整为未支付金额!", "",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
private String doCommon() {
int index = monthToBox.getSelectedIndex();
if (index == -1) {
JOptionPane.showMessageDialog(null, "请您选择月份!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
String month = year + "年" + monthToBox.getSelectedItem();
DBConnection con = new DBConnection();
String queryStr = "select hName from Head";
CachedRowSet crs = null;
int count = 0;
int total = 0;
int temp = 0;
try {
crs = con.getResultSet(queryStr);
while (crs.next()) {
count++;
}
if (count == 0) {
JOptionPane.showMessageDialog(null, "请您先添加员工职称!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
head = new String[count];
count = 0;
crs.beforeFirst();
while (crs.next()) {
head[count++] = crs.getString(1).trim();
}
crs = null;
temp = count;
count = 0;
for (; count < temp; count++) {
queryStr = "select ID,name,IdentityNo from Employee where head='"
+ head[count] + "'";
crs = con.getResultSet(queryStr);
while (crs.next()) {
total++;
}
}
} catch (SQLException sqle) {
sqle.printStackTrace();
return null;
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
return null;
}
data = new String[total][titleNum];
count = 0;
int row = 0;
for (; count < temp; count++) {
float totalMoney = 0;
try {
totalMoney = getTotal(head[count]);
if (totalMoney == -1) {
return null;
}
queryStr = "select ID,name,IdentityNo from Employee where head='"
+ head[count] + "'";
crs = con.getResultSet(queryStr);
int countSec = 0;
while (crs.next()) {
countSec++;
}
eIDS = new String[countSec];
crs.beforeFirst();
int rowSec = 0;
while (crs.next()) {
int id = crs.getInt(1);
eIDS[rowSec] = String.valueOf(id);
data[row][0] = crs.getString(2).trim();
data[row][1] = crs.getString(3).trim();
data[row][2] = head[count];
row++;
rowSec++;
}
crs = null;
int tempSec = rowSec;
rowSec = 0;
for (; rowSec < tempSec; rowSec++) {
queryStr = "select nnPunishment,occureTime from NNPunishment,nWorkRecord"
+ " where nnTypeID=nTypeID and eID="
+ eIDS[rowSec]
+ " and eTitle='" + head[count] + "'";
crs = con.getResultSet(queryStr);
float indicate = 0;// 该员工本月非正常出勤罚款金额
while (crs.next()) {
float punishment = crs.getFloat(1);
String occured = crs.getString(2);
identityDate.setFirstDate(occured.trim());
int monthTo = monthToBox.getSelectedIndex() + 1;
identityDate.setSecondDate(year+"-"+monthTo);
if (identityDate.isInTheSameMonth()==0) {//两个日期在同一个月份
indicate += punishment;// 把本月该员工的所有非正常出勤的罚款金额累加
}
}
data[row - tempSec + rowSec][3] = String
.valueOf(totalMoney);
crs = null;
String check = "select MoneyPayed from SalaryPay where eID="
+ eIDS[rowSec] + " and monthNo='" + month + "'";
crs = con.getResultSet(check);
float moneyPayed = 0;
if (crs.next()) {
moneyPayed = crs.getFloat(1);
} else {
moneyPayed = 0;
}
data[row - tempSec + rowSec][4] = String
.valueOf(moneyPayed);// 本行逻辑比较难懂
data[row - tempSec + rowSec][5] = String.valueOf(totalMoney
- moneyPayed - indicate);
data[row - tempSec + rowSec][6] = String.valueOf(indicate);
}
recordTable = new JTable(data, title);
recScrollPane = new JScrollPane(recordTable);
this.removeAll();
this.setLayout(new BorderLayout(0, 45));
this.add(p1, BorderLayout.NORTH);
this.add(recScrollPane, BorderLayout.CENTER);
this.validate();
} catch (SQLException sqle) {
sqle.printStackTrace();
return null;
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
return null;
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
return "success";
}
private float getTotal(String title) {
float total = 0;
try {
String month = year + "年" + monthToBox.getSelectedItem();
String head = title;
String queryStr = "select* from Head where hName='" + head + "'";
DBConnection con = new DBConnection();
CachedRowSet crs = con.getResultSet(queryStr);
int count = 0;
while (crs.next()) {
count++;
}
if (count == 0) {
JOptionPane.showMessageDialog(null, "数据库中没有该职称!", "",
JOptionPane.INFORMATION_MESSAGE);
return -1;
}
crs.beforeFirst();
if (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 (month.indexOf("十二月份") != -1) {
total = salary + onceMoney;
} else {
total = salary;
}
}
if (type.equals("按季发放")) {
if (month.indexOf("三月份") != -1
|| month.indexOf("六月份") != -1
|| month.indexOf("九月份") != -1
|| month.indexOf("十二月份") != -1) {
total = salary + onceMoney;
} else {
total = salary;
}
}
if (type.equals("按月发放")) {
total = salary + onceMoney;
}
}
} catch (SQLException sqle) {
sqle.printStackTrace();
return -1;
} catch (NumberFormatException nfe) {
nfe.printStackTrace();
return -1;
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
return -1;
}
return total;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -