- Time: Two 0 Two 0.5.18
- Note: I haven't typed the code seriously for a long time. I started to move myself when I finished a search of addition, deletion and modification
subject
Realization ideas
I'm afraid that the repetition rate of homework is too high, so I won't put it up first~
Implementation code
1. Main.java package exp_04; import exp_04.dao.EmployeeDao; import java.util.Scanner; import static java.lang.System.exit; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); EmployeeDao employeeDao = new EmployeeDao(); while (true){ System.out.println("************ Employee information management system ************"); System.out.println("1.Entry 2.Delete 3.Amendment 4.Output 0.sign out"); System.out.println("*****************************************"); System.out.print("Please select:"); int key = scan.nextInt(); switch (key){ case 1: // Entry employeeDao.input(); break; case 2: // delete employeeDao.delete(); break; case 3: // modify employeeDao.update(); break; case 4: // output employeeDao.output(); break; case 0: exit(0); break; default: break; } } } }
2. Employee.java package exp_04.javabean; public class Employee { private String id; private String name; private String sex; private int age; static int num; //Represents the true size of an array public Employee(){} public Employee(String id, String name, String sex, int age){ this.id = id; this.name = name; this.sex = sex; this.age = age; } public String toString() { return "Employee{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", sex='" + sex + '\'' + ", age=" + age + '}'; } public String getId() { return id; } public void setId(String id) { this.id = id; } 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; } }
3. EmployeeDao.java package exp_04.dao; import exp_04.javabean.Employee; import java.util.Scanner; public class EmployeeDao { Scanner scan = new Scanner(System.in); int num = 0; //Represents the true size of an array Employee[] e = new Employee[10000]; /* Entry */ public void input(){ while (true) { System.out.print("Employee No.:"); String id = scan.next(); if(id.equals("000")) { break; } System.out.print("full name:"); String name = scan.next(); System.out.print("Gender:"); String sex = scan.next(); System.out.print("Age:"); int age = scan.nextInt(); Employee employee = new Employee(id,name,sex,age); e[num++] = employee; //Use E [num] directly here= scan.next () is unsuccessful } } /* delete */ public int delete(){ System.out.print("Please enter the employee number to delete:"); String deleteId = scan.next(); int flag = 0; for (int i = 0; i < num; i++) { if(e[i].getId().equals(deleteId)){ num--; flag++; if(i == num-1) break; for (int j = i; j < num; j++) { e[j]=e[j+1]; } } } if (flag == 0) System.out.println("The employee does not exist!"); else System.out.println("Delete successfully!"); return flag; } /* modify */ public void update(){ System.out.println("****** modify ******"); System.out.println("1.appoint 2.whole"); System.out.println("******************"); System.out.print("Please select:"); int key = scan.nextInt(); int flag = 0; //Modify an employee's name if(key == 1){ System.out.print("Please enter the employee number to be modified:"); String updateId = scan.next(); for (int i = 0; i < num; i++) { if(e[i].getId().equals(updateId)){ flag++; System.out.print("full name:"); e[i].setName(scan.next()); } } if(flag == 0) System.out.println("The employee does not exist!"); else System.out.println("Modification succeeded!"); } //Modify the age of all employees else if(key == 2){ for (int i = 0; i < num; i++) { int tempAge = e[i].getAge(); e[i].setAge(tempAge+1); } /*This way of writing is a null pointer for(Employee i : e){ i.setAge(i.getAge()+1); }*/ /*Modify the age of all employees for (int i = 0; i < num; i++) { System.out.print("Please enter the age of the "+ (i+1) +" Employee: "; e[i].setAge(scan.nextInt()); }*/ System.out.println("Modification succeeded!"); } } /* output */ public void output(){ System.out.println("****** output ******"); System.out.println("1.Original array 2.sort"); System.out.println("******************"); System.out.print("Please select:"); while(true){ int key = scan.nextInt(); //Original array if(key == 1){ System.out.println("********** Original array ***********"); System.out.println("Employee No\t full name\t Gender\t Age"); for (int i = 0; i < num; i++) { System.out.println(e[i].getId() + "\t\t" + e[i].getName() + "\t\t" + e[i].getSex() + "\t\t" + e[i].getAge()); } } //Sorted array else if(key == 2){ sort(e); System.out.println("********** Sorted array ***********"); System.out.println("Employee No\t full name\t Gender\t Age"); for (int i = 0; i < num; i++) { System.out.println(e[i].getId() + "\t\t" + e[i].getName() + "\t\t" + e[i].getSex() + "\t\t" + e[i].getAge()); } } //sign out else{ break; } } } /* sort */ public void sort(Employee[] e){ Employee t = new Employee(); for (int i = 0; i < num ; i++) { for (int j = i+1; j < num; j++) { //Str1.compareTo(Str2); //It returns a value of type int. If Str1 is equal to the parameter string Str2 string, 0 is returned; // If the Str1 is less than the parameter string Str2 in dictionary order, the return value is less than 0; if the Str1 is greater than the parameter string Str2 in dictionary order, the return value is greater than 0. if(e[i].getId().compareTo(e[j].getId())>0){ t = e[i]; e[i] = e[j]; e[j] = t; } } } } }
Implementation result chart
I'm also worried that the repetition rate is too high. I'm also worried that I'll be lazy after I take my pocket, so I won't put it on first.
It's better to write the code by yourself, just for reference.