📄 ksalary.java
字号:
package file1;
/*
* 功能描述:员工的某月的工资查询以及结算入口
* Author:黄顺武
* Time:---
* Last Modified:2007-12-15
* Modify Reason:数据库连接类DBConnection 的内部结构设计得到优化
*/
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 KSalary extends JPanel implements ActionListener, ItemListener {
private String months[] = { "一月份", "二月份", "三月份", "四月份", "五月份", "六月份",
"七月份", "八月份", "九月份", "十月份", "十一月份", "十二月份" };
private JLabel head = new JLabel("员工职称:", SwingConstants.RIGHT);
private JLabel monthTo = new JLabel("查询月份:", SwingConstants.RIGHT);
private JComboBox headBox = new JComboBox();
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 String[] eIDS = null;
private GetDate getD = null;
private Formatter format = null;
private IdentityDate identityDate=new IdentityDate();//实例化对两个日期进行相关性判断的类对象
private int year = 0;
public KSalary() {
String result = getHeads();
if (result == 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();
p1.setLayout(new FlowLayout(FlowLayout.CENTER, 25, 0));
head.setToolTipText("请选择职称!");
head.setHorizontalTextPosition(JLabel.CENTER);
head.setForeground(Color.DARK_GRAY);
p1.add(head);
p1.add(headBox);
p1.add(monthTo);
monthTo.setToolTipText("请选择查询月份!");
monthTo.setHorizontalTextPosition(JLabel.CENTER);
monthTo.setForeground(Color.DARK_GRAY);
p1.add(monthToBox);
pay.setBorder(null);
pay.setBackground(Color.LIGHT_GRAY);
pay.setToolTipText("点击结算本月工资!");
p1.add(pay);
pay.addActionListener(this);
titleNum = title.length;
monthToBox.setSelectedIndex(-1);
monthToBox.addItemListener(this);
this.setLayout(new BorderLayout(0, 45));
this.add(p1, BorderLayout.NORTH);
this.validate();
}
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).trim());
}
headBox.setSelectedIndex(-1);
} catch (SQLException sqle) {
sqle.printStackTrace();
return null;
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
return null;
}
return "success";
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == pay) {
int index = headBox.getSelectedIndex();
if (index == -1) {
JOptionPane.showMessageDialog(null, "请您选择职称!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
index = monthToBox.getSelectedIndex();
if (index == -1) {
JOptionPane.showMessageDialog(null, "请您选择月份!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String month = year + "年" + monthToBox.getSelectedItem();
String head = (String) headBox.getSelectedItem();
String input = JOptionPane.showInputDialog(null, "请输入本次结算金额!", "",
JOptionPane.INFORMATION_MESSAGE);
if (input == null) {
return;
}
try {
float get = Float.valueOf(input);
if (get <0) {
JOptionPane.showMessageDialog(null, "结算金额必须为数字且大于等于零!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
float salary = getTotal();
if (salary == -1) {
return;
}
DBConnection con = new DBConnection();
CachedRowSet crs = null;
String queryStr = "select ID from Employee where head='" + head
+ "'";
crs = con.getResultSet(queryStr);
int 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,occureTime from NNPunishment,nWorkRecord"
+ " where nnTypeID=nTypeID and eID="
+ eids[count]
+ " and eTitle='" + head + "'";
crs = con.getResultSet(queryStr);
while (crs.next()) {
float punishment = crs.getFloat(1);
String occured = crs.getString(2);
identityDate.setFirstDate(occured.trim());
int Month = monthToBox.getSelectedIndex() + 1;
identityDate.setSecondDate(year+"-"+Month);
if (identityDate.isInTheSameMonth()==0) {//两个日期在同一个月份
salary -= punishment;
}
}
crs = null;
queryStr = "select ID,moneyPayed from SalaryPay where eID="
+ eids[count] + " and monthNo='" + month + "'";
crs = con.getResultSet(queryStr);
String update = null;
float moneyPayed = 0;
if (crs.next()) {
int id = crs.getInt(1);
moneyPayed = crs.getFloat(2);
if (get + moneyPayed > salary) {
JOptionPane.showMessageDialog(null, "输入金额过大!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
} else {
update = "update SalaryPay set moneyPayed=moneyPayed+"
+ get + " where ID=" + id;
con.addSql(update);
try {
con.doDML();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
} else {
if (get <= salary) {
update = "insert into SalaryPay values("
+ eids[count] + ",'" + month + "'," + get
+ ")";
con.addSql(update);
con.doDML();
} else {
JOptionPane.showMessageDialog(null, "输入金额过大!", "",
JOptionPane.INFORMATION_MESSAGE);
return;
}
}
}
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "结算金额必须为数字且大于零!", "",
JOptionPane.INFORMATION_MESSAGE);
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
doCommon();
}
}
public void itemStateChanged(ItemEvent ie) {
if (ie.getSource() == monthToBox) {
if (ie.getStateChange() == ItemEvent.SELECTED) {
String returned = doCommon();
if (returned == null) {
return;
}
}
}
}
private String doCommon() {
int index = headBox.getSelectedIndex();
if (index == -1) {
JOptionPane.showMessageDialog(null, "请您选择职称!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
index = monthToBox.getSelectedIndex();
if (index == -1) {
JOptionPane.showMessageDialog(null, "请您选择月份!", "",
JOptionPane.INFORMATION_MESSAGE);
return null;
}
String month = year + "年" + monthToBox.getSelectedItem();
String head = (String) headBox.getSelectedItem();
int count = 0;
float totalMoney = 0;
try {
totalMoney = getTotal();
if (totalMoney == -1) {
return null;
}
DBConnection con = new DBConnection();
String queryStr = "select ID,name,IdentityNo from Employee where head='"
+ head + "'";
CachedRowSet crs = con.getResultSet(queryStr);
count = 0;
while (crs.next()) {
count++;
}
data = new String[count][titleNum];
eIDS = new String[count];
crs.beforeFirst();
int row = 0;
while (crs.next()) {
eIDS[row] = String.valueOf(crs.getInt(1));
data[row][0] = crs.getString(2).trim();
data[row][1] = crs.getString(3).trim();
row++;
}
crs = null;
int temp = row;
row = 0;
int monthTo = monthToBox.getSelectedIndex() + 1;
for (; row < temp; row++) {
queryStr = "select nnPunishment,occureTime from NNPunishment,nWorkRecord"
+ " where eID="
+ eIDS[row]
+ " and nTypeID=nnTypeID and eTitle='" + head + "'";
crs = con.getResultSet(queryStr);
float indicate = 0;// 该员工本月非正常出勤罚款金额
while (crs.next()) {
float punishment = crs.getFloat(1);
String dateStr=crs.getString(2);
identityDate.setFirstDate(dateStr.trim());
identityDate.setSecondDate(year+"-"+monthTo);
if (identityDate.isInTheSameMonth()==0) {//两个日期在同一个月份
indicate += punishment;// //把本月该员工的所有非正常出勤的罚款金额累加
}
}
data[row][2] = String.valueOf(totalMoney);
crs = null;
String check = "select MoneyPayed from SalaryPay where eID="
+ eIDS[row] + " and monthNo='" + month + "'";
crs = con.getResultSet(check);
float moneyPayed = 0;
if (crs.next()) {
moneyPayed = crs.getFloat(1);
} else {
moneyPayed = 0;
}
data[row][3] = String.valueOf(moneyPayed);
data[row][4] = String.valueOf(totalMoney - moneyPayed
- indicate);
data[row][5] = 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() {
float total = 0;
try {
DBConnection con = new DBConnection();
String month = year + "年" + monthToBox.getSelectedItem();
String head = (String) headBox.getSelectedItem();
String queryStr = "select* from Head where hName='" + head + "'";
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 = crs.getFloat(2);
String type_temp=crs.getString(3);
String type = type_temp.trim();
float onceMoney = 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 total;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -