一:要求
存储的信息包括:ID,姓名、手机号、地址、婚姻状况、生日。
功能:对员工能进行增删查改。
思路:
1)一个带有main方法的类
2)一个雇员的实体类Employee;
3)一个增删查改的类DBcenter;
1)代码:带main方法的类
package com.niit.book; import java.util.Scanner; import com.niit.dbcenter.DBcenter; import com.niit.entity.Employee; /** * @author: Annie * @date:2016年5月15日 * @description:运行程序,调用增删查改方法 */ public class EmployeeMain { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("请输入要存入的雇员人数:"); int number = in.nextInt(); Employee [] employee = new Employee[number]; DBcenter db = new DBcenter(); while(true){ System.out.println("请输入要执行的操作"); System.out.println("------Menu-------"); System.out.println("1.add Data"); System.out.println("2.Update Data"); System.out.println("3.Display Data"); System.out.println("4:delete Date"); System.out.println("5.Exit"); System.out.println("---------"); int num = in.nextInt(); switch(num){ case 1 : db.addDate(employee); break; case 2: db.updateDate(employee); break; case 3: db.displayDate(employee); break; case 4: db.DeleteDate(employee); break; case 5: System.exit(0); break; } } } }
2)代码:实体类
/** * */ package com.niit.entity; /** * @author: Annie * @date:2016年5月15日 * @description:雇员的基本信息 */ public class Employee { private int id; private String name; private String tel; private String addr; private String birthday; private String maritalStatus; public int getID() { return id; } public void setID(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getMaritalStatus() { return maritalStatus; } public void setMaritalStatus(String maritalStatus) { this.maritalStatus = maritalStatus; } public String toString(){ String msg = this.id+"\t"+this.name+"\t"+this.tel+"\t\t"+this.addr+"\t"+this.birthday+"\t"+this.maritalStatus; return msg; } }
3)代码DBcenter
/** * */ package com.niit.dbcenter; import java.util.Scanner; import com.niit.entity.Employee; /** * @author: Annie * @date:2016年5月15日 * @description:对雇员进行增删查改 */ public class DBcenter { Scanner in =new Scanner(System.in); String msg = "编号"+"\t"+"姓名"+"\t"+"手机号"+"\t\t"+"地址"+"\t"+"生日"+"\t"+"婚姻状况"; /*判断数组中是否有空的位置,如果没有,则不装,如果有,则装 * */ public int setIndext(Employee [] arr){ for (int i = 0; i < arr.length; i++) { if(arr[i]==null){ return i; } } return 999; } /*用于输出学生信息,即刷新信息*/ public void print(Employee [] arr){ System.out.println(msg); for (int i = 0; i < arr.length; i++) { //判断是否存在 if(arr[i]!=null){ System.out.println(arr[i]); } } } /*用于判断雇员是否存在*/ public boolean exsit(int id,Employee arr){ if (arr!=null) { if(arr.getID() == id){ return true; }else{ return false; } } return false; } /* 增加雇员*/ public void addDate(Employee[] arr){ System.out.println("请输入雇员的姓名:"); String name = in.next(); System.out.println("请输入雇员的手机号:"); String tel = in.next(); System.out.println("请输入雇员的地址:"); String addr = in.next(); System.out.println("请输入雇员的生日:"); String birthday = in.next(); System.out.println("请输入雇员的婚姻状况"); String maritalStatus = in.next(); Employee employee = new Employee(); employee.setName(name); employee.setAddr(addr); employee.setBirthday(birthday); employee.setTel(tel); employee.setMaritalStatus(maritalStatus); int i = this.setIndext(arr); employee.setID(i); if(i==999){ System.out.println("人数已经达到了最大,不能再添加了"); }else{ arr[i]=employee; this.print(arr); } } /*删除雇员*/ public void DeleteDate(Employee[] arr){ System.out.println("请输入要删除雇员的id:"); int id = in.nextInt(); for (int i = 0; i < arr.length; i++) { if(this.exsit(id, arr[i])){ arr[i]=null; System.out.println("---学生信息删除成功---"); this.print(arr); return; } } System.out.println("你所指定的学生不存在"); } /*查询雇员:通过id来查找*/ public void displayDate(Employee[] arr){ System.out.println("请输入要查询雇员的编号:"); int id =in.nextInt(); for (int i = 0; i < arr.length; i++) { if(this.exsit(id, arr[i])){ System.out.println(arr[id]); return; } } System.out.println("此学生不存在"); } /* 修改雇员:通过id来更新*/ public void updateDate(Employee [] arr){ System.out.println("请输入要更新的雇员id"); int id = in.nextInt(); for (int i = 0; i < arr.length; i++) { if(this.exsit(id, arr[i])){ System.out.println("请输入"+arr[i].getName()+"修改的姓名:"); String name = in.next(); System.out.println("请输入"+arr[i].getName()+"修改的手机号:"); String tel = in.next(); System.out.println("请输入"+arr[i].getName()+"修改的地址:"); String addr = in.next(); System.out.println("请输入"+arr[i].getName()+"修改的生日:"); String birthday = in.next(); System.out.println("请输入"+arr[i].getName()+"修改的婚姻状况"); String maritalStatus = in.next(); arr[i].setName(name); arr[i].setAddr(addr); arr[i].setBirthday(birthday); arr[i].setTel(tel); arr[i].setMaritalStatus(maritalStatus); System.out.println("----更新成功----"); this.print(arr); } } } }
运行结果:
增加雇员信息的运行结果:
更新操作运行结果:
其余的自己运行