java实现工资管理简单程序
作者:飘若归客
这篇文章主要为大家详细介绍了java实现工资管理简单程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文为大家分享了java实现工资管理简单程序的具体代码,供大家参考,具体内容如下
程序总体说明(ReadMe):
总体分为四部分:
管理程序(staffmanagment.java):
- 在这里面使用了arraylist泛型来存放数据
- 博主正在想办法把数据放进mysql,这样读取会更方便,而且数据不会消失
- 当然(也可以把数据存入txt文件)
import java.util.ArrayList; //员工管理 public class staffmanagement { ArrayList al = null;//员工信息在这里面建立 public staffmanagement() { al = new ArrayList(); } //成员方法 //添加新员工 public void addstaff(staff staff) { al.add(staff); } //根据工号查询员工的信息 public void showInfo(String no) { int flag = 0; for(int i=0; i<al.size(); i++) { staff staff= (staff)al.get(i); if(staff.getNo().equals(no)) { flag = 1; System.out.println("员工"+(i+1)+"的信息是:"); System.out.print("姓名:"+staff.getName()); System.out.print(" 性别:"+staff.getSex()); System.out.print(" 年龄:"+staff.getAge()); System.out.print(" 工号:"+staff.getNo()); System.out.print(" 薪资:"+staff.getSalary()); System.out.print(" 工作:"); if(staff.getWork() == 1) System.out.println("common staff"); else if(staff.getWork() == 2) System.out.println("common manager"); else if(staff.getWork() == 3) System.out.println("head manager"); System.out.println(staff); } } if(flag == 0) System.out.println("该工号不存在"); } //显示所有员工信息 public void showAllInfo() { for(int i=0; i<al.size(); i++) { staff staff= (staff)al.get(i); System.out.println("staff"+(i+1)+"的信息是:"); System.out.print("姓名:"+staff.getName()); System.out.print(" 性别:"+staff.getSex()); System.out.print(" 年龄:"+staff.getAge()); System.out.print(" 工号:"+staff.getNo()); System.out.print(" 薪资:"+staff.getSalary()); System.out.print(" 工作:"); if(staff.getWork() == 1) System.out.println("common staff"); else if(staff.getWork() == 2) System.out.println("common manager"); else if(staff.getWork() == 3) System.out.println("head manager"); System.out.println(staff); } } //修改员工的薪水 public void modifySal(String no, double sal) //员工工号 最新的薪资 { int flag = 0; for(int i=0; i<al.size(); i++) { staff staff= (staff)al.get(i); if(staff.getNo().equals(no)) { flag = 1; staff.setSalary(sal); } } if(flag == 0) System.out.println("找不到所查工号"); } //根据工号删除该员工的信息 public void deleteInfo(String no) { int flag = 0; for(int i=0; i<al.size(); i++) { staff staff= (staff)al.get(i); if(staff.getNo().equals(no)) { flag = 1; al.remove(i); } } if(flag == 0) System.out.println("找不到所查工号"); } //统计当前公司的三个水平工资线 public void calSalary() { if (al.isEmpty()) { System.out.println("the stafflist is empty,please input the staff infomation"); System.out.println("please input the No 1 to add the staffinfo"); } else { double totalSal = 0; double minSal = ((staff) al.get(0)).getSalary(); double maxSal = ((staff) al.get(0)).getSalary(); for (int i = 0; i < al.size(); i++) { staff staff = (staff) al.get(i); totalSal += staff.getSalary(); if (minSal > staff.getSalary()) { minSal = staff.getSalary(); } if (maxSal < staff.getSalary()) { maxSal = staff.getSalary(); } } System.out.println(al.size() + "个人的平均薪资为:" + totalSal / al.size()); System.out.println(al.size() + "个人的最低薪资为:" + minSal); System.out.println(al.size() + "个人的最高薪资为:" + maxSal); } } }
界面模块(SalarySystem.java)
- 对不同员工的工资最低进行了限制,毕竟法律规定了不能给太低
- 年龄也是(至少得成年吧——你懂的)
- 职位还可以安排更多
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; /* * @antuor:jeffw * 员工界面为博主清风追梦的写法 * 清风大大的blogs:http://www.cnblogs.com/qingfengzhuimeng/p/6509965.html * io输入输出 * 在员工的类别上加入员工的职业区分别建立不同的类 * 程序使用英语+中文方式= =顺便练习英语 */ public class SalarySystem { public static void main(String[] args) throws IOException //使用bufferreader读取更加的安全,当读取不到的返回错误抛出 { staffmanagement sys = new staffmanagement(); InputStreamReader isr = new InputStreamReader(System.in);//输入流为系统,也可以从文本读取,一次读取一个字符 BufferedReader br = new BufferedReader(isr);//读取,对输入流的封装,可以读取一行,一个字符或者是一个数字 //这里以isr读取到的传给br while(true) { //简易菜单 System.out.println("简单管理菜单" ); System.out.println("---------------------------------------"); System.out.println("1.添加新员工"); System.out.println("2.根据工号显示员工的信息"); System.out.println("3.显示所有员工信息"); System.out.println("4.修改员工的薪水"); System.out.println("5.根据工号删除员工的信息"); System.out.println("6.统计员工的平均工资、最高工资和最低工资"); System.out.println("7.查看具体工作名称"); System.out.println("8.最低工资说明"); System.out.println("9.清屏"); System.out.println("0.退出"); System.out.println("---------------------------------------"); System.out.println("请选择序号:"); String no = br.readLine(); if(no.equals("1")) { System.out.println("添加新员工:"); System.out.println("请输入姓名:"); String name = br.readLine(); System.out.println("请输入性别(男or女):"); String sex = br.readLine(); System.out.println("请输入工作:"); System.out.println("目前共有三种职位:\n" + "1.common_staff\n" + "2.common_manager\n" + "3.head_manager"); System.out.println("please input the work id:"); int work = Integer.parseInt(br.readLine()); System.out.println("请输入年龄:"); Scanner sc = new Scanner(System.in); int age = sc.nextInt(); if( age < 18 ){ System.out.println("the age is too small,please input again"); sc = new Scanner(System.in); age = sc.nextInt(); } System.out.println("请输入工号:"); String staffNo = br.readLine(); System.out.println("请输入工资:"); double salary = Double.parseDouble(br.readLine()); if( work == 1 ) { while (salary < 2000) { System.out.println("the common staff's salary is too low"); System.out.println("请重新输入"); salary = Double.parseDouble(br.readLine()); } } else if( work == 2 ) { while (salary < 5000) { System.out.println("the manager's salary is too low"); System.out.println("请重新输入"); salary = Double.parseDouble(br.readLine()); } } else if( work == 3 ) { while (salary < 8000) { System.out.println("the head manager's salary is too low"); System.out.println("请重新输入"); salary = Double.parseDouble(br.readLine()); } } staff staff = null; if (work == 1) { staff = new staff(name, sex, age, staffNo, salary, work); } else if(work == 2){ staff = new manager(name, sex, age, staffNo, salary, work); }else if(work == 3 ) { staff = new manager(name, sex, age, staffNo, salary, work); } sys.addstaff(staff); }else if(no.equals("2")){ System.out.println("请输入员工号:"); String staffNo = br.readLine(); sys.showInfo(staffNo); }else if(no.equals("3")){ System.out.println("显示所有员工信息:"); sys.showAllInfo(); }else if(no.equals("4")){ System.out.println("修改员工的薪水:"); System.out.println("输入员工工号:"); String staffNo = br.readLine();//接收输入的工号 System.out.println("修改后的员工工资:"); String sal = br.readLine(); double salary = Double.parseDouble(sal); sys.modifySal(staffNo, salary); }else if(no.equals("5")){ System.out.println("根据工号删除该员工的信息:"); System.out.println("输入员工工号:"); String staffNo = br.readLine();//接收输入的工号 sys.deleteInfo(staffNo); }else if(no.equals("6")){ System.out.println("统计员工的平均工资、最高工资和最低工资:"); sys.calSalary(); }else if(no.equals("7")) { System.out.println("查看具体工作名称:\n" + "序号1为common staff\n" + "序号2为conmmon manager\n" + "序号3为head manger"); }else if(no.equals("8")) { System.out.println("最低工资说明:\n" + "1.common staff 最低工资为2000\n" + "2.conmmon manager 最低工资为5000\n" + "3.head manager 最低工资为8000\n"); }else if(no.equals("9")){ int i=0; while( i < 80) { System.out.println("********"); i++; }//java因为ide的原因没有提供内置类似c++的方法,所以只能简陋的使用***来清屏了= = System.out.println("如果觉得看着麻烦的话,可以在console里面鼠标右击->clear"); }else if(no.equals("0")){ System.exit(0); }else{ System.out.println("请输入正确的序号"); } } } }
普通职位模块(staff.java)
所有的员工模块通过此模块进行扩展
//普通员工类 public class staff { private String name; private String sex; private int age; private String no; private double salary; private int work; public staff(){} public staff(String name, String sex, int age, String no, double salary,int work) { this.name = name; this.sex = sex; this.age = age; this.no = no; this.salary = salary; this.work = work; } @Override public String toString() { // return "this is a common staff\n" // +"name:"+name+"\n" // +"sex :"+sex+"\n" // +"age :"+age+"\n" // +"no :"+no+"\n" // +"salary :"+salary+"\n" // +"work :"+work+"\n"; return getWork()+" the common staff is working and the salary is "+getSalary()+"\n" + "his/her workid is 1 and he/she needs handle with the company's common situations"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getNo() { return no; } public void setNo(String no) { this.no = no; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public void setWork(int work){ this.work = work; } public int getWork(){ return work; } }
扩展职位模块
1.common manager
public class manager extends staff{ public manager(String name, String sex, int age, String no, double salary, int work) { setName(name); setSalary(salary); setAge(age); setNo(no); setSex(sex); setWork(work); } public manager(){ System.out.println("this is a manager"); } @Override public String toString() { return "the manager is working and the salary is "+getSalary()+"\n" + "his workid is 3 and he needs handle with the company's emergency situations"; } }
2.head manager
public class headmanager extends manager{ public headmanager(String name, String sex, int age, String no, double salary, int work) { setName(name); setSalary(salary); setAge(age); setNo(no); setSex(sex); setWork(work); } public headmanager(){ System.out.println("this is a headmanager"); System.out.println("his workid is 3 " + "and he needs handle with " + "the company's emergency situations"); } @Override public String toString() { return "the headmanager is working and the salary is "+getSalary()+getSalary()+"\n" + "his/her workid is 2 and he/she needs hanle whih the compant's main development"; } }
运行展示
2018.12.15更新:
现在还没有想到怎么用数据库连接,不过新学习了file文件操作模式,现在把这种方式加入到员工管理里面:
以下是增加内容:
else if(no.equals("10")) { System.out.println("正在读取中,请稍候..."); File file = new File("1.txt"); Scanner input = new Scanner(file); while(input.hasNext()){ String name = input.nextLine(); String sex = input.nextLine(); int work = input.nextInt(); int age =input.nextInt(); input.nextLine(); String staffNo = input.nextLine(); double salary = input.nextDouble(); staff staff = null; if (work == 1) { staff = new staff(name, sex, age, staffNo, salary, work); } else if(work == 2){ staff = new manager(name, sex, age, staffNo, salary, work); }else if(work == 3 ) { staff = new manager(name, sex, age, staffNo, salary, work); }else{ System.out.println("不存在此职业"); } sys.addstaff(staff); System.out.println("导入成功"); }
接下来的想法是把重复的员工去除,想先通过提醒使用者导入员工后,删除原有员工文件,然后在使用者退出前提示导入员工名单进行解决可能出现的员工重复问题
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。