2015/8/31
員工實體類
package com.lovo.bean;
/**
* 工資實體類
*
* @author acer
*
*/
public class MoneyBean {
/** 編號,不能重複 */
private int id;
/** 姓名 */
private String name;
/** 基本工資 */
private int baseMoney;
/** 獎金 */
private int awardMoney;
/** 罰金 */
private int fineMoney;
/** 實際工資 */
private int realMoney;
/** 發薪年月 */
private String date;
public MoneyBean() {
}
public MoneyBean(int id, String name, int baseMoney, int awardMoney,
int fineMoney, int realMoney, String date) {
super();
this.id = id;
this.name = name;
this.baseMoney = baseMoney;
this.awardMoney = awardMoney;
this.fineMoney = fineMoney;
this.realMoney = realMoney;
this.date = date;
}
public MoneyBean(int id) {
this.id = id;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MoneyBean other = (MoneyBean) obj;
if (id != other.id)
return false;
return true;
}
@Override
public String toString() {
return "MoneyBean [id=" + id + ", name=" + name + ", baseMoney="
+ baseMoney + ", awardMoney=" + awardMoney + ", fineMoney="
+ fineMoney + ", realMoney=" + realMoney + ", date=" + date
+ "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBaseMoney() {
return baseMoney;
}
public void setBaseMoney(int baseMoney) {
this.baseMoney = baseMoney;
}
public int getAwardMoney() {
return awardMoney;
}
public void setAwardMoney(int awardMoney) {
this.awardMoney = awardMoney;
}
public int getFineMoney() {
return fineMoney;
}
public void setFineMoney(int fineMoney) {
this.fineMoney = fineMoney;
}
public int getRealMoney() {
return realMoney;
}
public void setRealMoney(int realMoney) {
this.realMoney = realMoney;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
接口類:
package com.lovo.dao;
import java.util.List;
import com.lovo.bean.MoneyBean;
public interface IMoneyDao {
/**
* 查詢所有
* @return 工資實體對象集合
*/
public List<MoneyBean> findAll();
/**
* 按編號刪除
* @param id 編號
*/
public void del(int id);
/**
* 添加
* @param bean 工資實體對象
* @return 添加是否成功。如果姓名和發薪年月同時相同,則返回false,否則返回true;
*/
public boolean add(MoneyBean bean);
/**
* 修改工資信息,按編號修改獎金和罰金
* @param id 編號
* @param awardMoney 獎金
* @param fineMoney 罰金
*/
public void update(int id,int awardMoney,int fineMoney);
/**
* 按條件查詢
* @param item 選項內容
* @param value 查詢選項值
* @return 符合條件的對象集合
*/
public List<MoneyBean> findByItem(String item,String value);
}
接口實現類:
package com.lovo.dao.impl;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import com.lovo.bean.MoneyBean;
import com.lovo.dao.IMoneyDao;
public class MoneyDaoImpl implements IMoneyDao {
@Override
public List<MoneyBean> findAll() {
List<MoneyBean> list = new ArrayList<MoneyBean>();
Reader r = null;
BufferedReader br = null;
try {
r = new FileReader("info.txt");
br = new BufferedReader(r);
String str = null;
while ((str = br.readLine()) != null) {
MoneyBean bean = new MoneyBean();
String[] strArray = str.split("\\s+");
bean.setId(Integer.parseInt(strArray[0]));
bean.setName(strArray[1]);
bean.setBaseMoney(Integer.parseInt(strArray[2]));
bean.setAwardMoney(Integer.parseInt(strArray[3]));
bean.setFineMoney(Integer.parseInt(strArray[4]));
bean.setRealMoney(Integer.parseInt(strArray[5]));
bean.setDate(strArray[6]);
list.add(bean);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
r.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return list;
}
@Override
public void del(int id) {
List<MoneyBean> list = this.findAll();
list.remove(new MoneyBean(id));
this.savaFile(list);
}
private void savaFile(List<MoneyBean> list) {
Writer w = null;
try {
w = new FileWriter("info.txt");
for (MoneyBean bean : list) {
w.write(bean.getId() + " " + bean.getName() + " "
+ bean.getBaseMoney() + " " + bean.getAwardMoney()
+ " " + bean.getFineMoney() + " "
+ bean.getRealMoney() + " " + bean.getDate() + "\r\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
w.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public boolean add(MoneyBean bean) {
Boolean istrue = checkBean(bean);
if (istrue == true) {// 如果添加元素與原集合中元素的姓名和發薪日期同時重複,則返回false,不允許添加元素。
return false;
}
List<MoneyBean> list = this.findAll();
bean.setId(getNewId());
list.add(bean);
this.savaFile(list);
return true;
}
/**
* 檢測添加元素的名字和發薪日期是否與原集合中所有的元素姓名和發薪日期同時重複
*
* @param list
* @return 是否重複
*/
private boolean checkBean(MoneyBean bean) {
List<MoneyBean> list = this.findAll();
for (MoneyBean oldBean : list) {
if (bean.getName().equals(oldBean.getName())
&& bean.getDate().equals(oldBean.getDate())) {
return true;
}
}
return false;
}
/**
* 得到最新編號
*
* @return 最新編號
*/
private int getNewId() {
List<MoneyBean> list = this.findAll();
MoneyBean maxBean = Collections.max(list, new Comparator<MoneyBean>() {
@Override
public int compare(MoneyBean o1, MoneyBean o2) {
return o1.getId() - o2.getId();
}
});
return maxBean.getId() + 1;
}
@Override
public void update(int id, int awardMoney, int fineMoney) {
List<MoneyBean> list = this.findAll();
// 得到指定id對應的下標
int index = list.indexOf(new MoneyBean(id));
// 得到指定下標對應的元素
MoneyBean bean = list.get(index);
bean.setAwardMoney(awardMoney);
bean.setFineMoney(fineMoney);
bean.setRealMoney(bean.getBaseMoney() + bean.getAwardMoney()
- bean.getFineMoney());
this.savaFile(list);
}
@Override
public List<MoneyBean> findByItem(String item, String value) {
List<MoneyBean> list = new ArrayList<MoneyBean>();
List<MoneyBean> allList = this.findAll();
if ("員工姓名".equals(item)) {
for (MoneyBean bean : allList) {
if ((bean.getName().indexOf(value)) != -1) {
list.add(bean);
}
}
} else if ("發薪年月".equals(item)) {
for (MoneyBean bean : allList) {
if (value.equals(bean.getDate())) {
list.add(bean);
}
}
}
return list;
}
}
窗體類
主窗體:
package com.lovo.frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import com.lovo.bean.MoneyBean;
import com.lovo.dao.IMoneyDao;
import com.lovo.dao.impl.MoneyDaoImpl;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoComboBox;
import com.lovo.netCRM.component.LovoTable;
@SuppressWarnings("serial")
public class MainFrame extends JFrame {
// 第四個參數“id”是標誌記錄的唯一性字段名
private LovoTable table = new LovoTable(this, new String[] { "員工姓名",
"基本工資", "獎金", "罰金", "實際工資", "發薪年月" }, new String[] { "name",
"baseMoney", "awardMoney", "fineMoney", "realMoney", "date" }, "id");
private IMoneyDao dao = new MoneyDaoImpl();
/** 查詢下拉框 */
private LovoComboBox<String> nameComboBox = new LovoComboBox<String>(
new String[] { "員工姓名", "發薪年月" }, 400, 250, this);
private JTextField realMoneyTxt = new JTextField();
public MainFrame() {
this.setLayout(null);
realMoneyTxt.setBounds(540, 250, 100, 20);
this.add(realMoneyTxt);
LovoButton findButton = new LovoButton("查詢", 650, 250, this);
findButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 得到選項
String item = nameComboBox.getItem();
// 得到文本框內容
String value = realMoneyTxt.getText();
List<MoneyBean> findeList = dao.findByItem(item, value);
table.updateLovoTable(findeList);
}
});
table.setSizeAndLocation(30, 30, 500, 200);
// 從文件中讀取數據
List<MoneyBean> list = dao.findAll();
// 更新表格
table.updateLovoTable(list);
LovoButton addButton = new LovoButton("添加", 30, 250, this);
LovoButton delButton = new LovoButton("刪除", 150, 250, this);
LovoButton updateButton = new LovoButton("修改", 280, 250, this);
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
MainFrame.this.dispose();
new AddFrame();
}
});
delButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 得到選中行的唯一標識值,要求構建表格時,必須填寫第四個參數
int id = table.getKey();
if (id == -1) {
JOptionPane.showMessageDialog(null, "請選中行操作!");
return;
}
int x = JOptionPane.showConfirmDialog(null, "是否要刪除?");
if (x == 0) {
dao.del(id);
List<MoneyBean> list = dao.findAll();
table.updateLovoTable(list);
}
}
});
updateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int id = table.getKey();
if (id == -1) {
JOptionPane.showMessageDialog(null, "請選中行操作");
return;
}
MainFrame.this.dispose();
new UpdateFrame(id);
}
});
this.setSize(800, 400);
this.setVisible(true);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
}
public static void main(String[] args) {
new MainFrame();
}
}
添加窗體類:
package com.lovo.frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import com.lovo.bean.MoneyBean;
import com.lovo.dao.IMoneyDao;
import com.lovo.dao.impl.MoneyDaoImpl;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoTxt;
@SuppressWarnings("serial")
public class AddFrame extends JFrame {
private LovoTxt nameTxt = new LovoTxt("員工姓名:", 30, 30, this);
private LovoTxt baseMoneyTxt = new LovoTxt("基本工資:", 30, 80, this);
private LovoTxt awardMoneyTxt = new LovoTxt("獎金:", 30, 130, this);
private LovoTxt fineMoneyTxt = new LovoTxt("罰金:", 30, 180, this);
private LovoTxt dateTxt = new LovoTxt("發薪年月:", 30, 230, this);
private IMoneyDao dao = new MoneyDaoImpl();
public AddFrame() {
this.setLayout(null);
LovoButton sureButton = new LovoButton("確認", 100, 280, this);
sureButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 將界面數據封裝成實體對象
MoneyBean bean = new MoneyBean();
bean.setName(nameTxt.getText());
bean.setBaseMoney(Integer.parseInt(baseMoneyTxt.getText()));
bean.setAwardMoney(Integer.parseInt(awardMoneyTxt.getText()));
bean.setFineMoney(Integer.parseInt(fineMoneyTxt.getText()));
bean.setRealMoney(bean.getBaseMoney() + bean.getAwardMoney()
- bean.getFineMoney());
bean.setDate(dateTxt.getText());
boolean istrue=dao.add(bean);
if(istrue==false){
JOptionPane.showMessageDialog(null, "該員工當月工資已發");
}
else{
AddFrame.this.dispose();
new MainFrame();
}
}
});
this.setSize(300, 400);
this.setVisible(true);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
}
}
修改窗體類:
package com.lovo.frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JFrame;
import com.lovo.bean.MoneyBean;
import com.lovo.dao.IMoneyDao;
import com.lovo.dao.impl.MoneyDaoImpl;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoLabel;
import com.lovo.netCRM.component.LovoTxt;
@SuppressWarnings("serial")
public class UpdateFrame extends JFrame {
private LovoLabel nameTxt = new LovoLabel("姓名", 30, 30, this);
private LovoLabel baseMoneyTxt = new LovoLabel("基本工資", 30, 80, this);
private LovoTxt awardMoneyTxt = new LovoTxt("獎金", 30, 130, this);
private LovoTxt fineMoneyTxt = new LovoTxt("罰金", 30, 180, this);
private LovoLabel dateTxt = new LovoLabel("發薪年月", 30, 230, this);
private IMoneyDao dao = new MoneyDaoImpl();
/**
* 構造方法
*
* @param id
* 要修改記錄的id
*/
public UpdateFrame(final int id) {
this.setLayout(null);
this.init(id);
LovoButton sureButton = new LovoButton("確定", 120, 280, this);
sureButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dao.update(id, Integer.parseInt(awardMoneyTxt.getText()),
Integer.parseInt(fineMoneyTxt.getText()));
UpdateFrame.this.dispose();
new MainFrame();
}
});
this.setSize(300, 400);
this.setVisible(true);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
}
/**
* 初始化數據
*
* @param id
* 要修改的記錄id
*/
private void init(int id) {
List<MoneyBean> list = dao.findAll();
int index = list.indexOf(new MoneyBean(id));
MoneyBean bean = list.get(index);
nameTxt.setText(bean.getName());
baseMoneyTxt.setText(bean.getBaseMoney() + "");
awardMoneyTxt.setText(bean.getAwardMoney() + "");
fineMoneyTxt.setText(bean.getFineMoney() + "");
dateTxt.setText(bean.getDate());
}
}