java学习代码练习,简单易懂
员工实体类Employee
package lx1025;
public class Employee {
private String name;
private String sex;
private int age;
private float salary; //薪资
private String did; //所在部门编号
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;
}
public String getDid() {
return did;
}
public void setDid(String did) {
this.did = did;
}
}
员工管理模块类Personnel
写明操作员工的具体实现
package lx1025;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
//员工管理模块
public class Personnel {
private List<Employee> emlist = new ArrayList<Employee>();//员工对象集合,存放员工信息
//添加员工
public void addEmployee(Scanner cin, DPMSystem dpm) {
System.out.print("姓名:");
String addname = cin.next();
System.out.print("性别:");
String addsex = cin.next();
System.out.print("年龄:");
int addage = cin.nextInt();
System.out.print("工资:");
float addsalary = cin.nextFloat();
boolean b = true;
for (int i = 0; i < emlist.size(); i++) {
if (emlist.get(i).getName().equals(addname)) {
b = false;
break;
}
}
if (b) {
Employee em = new Employee();
em.setName(addname);
em.setSex(addsex);
em.setAge(addage);
em.setSalary(addsalary);
System.out.print("是否为该员工分配部门?(Y/N)");
String m = cin.next();
if (m.equals("Y") || m.equals("y")) {
dpm.dpmselectall();
while (true) {
System.out.print("请输入部门编号:");
String did = cin.next();
int n = dpm.dpmgetbyid(did);
if (n != -1) {
em.setDid(did);
dpm.getDpmlist().get(n).setNum(dpm.getDpmlist().get(n).getNum() + 1);
break;
} else {
System.out.println("输入错误,部门分配失败!");
System.out.println("是否重新输入?(Y/N)");
String s = cin.next();
if (s.equals("Y") || s.equals("y")) {
continue;
} else {
break;
}
}
}
}
emlist.add(em);
System.out.println(addname + "添加成功!");
} else {
System.out.println("请不要重复添加!");
}
}
//删除员工
public void subEmployee(Scanner cin, DPMSystem dpm) {
System.out.print("请输入要删除的姓名:");
String subname = cin.next();
int j = 0;
boolean b = false;
for (int i = 0; i < emlist.size(); i++) {
if (emlist.get(i).getName().equals(subname)) {
j = i;
b = true;
break;
}
}
if (b) {
int n = dpm.dpmgetbyid(emlist.get(j).getDid());
if (n != -1) {
dpm.getDpmlist().get(n).setNum(dpm.getDpmlist().get(n).getNum() - 1);
}
emlist.remove(j);
System.out.println(subname + "删除成功!");
} else {
System.out.println("查无此人!");
}
}
//修改员工
public void updateEmployee(Scanner cin, DPMSystem dpm) {
System.out.print("请输入要修改的姓名:");
String oldname = cin.next();
int j = 0;
boolean b = false;
for (int i = 0; i < emlist.size(); i++) {
if (emlist.get(i).getName().equals(oldname)) {
j = i;
b = true;
break;
}
}
if (b) {
System.out.print("姓名:" + emlist.get(j).getName() + " " + "性别:" + emlist.get(j).getSex() + " " + "年龄:" + emlist.get(j).getAge() + " " + "工资:" + emlist.get(j).getSalary());
int n = dpm.dpmgetbyid(emlist.get(j).getDid());
if (n != -1) {
String dname = dpm.getDpmlist().get(n).getDname();
System.out.print(" " + "部门:" + dname);
System.out.println();
} else {
System.out.print(" " + "部门:");
System.out.println();
}
System.out.println("1.修改姓名 2.修改性别 3.修改年龄 4.修改工资 5.修改部门");
int m = cin.nextInt();
switch (m) {
case 1: {
System.out.print("修改后姓名:");
String newname = cin.next();
emlist.get(j).setName(newname);
System.out.println("修改成功!");
break;
}
case 2: {
System.out.print("修改后性别:");
String newsex = cin.next();
emlist.get(j).setSex(newsex);
System.out.println("修改成功!");
break;
}
case 3: {
System.out.print("修改后年龄:");
int newage = cin.nextInt();
emlist.get(j).setAge(newage);
System.out.println("修改成功!");
break;
}
case 4: {
System.out.print("修改后工资:");
Float newsalary = cin.nextFloat();
emlist.get(j).setSalary(newsalary);
System.out.println("修改成功!");
break;
}
case 5: {
dpm.dpmselectall();
while (true) {
System.out.print("请输入部门编号:");
String did = cin.next();
int t = dpm.dpmgetbyid(did);
if (t != -1) {
if (t != n) {
if (n != -1) {
dpm.getDpmlist().get(n).setNum(dpm.getDpmlist().get(n).getNum() - 1);
}
dpm.getDpmlist().get(t).setNum(dpm.getDpmlist().get(t).getNum() + 1);
emlist.get(j).setDid(did);
System.out.println("修改成功!");
}
break;
} else {
System.out.println("输入错误,部门分配失败!");
System.out.println("是否重新输入?(Y/N)");
String s = cin.next();
if (s.equals("Y") || s.equals("y")) {
continue;
} else {
break;
}
}
}
break;
}
default: {
System.out.println("输入错误!");
}
}
} else {
System.out.println("查无此人!");
}
}
//查询员工
public void selectEmployee(Scanner cin, DPMSystem dpm) {
System.out.print("请输入要查询的姓名:");
String selectname = cin.next();
int j = 0;
boolean b = false;
for (int i = 0; i < emlist.size(); i++) {
if (emlist.get(i).getName().equals(selectname)) {
j = i;
b = true;
break;
}
}
if (b) {
System.out.print("姓名:" + emlist.get(j).getName() + " " + "性别:" + emlist.get(j).getSex() + " " + "年龄:" + emlist.get(j).getAge() + " " + "工资:" + emlist.get(j).getSalary());
int n = dpm.dpmgetbyid(emlist.get(j).getDid());
if (n != -1) {
String dname = dpm.getDpmlist().get(n).getDname();
System.out.print(" " + "部门:" + dname);
System.out.println();
} else {
System.out.print(" " + "部门:");
System.out.println();
}
} else {
System.out.println("查无此人!");
}
}
//工资排名
public void salarysort(DPMSystem dpm) {
for (int i = 0; i < emlist.size() - 1; i++) {
int m = i;
for (int j = i + 1; j < emlist.size(); j++) {
if (emlist.get(j).getSalary() > emlist.get(m).getSalary()) {
m = j;
}
}
if (m != i) {
Employee e1 = emlist.get(m);
emlist.set(m, emlist.get(i));
emlist.set(i, e1);
}
}
for (int i = 0; i < emlist.size(); i++) {
System.out.print("姓名:" + emlist.get(i).getName() + " " + "性别:" + emlist.get(i).getSex() + " " + "年龄:" + emlist.get(i).getAge() + " " + "工资:" + emlist.get(i).getSalary());
int n = dpm.dpmgetbyid(emlist.get(i).getDid());
if (n != -1) {
String dname = dpm.getDpmlist().get(n).getDname();
System.out.print(" " + "部门:" + dname);
System.out.println();
} else {
System.out.print(" " + "部门:");
System.out.println();
}
}
}
public List<Employee> getEmlist() {
return emlist;
}
}
部门实体类Department
package lx1025;
//部门
public class Department {
private String did; //部门编号
private String dname; //部门名称
private int num = 0; //员工数
public Department(String did, String dname) {
this.did = did;
this.dname = dname;
}
public String getDid() {
return did;
}
public void setDid(String did) {
this.did = did;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
部门管理模块类DPMSystem
写明操作部门的具体实现
package lx1025;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
//部门管理模块
public class DPMSystem {
private List<Department> dpmlist = new ArrayList<Department>();//部门对象集合,存放部门对象
//新建部门
public void dpmadd(Scanner cin) {
while (true) {
System.out.print("请输入部门编号:");
String did = cin.next();
System.out.print("请输入部门名称:");
String dname = cin.next();
int n = dpmgetbyid(did);
if (n == -1) {
Department dpm = new Department(did, dname);
dpmlist.add(dpm);
System.out.println(dname + "部门创建成功!");
break;
} else {
System.out.println("编号为" + did + "的部门已存在!");
String m = "";
System.out.print("是否继续输入?(Y/N)");
m = cin.next();
if (m.equals("Y") || m.equals("y")) {
continue;
} else {
break;
}
}
}
}
//删除部门
public void dpmdelete(Scanner cin, Personnel ps) {
dpmselectall();
while (true) {
System.out.print("请输入要删除的部门编号:");
String did = cin.next();
int n = dpmgetbyid(did);
if (n != -1) {
String m = "";
System.out.print("是否将部门成员一并删除?(Y/N)");
m = cin.next();
if (m.equals("Y") || m.equals("y")) {
for (int i = 0; i < ps.getEmlist().size(); i++) {
if (ps.getEmlist().get(i).getDid().equals(did)) {
ps.getEmlist().get(i).setDid(null);
}
}
}
dpmlist.remove(n);
System.out.println("部门删除成功!");
break;
} else {
System.out.println("输入错误/该部门不存在!");
String s = "";
System.out.print("是否继续输入?(Y/N)");
s = cin.next();
if (s.equals("Y") || s.equals("y")) {
continue;
} else {
break;
}
}
}
}
//修改部门
public void dpmupdate(Scanner cin) {
dpmselectall();
while (true) {
System.out.print("请输入要修改的部门编号:");
String did = cin.next();
int n = dpmgetbyid(did);
if (n != -1) {
System.out.println("部门编号:" + dpmlist.get(n).getDid() + " " + "部门名称:" + dpmlist.get(n).getDname());
System.out.println("1.修改编号 2.修改名称");
int s = cin.nextInt();
if (s == 1) {
while (true) {
System.out.print("请输入新编号:");
String newdid = cin.next();
int k = dpmgetbyid(newdid);
if (k != -1 && k != n) {
System.out.println("该编号已存在,请重新输入!");
continue;
} else {
dpmlist.get(n).setDid(newdid);
System.out.println("部门编号修改成功!");
break;
}
}
} else {
System.out.print("请输入新名称:");
String newdname = cin.next();
dpmlist.get(n).setDname(newdname);
System.out.println("部门名称修改成功!");
}
break;
} else {
System.out.println("输入错误/该部门不存在!");
String m = "";
System.out.print("是否继续输入?(Y/N)");
m = cin.next();
if (m.equals("Y") || m.equals("y")) {
continue;
} else {
break;
}
}
}
}
//查询部门
public void dpmselect(Scanner cin) {
System.out.print("请输入要查询的部门名称:");
String dname = cin.next();
int n = 0;
for (int i = 0; i < dpmlist.size(); i++) {
if (dpmlist.get(i).getDname().equals(dname)) {
System.out.println("部门编号:" + dpmlist.get(i).getDid() + " " + "部门名称:" + dpmlist.get(i).getDname() + " " + "员工数:" + dpmlist.get(i).getNum());
n++;
}
}
if (n != 0) {
System.out.println("一共查到" + n + "条结果!");
} else {
System.out.println("未查到相关信息!");
}
}
//通过编号(did)查询部门,返回其在集合中的索引
public int dpmgetbyid(String did) {
int n = -1;
for (int i = 0; i < dpmlist.size(); i++) {
if (dpmlist.get(i).getDid().equals(did)) {
n = i;
break;
}
}
return n;
}
//显示所有部门
public void dpmselectall() {
for (int i = 0; i < dpmlist.size(); i++) {
System.out.println("部门编号:" + dpmlist.get(i).getDid() + " " + "部门名称:" + dpmlist.get(i).getDname() + " " + "员工数:" + dpmlist.get(i).getNum());
}
}
public List<Department> getDpmlist() {
return dpmlist;
}
}
模块管理类Modules
创建各个模块的对象,调用其中的操作函数,处理模块间的调用关系
package lx1025;
import java.util.Scanner;
//模块管理
public class Modules {
private Personnel ps = new Personnel();
private DPMSystem dpm = new DPMSystem();
//员工管理模块
public void openps(Scanner cin) {
while (true) {
System.out.println("******************************************");
System.out.println("* 1.添加员工 2.修改员工 3.查询员工 *");
System.out.println("* 4.删除员工 5.工资排名 6.退出系统 *");
System.out.println("******************************************");
int n = cin.nextInt();
if (n == 1) {
ps.addEmployee(cin, dpm);
} else if (n == 2) {
ps.updateEmployee(cin, dpm);
} else if (n == 3) {
ps.selectEmployee(cin, dpm);
} else if (n == 4) {
ps.subEmployee(cin, dpm);
} else if (n == 5) {
ps.salarysort(dpm);
} else if (n == 6) {
break;
} else {
System.out.println("输入错误,请输入正确指令!");
}
}
System.out.println("已退出人事管理系统!");
}
//部门管理模块
public void opendpm(Scanner cin) {
while (true) {
System.out.println("************************************************");
System.out.println("* 1.新建部门 2.删除部门 3.修改部门 *");
System.out.println("* 4.查询部门 5.退出系统 *");
System.out.println("************************************************");
int n = cin.nextInt();
if (n == 1) {
dpm.dpmadd(cin);
} else if (n == 2) {
dpm.dpmdelete(cin, ps);
} else if (n == 3) {
dpm.dpmupdate(cin);
} else if (n == 4) {
dpm.dpmselect(cin);
} else if (n == 5) {
break;
} else {
System.out.println("输入错误,请输入正确指令!");
}
}
System.out.println("已退出部门管理系统!");
}
}
程序主函数,系统入口STSystem
面向模块管理Modules对象,调用各个模块入口函数
package lx1025;
import java.util.Scanner;
public class STSystem {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
Modules modules = new Modules();
System.out.println("******************************************");
System.out.println("* 欢迎使用员工管理系统 *");
while (true) {
System.out.println("******************************************");
System.out.println("* 1.人事管理 2.部门管理 3.退出系统 *");
System.out.println("******************************************");
int n = cin.nextInt();
if (n == 1) {
modules.openps(cin);
} else if (n == 2) {
modules.opendpm(cin);
} else if (n == 3) {
break;
} else {
System.out.println("输入错误,请输入正确指令!");
}
}
System.out.println("已退出系统!");
}
}