您好,登錄后才能下訂單哦!
今天小編給大家分享一下java實現人員信息管理系統的代碼怎么寫的相關知識點,內容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
實現增刪改查.
java入門的練手小程序
1.Person類
package p1; public class Person { // Person屬性 private int num; private String name; private String sex; private int salary; public Person(int num, String name, String sex, int salary) { super(); this.num = num; this.name = name; this.sex = sex; this.salary = salary; } // 對Perosn操作的方法 public int getNum() { return num; } public void setNum(int num) { this.num = num; } 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 getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } }
2.SysMenu類
package p1; public class SysMenu { public static final String[] MENU = { "1.員工信息管理", "2.退出" }; public static final String[] OPERATION_MENU = { "1.新增", "2.查看", "3.修改", "4.刪除", "5.返回" }; public static void showMenu(String[] Menu) { for (int i = 0; i < Menu.length; i++) System.out.print(Menu[i] + "\t\t"); System.out.println(); System.out.println("---------------------------------------"); } }
3.SysInfo類
package p1; import java.util.ArrayList; import java.util.List; public class SysInfo { private static List informationList = new ArrayList(); // 獲取 informationList public static List getList() { return informationList; } }
4.InformationService類
package p1; import java.util.List; public class InformationService { private List informationList = SysInfo.getList(); // 獲取信息列表 public List getList() { return informationList; } // 按編號查找信息 public Person getPersonByNum(final int num) { if (num < 1) { System.out.println("編號錯誤"); return null; } for (int i = 0; i < informationList.size(); i++) { Person p = (Person) informationList.get(i); if (p.getNum() == num) { System.out.println("查找成功"); return p; } } System.out.println("查找失敗"); return null; } //查看單一Person信息 public void showAPerson(Person p) { System.out.println("編號\t\t姓名\t\t性別\t\t薪水"); System.out.println(p.getNum()+ "\t\t" + p.getName() + "\t\t" + p.getSex() + "\t\t" + p.getSalary()); } //show all Person public void showPerson() { System.out.println("編號\t\t姓名\t\t性別\t\t薪水"); List ps = getList(); for (int i = 0; i < ps.size(); i++) { Person p = (Person) ps.get(i); System.out.println(p.getNum() + "\t\t" + p.getName() + "\t\t" + p.getSex() + "\t\t" + p.getSalary()); } } // 按名字查找信息 public Person getPersonByName(final String name) { if (name == null) return null; for (int i = 0; i < informationList.size(); i++) { Person p = (Person) informationList.get(i); if (p.getName().equals(name)) { return p; } } return null; } //檢查對象是否存在 public boolean CheckExitByNum(int num) { for(int i=0;i<informationList.size();i++) { Person p = (Person)informationList.get(i); if(p.getNum()==num) return true; } return false; } //save Person public void savePerson(Person p) { p.setNum(getPersonMaxInt()+1); informationList.add(p); } // 查找最大編號 public int getPersonMaxInt() { int max = 0; for(int i =0;i<informationList.size();i++) { Person p =(Person)informationList.get(i); if(max < p.getNum()) max = p.getNum(); } return max; } }
5.SysRun類
package p1; import java.util.InputMismatchException; import java.util.List; import java.util.Scanner; public class SysRun { private List informationList = SysInfo.getList(); private Scanner s = new Scanner(System.in); private InformationService is = new InformationService(); // 系統運行類 public static void main(String[] args) { SysRun sys = new SysRun(); sys.sysRun(); } public void sysRun() { System.out.println("啟動系統管理系統"); boolean isExit = false; do { System.out.println("----------操作選項-------------"); SysMenu.showMenu(SysMenu.MENU); // 獲取用戶輸入 int operNum = getCorrONum(SysMenu.MENU); // 管理操作 isExit = doManageNum(operNum); } while (!isExit); System.out.println("系統退出."); } private boolean doManageNum(int operNum) { boolean isExit = false; switch (operNum) { case 1: is.showPerson(); System.out.println("----------操作選項-------------"); SysMenu.showMenu(SysMenu.OPERATION_MENU); // addPerson();//test System.out.println("輸入功能選擇:"); int num = getVaildInt(); doOperationNum(num); break; case 2: isExit = true; return isExit; } return isExit; } // doOperationNum private void doOperationNum(int OperationNum) { // 增,查,修,刪,返回 switch (OperationNum) { case 1: // add addPerson(); is.showPerson(); break; case 2: // 查看 viewPerson(); break; case 3: updatePerson(); break; case 4: deletePerson(); is.showPerson(); break; case 5: break; } } // 刪除Person private void deletePerson() { int num; // Person p; boolean isOk = false; System.out.println("請輸入要刪除信息的編號:"); do { num = getVaildInt(); isOk = is.CheckExitByNum(num); if (isOk == true) { System.out.println("編號信息查找成功。"); informationList.remove(is.getPersonByNum(num)); } else System.out.println("輸入編號有誤,請重新輸入:"); } while (!isOk); } // 修改Person public void updatePerson() { System.out.println("請輸入要修改的信息編號:"); boolean isOk = false; Person p; do { int num = getVaildInt(); isOk = is.CheckExitByNum(num); if (isOk == true) { isOk = true; p = is.getPersonByNum(num); is.showAPerson(p); System.out.println("請輸入名字:"); String name = s.next(); System.out.println("請輸入性別:"); String sex = getVaildSex(); System.out.println("請輸入工資:"); int salary = getVaildInt(); p.setName(name); p.setSex(sex); p.setSalary(salary); is.showPerson(); } else System.out.println("輸入要修改的編號有誤,請重新輸入:"); } while (!isOk); } // 查看viewPerson() private void viewPerson() { System.out.println("請輸入要查看的人的信息編號:"); Person p; boolean isOk = false; do { int num = getVaildInt(); boolean NumIsOk = is.CheckExitByNum(num); if (NumIsOk == true) { p = is.getPersonByNum(num); is.showAPerson(p); isOk = true; } else { System.out.println("無此編號的人的信息,請重新輸入:"); } } while (!isOk); } // addPerson() private void addPerson() { System.out.println("------------新增對象---------------"); boolean isOk = false; String name = null; do { System.out.println("請輸入名稱(且不能與現有的對象重名)"); name = s.next(); // 處理同名沖突 if (is.getPersonByName(name) == null) { isOk = true; } else { System.out.println("該人信息已存在,請重新輸入!"); s.next(); } } while (!isOk); // other information System.out.println("請輸入其他信息..."); System.out.println("sex:"); String sex = getVaildSex(); System.out.println("salary:"); int salary = getVaildInt(); // save is.savePerson(new Person(0, name, sex, salary)); } /* 輸入有效int */ private int getVaildInt() { int num = 0; boolean isOk = false; do { try { num = s.nextInt(); isOk = true; } catch (InputMismatchException e) { System.out.println("輸入錯誤,請重新輸入"); s.next(); } } while (!isOk); return num; } /* 輸入有效sex信息 */ private String getVaildSex() { String sex = null; boolean isOk = false; do { sex = s.next(); if (sex.equals("f") || sex.equals("m")) isOk = true; else { System.out.println("sex輸入讓 有誤,請重新輸入"); } } while (!isOk); return sex; } public int getCorrONum(String[] targetMenu) { System.out.println("請輸入要選擇的操作:"); int inputNum = 0; boolean inputIsOk = false; do { try { inputNum = s.nextInt(); System.out.println("輸入的是" + inputNum); if (inputNum >= 1 && inputNum <= targetMenu.length) { inputIsOk = true; } else { System.out.println("輸入錯誤,請重新輸入!"); } } catch (InputMismatchException e) { System.out.println("輸入有誤,請重新輸入"); // 若輸入出現異常,Scanner要丟棄上一次的輸入,否則 do-while會出現死循環 s.next(); } } while (!inputIsOk); return inputNum; } }
效果圖:
以上就是“java實現人員信息管理系統的代碼怎么寫”這篇文章的所有內容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。