员工管理系统(对象实现)
可以参考本人的博客《面向过程--员工管理系统》
https://blog.csdn.net/qq_43804919/article/details/120969915?spm=1001.2014.3001.5501
对比面向对象和面向过程的区别。
员工类
package com.situ.chapterwork;
public class Employee extends Department {
private String name;
private String sex;
private int age;
private float salary;
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;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
}
部门类
package com.situ.chapterwork;
public class Department {
private int did;
private String dname;
private int sum;
public int getDid() {
return did;
}
public void setDid(int did) {
this.did = did;
}
public String getDName() {
return dname;
}
public void setDName(String dname) {
this.dname = dname;
}
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
}
薪资排序接口
package com.situ.chapterwork;
import java.util.Comparator;
public class MyComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
Employee e1 = (Employee)o1;
Employee e2 = (Employee)o1;
if (e1.getSalary() < e2.getSalary()) {
return -1;
} else if (e1.getSalary() > e2.getSalary()) {
return 1;
} else {
return 0;
}
}
}
主函数main
package com.situ.chapterwork;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List employee = new ArrayList();
while (true) {
System.out.println("************************************************");
System.out.println("* 1.添加 2.修改 3.查询 4.删除 5.排名 6.部门管理 7.退出 *");
System.out.println("************************************************");
System.out.println("请选择您要进行的操作:");
String choice = scanner.next();
if (choice.equals("1")) {
add(scanner, employee);
} else if (choice.equals("2")) {
edit(scanner, employee);
} else if (choice.equals("3")) {
find(scanner, employee);
} else if (choice.equals("4")) {
delete(scanner, employee);
} else if (choice.equals("5")) {
sort(employee);
} else if (choice.equals("6")) {
departmentmannger(employee);
}
else if (choice.equals("7")) {
System.out.println("感谢使用本系统");
break;
}
}
}
private static void departmentmannger(List employee) {
Scanner scanner = new Scanner(System.in);
while(true) {
System.out.println("1.部门查询 2.部门修改 3.返回上层");
System.out.println("请输入操作:");
int choice = scanner.nextInt();
if(choice == 1) {
findment(employee);
}else if(choice == 2) {
editment(employee);
}else if(choice == 3) {
break;
}
}
}
private static void editment(List employee) {
while(true) {
Scanner sc = new Scanner(System.in);
System.out.println("1.修改部门号2.修改部门名 3.返回上层");
System.out.println("请输入操作:");
int flag = 0;
int choice = sc.nextInt();
if(choice == 1) {
int count = employee.size();
System.out.println("输入查找部门号:");
int id = sc.nextInt();
for(int i = 0;i<count;i++) {
Employee e = (Employee)employee.get(i);
if(e.getDid() == id) {
System.out.println("输入部门号:");
int eid = sc.nextInt();
e.setDid(eid);
printEmployeeInfo(e);
flag++;
}
}
if(flag == 0) {
System.out.println("没有此部门号!");
}else {
System.out.println("修改成功!");
}
}else if(choice == 2) {
int count = employee.size();
System.out.println("输入查找部门名:");
String name= sc.next();
for(int i = 0;i<count;i++) {
Employee e = (Employee)employee.get(i);
if(e.getDName().equals(name)) {
System.out.println("输入部门名:");
String ename = sc.next();
e.setDName(ename);
printEmployeeInfo(e);
flag++;
}
}
if(flag == 0) {
System.out.println("没有此部门号!");
}else {
System.out.println("修改成功!");
}
}else if(choice ==3) {
break;
}
}
}
private static void findment(List employee) {
while(true) {
Scanner sc = new Scanner(System.in);
System.out.println("1.按部门号查找 2.按部门名查找 3.返回上层");
System.out.println("请输入操作:");
int choice = sc.nextInt();
int count = employee.size();
if(choice == 1) {
int flag = 0;
System.out.println("输入部门号:");
int id =sc.nextInt();
for(int i=0;i<count;i++) {
Employee e = (Employee)employee.get(i);
if(e.getDid()==id) {
printEmployeeInfo(e);
flag++;
}
}
if(flag == 0) {
System.out.println("没有找到此部门!");
}else {
System.out.println(id+"部门共有"+flag+"人");
}
}else if(choice == 2) {
int flag = 0;
System.out.println("输入部门名:");
String name =sc.next();
for(int i=0;i<count;i++) {
Employee e = (Employee)employee.get(i);
if(e.getDName().equals(name)) {
printEmployeeInfo(e);
flag++;
}
}
if(flag == 0) {
System.out.println("没有找到此部门!");
}else {
System.out.println(name+"部门共有"+flag+"人");
}
}else if(choice == 3) {
break;
}
}
}
public static void add(Scanner scanner, List employee) {
while (true) {
System.out.println("====您现在执行的操作是添加员工====");
System.out.println("请输入员工姓名:");
String name = scanner.next();
System.out.println("请输入员工性别:");
String sex = scanner.next();
System.out.println("请输入员工年龄:");
int age = scanner.nextInt();
System.out.println("请输入员工部门号:");
int did = scanner.nextInt();
System.out.println("请输入员工部门名:");
String mname = scanner.next();
System.out.println("请输入员工工资:");
float salary = scanner.nextFloat();
Employee e = new Employee();
e.setName(name);
e.setAge(age);
e.setSex(sex);
e.setSalary(salary);
e.setDid(did);
e.setDName(mname);
employee.add(e);
System.out.println("录入成功,是否继续?y/n");
String choice = scanner.next();
if (choice.equals("y")) {
continue;
} else {
break;
}
}
}
public static void find(Scanner scanner, List employee) {
System.out.println("====您现在执行的操作是查询====");
int count = employee.size();
int max = count < 5 ? count : 5;
for (int i = 0; i < max; i++) {
Employee e = (Employee)employee.get(i);
printEmployeeInfo(e);
}
if (count > 5) {
while (true) {
System.out.println("当前总员工数:" + count);
System.out.println("1.展示全部 2.根据姓名查询 3.根据年龄查询 4.返回上一层");
String choice = scanner.next();
if (choice.equals("1")) {
for (int i = 0; i < count; i++) {
Employee e = (Employee)employee.get(i);
printEmployeeInfo(e);
}
} else if (choice.equals("2")) {
String name = scanner.next();
for (int i = 0; i < count; i++) {
Employee e = (Employee)employee.get(i);
if (e.getName().equals(name)) {
printEmployeeInfo(e);
}
}
} else if (choice.equals("3")) {
int age = scanner.nextInt();
for (int i = 0; i < count; i++) {
Employee e = (Employee)employee.get(i);
if (e.getAge() == age) {
printEmployeeInfo(e);
}
}
} else if (choice.equals("4")) {
break;
}
}
}
}
public static void delete(Scanner scanner, List employee) {
while (true) {
System.out.println("====您现在执行的操作是删除====");
System.out.println("请输入您要删除的员工姓名:");
int count = employee.size();
String name = scanner.next();
int id = 1;
List forDelete = new ArrayList();
for (int i = 0; i < count; i++) {
Employee e = (Employee)employee.get(i);
if (e.getName().equals(name)) {
forDelete.add(e);
printEmployeeInfo(e);
id++;
}
}
if (id == 1) {
System.out.println("未找到您要删除的员工");
} else {
System.out.println("请输入您要删除的员工序号:");
int choice = scanner.nextInt();
Employee deleteEmp = (Employee)forDelete.get(choice - 1);
employee.remove(deleteEmp);
System.out.println("删除成功,是否继续删除?y/n");
String choose = scanner.next();
if (choose.equals("y")) {
continue;
} else {
break;
}
}
}
}
public static void sort(List employee) {
List newEmployee = new ArrayList();
newEmployee.addAll(employee);
for (int i = 0; i < newEmployee.size() - 1; i++) {
for (int j = i + 1; j < newEmployee.size(); j++) {
Employee ej = (Employee)newEmployee.get(j);
Employee ei = (Employee)newEmployee.get(i);
if (ej.getSalary() > ei.getSalary()) {
Employee t = ei;
newEmployee.set(i, ej);
newEmployee.set(j, t);
}
}
}
for (int i = 0; i < employee.size(); i++) {
Employee e = (Employee)employee.get(i);
printEmployeeInfo(e);
}
}
public static void edit(Scanner scanner, List employee) {
while (true) {
System.out.println("====您现在执行的操作是修改====");
System.out.println("请输入您要修改的员工姓名:");
String name = scanner.next();
int id = 1;
List forEdit = new ArrayList();
for (int i = 0; i < employee.size(); i++) {
Employee e = (Employee)employee.get(i);
if (e.getName().equals(name)) {
forEdit.add(e);
System.out.println(id + ". 姓名:" + e.getName() + " 性别:" + e.getSex() + " 年龄:" + e.getAge() + " 部门:"+e.getDName()+" 工资:"
+ e.getSalary());
id++;
}
}
if (id == 1) {
System.out.println("未找到您要修改的员工");
} else {
System.out.println("请输入您要修改的员工序号:");
int choice = scanner.nextInt();
Employee editEmp = (Employee)forEdit.get(choice - 1);
System.out.println("请输入员工姓名:");
String newName = scanner.next();
System.out.println("请输入员工性别:");
String newSex = scanner.next();
System.out.println("请输入员工年龄:");
int newAge = scanner.nextInt();
System.out.println("请输入员工部门号:");
int newDid = scanner.nextInt();
System.out.println("请输入员工部门:");
String newMent = scanner.next();
System.out.println("请输入员工工资:");
float newSalary = scanner.nextFloat();
editEmp.setName(newName);
editEmp.setSex(newSex);
editEmp.setAge(newAge);
editEmp.setDid(newDid);
editEmp.setDName(newMent);
editEmp.setSalary(newSalary);
System.out.println("修改成功,是否继续?y/n");
String choose = scanner.next();
if (choose.equals("y")) {
continue;
} else {
break;
}
}
}
}
public static void printEmployeeInfo(Employee e) {
System.out.println("姓名:" + e.getName() + " 性别:" + e.getSex() + " 年龄:" + e.getAge() +" 部门号:"+e.getDid()+" 部门:"+e.getDName()+ " 工资:" + e.getSalary());
}
}