今天小编给大家分享一下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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。