8/31工资问题

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());

}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值