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("導入成功"); }
接下來的想法是把重復的員工去除,想先通過提醒使用者導入員工後,刪除原有員工文件,然後在使用者退出前提示導入員工名單進行解決可能出現的員工重復問題
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- Java基礎題新手練習(三)
- Java語言之包和繼承詳解
- 基於Arrays.sort()和lambda表達式
- 深入淺析C/C++語言結構體指針的使用註意事項
- Java集合的Collection接口和List接口詳解