本文实例为大家分享了java实现人员信息管理系统的具体代码,供大家参考,具体内容如下
实现增删改查.
java入门的练手小程序
1.Person类
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
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类
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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类
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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类
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
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类
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
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; }
} |
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://blog.csdn.net/u014453898/article/details/79007883