目录
资源分享:
尚硅谷:
链接:https://pan.baidu.com/s/1BimdaZGrlChLs6iQaCidPw
提取码:y8j5
--来自百度网盘超级会员V6的分享
「Project3(开发团队人员调度软件)」https://www.aliyundrive.com/s/F3Ep6PbaMqg 提取码: c6x9 点击链接保存,或者复制本段内容,打开「阿里云盘」APP ,无需下载极速在线查看,视频原画倍速播放。
我实现的:
「myproject3」https://www.aliyundrive.com/s/iG9nnuAjBUr 提取码: c6x9 点击链接保存,或者复制本段内容,打开「阿里云盘」APP ,无需下载极速在线查看,视频原画倍速播放。
一、主要涉及以下知识点
类的继承性和多态性
对象的值传递、接口
static和final修饰符
特殊类的使用:包装类、抽象类、内部类
异常处理
二、需求说明
该软件实现以下功能:
软件启动时,根据给定的数据创建公司部分成员列表(数组)
根据菜单提示,基于现有的公司成员,组建一个开发团队以开发一个新的项目 组建过程包括将成员插入到团队中,或从团队中删除某成员,还可以列出团队中现有成员的列表
开发团队成员包括架构师、设计师和程序员
三、软件结构设计
team.view模块为主控模块,负责菜单的显示和处理用户操作
team.service模块为实体对象(Employee及其子类如程序员等)的管理模块, NameListService和TeamService类分别用各自的数组来管理公司员工和开发团队成员对象
domain模块为Employee及其子类等JavaBean类所在的包
四、具体实现
项目结构图:
junit包中的类是自定义的用来测试的类,完成一部分的代码之后可以在这里测试一下。
1. 创建项目基本组件
1. 完成以下工作:
1.1 创建TeamSchedule项目
1.2 按照设计要求,创建所有包
1.3 将项目提供的几个类复制到相应的包中(view包中TSUtility.java; service包中:Data.java)
2 按照设计要求,在team.domain包中,创建Equipment接口及其各实现子类代码
3. 按照设计要求,在team.domain包中,创建Employee类及其各子类代码
4. 检验代码的正确性
Equipment接口:
package team.domain;
/**
* @BelongsProject: myproject3
* @BelongsPackage: team.domain
* @Classname: Equipment
* @Author: admin
* @Date: 2022/4/20 9:35
* @Description:
*/
public interface Equipment {
String getDescription();
}
Notebook:
package team.domain;
/**
* @BelongsProject: myproject3
* @BelongsPackage: team.domain
* @Classname: NoteBook
* @Author: admin
* @Date: 2022/4/20 9:46
* @Description:
*/
public class NoteBook implements Equipment {
private String model;//机器型号
private double price;//价格
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public NoteBook() {
}
public NoteBook(String model, double price) {
this.model = model;
this.price = price;
}
@Override
public String getDescription() {
return model + "(" + price + ")";
}
}
PC:
package team.domain;
/**
* @BelongsProject: myproject3
* @BelongsPackage: team.domain
* @Classname: PC
* @Author: admin
* @Date: 2022/4/20 9:41
* @Description:
*/
public class PC implements Equipment {
private String model;//机器型号
private String display;//显示器名称
public PC() {
}
public PC(String model, String display) {
this.model = model;
this.display = display;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getDisplay() {
return display;
}
public void setDisplay(String display) {
this.display = display;
}
@Override
public String getDescription() {
return model + "(" + display + ")";
}
}
Printer:
package team.domain;
/**
* @BelongsProject: myproject3
* @BelongsPackage: team.domain
* @Classname: Printer
* @Author: admin
* @Date: 2022/4/20 9:52
* @Description:
*/
public class Printer implements Equipment {
private String name;//机器型号
private String type;//机器类型
public String getName(){
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType(){
return type;
}
public void setType(String type){
this.type = type;
}
public Printer(){
super();
}
public Printer(String name,String type){
super();
this.name = name;
this.type = type;
}
@Override
public String getDescription(){
return name + "(" + type + ")";
}
}
Status枚举类位于com.atguigu.team.service包中,封装员工的状态。代码如下
package team.service;
import javax.print.attribute.standard.MediaSize;
/**
* @BelongsProject: myproject3
* @BelongsPackage: team.service
* @Classname: Status
* @Author: admin
* @Date: 2022/4/20 10:31
* @Description: 表示员工的状态
*/
public class Status {
private final String NAME;
private Status(String name){
this.NAME = name;
}
//枚举
public static final Status FREE = new Status("FREE");
public static final Status BUSY = new Status("BUSY");
public static final Status VOCATION = new Status("VOCATION");
public String getNAME() {
return NAME;
}
@Override
public String toString() {
return NAME;
}
}
Employee表示所有员工的父类,他有三个子类,programmer,designer,architect。
具体关系为:Programmer是Employee的子类,Designer是programmer的子类,Architect是designer的子类。
Employee:
package team.domain;
/**
* @BelongsProject: myproject3
* @BelongsPackage: team.domain
* @Classname: Employee
* @Author: admin
* @Date: 2022/4/20 10:27
* @Description:
*/
public class Employee {
private int id;
private String name;
private int age;
private double salary;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Employee(){
super();
}
public Employee(int id, String name, int age, double salary) {
super();
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public String GetDetials(){
return id + "\t" + name + "\t" + age + "\t" + salary;
}
@Override
public String toString() {
return GetDetials();
}
}
Programmer:
package team.domain;
import team.service.Status;
/**
* @BelongsProject: myproject3
* @BelongsPackage: team.domain
* @Classname: Programmer
* @Author: admin
* @Date: 2022/4/20 10:38
* @Description:
*/
public class Programmer extends Employee{
private int memberId;//开发团队的ID
private Status status = Status.FREE;
private Equipment equipment;
public Programmer() {
super();
}
public Programmer(int id, String name, int age, double salary,Equipment equipment) {
super(id, name, age, salary);
// this.status = status;
this.equipment = equipment;
}
public int getMemberId() {
return memberId;
}
public void setMemberId(int memberId) {
this.memberId = memberId;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public Equipment getEquipment() {
return equipment;
}
public void setEquipment(Equipment equipment) {
this.equipment = equipment;
}
@Override
public String toString() {
return GetDetials() + "\t程序员\t" + status + "\t\t\t" + equipment.getDescription();
}
public String getTeamBaseDetails(){
return memberId + "/" + getId() + "\t" + getName() + "\t" + getAge() + "\t" + getSalary();
}
public String getDetailsForTeam() {
return getTeamBaseDetails() + "\t程序员";
}
}
Designer:
package team.domain;
/**
* @BelongsProject: myproject3
* @BelongsPackage: team.domain
* @Classname: Designer
* @Author: admin
* @Date: 2022/4/20 10:44
* @Description:
*/
public class Designer extends Programmer {
private double bonus;
public Designer() {
super();
}
public Designer(int id, String name, int age, double salary, Equipment equipment, double bonus) {
super(id, name, age, salary, equipment);
this.bonus = bonus;
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
@Override
public String toString() {
return GetDetials() + "\t设计师\t" + getStatus() + "\t" + bonus + "\t\t" + getEquipment().getDescription();
}
public String getDetailsForTeam(){
return getTeamBaseDetails() + "\t设计师" + getBonus();
}
}
Architect:
package team.domain;
/**
* @BelongsProject: myproject3
* @BelongsPackage: team.domain
* @Classname: Architect
* @Author: admin
* @Date: 2022/4/20 10:45
* @Description:
*/
public class Architect extends Designer {
private double stock;
public Architect() {
super();
}
public Architect(int id, String name, int age, double salary, Equipment equipment, double bonus, double stock) {
super(id, name, age, salary, equipment, bonus);
this.stock = stock;
}
public double getStock() {
return stock;
}
public void setStock(double stock) {
this.stock = stock;
}
@Override
public String toString() {
return GetDetials() + "\t架构师\t" + getStatus() + "\t" + getBonus() + "\t" + stock + "\t" + getEquipment().getDescription();
}
public String getDetailsForTeam(){
return getTeamBaseDetails() + "\t架构师" + getBonus() + "\t" + getStock();
}
}
2.实现service包中的类
自定义异常类:TeamException
package team.service;
/**
* @BelongsProject: myproject3
* @BelongsPackage: team.service
* @Classname: TeamException
* @Author: admin
* @Date: 2022/4/20 15:19
* @Description:
*/
public class TeamException extends Exception {
static final long serialVersionUID = -3387514229948L;//随便选,别和系统自带的重合就行
public TeamException() {
super();
}
public TeamException(String message) {
super(message);
}
}
NameListService:
package team.service;
import team.domain.*;
import static team.service.Data.*;
//import team.service.Data.*;
/**
* @BelongsProject: myproject3
* @BelongsPackage: team.service
* @Classname: NameListService
* @Author: admin
* @Date: 2022/4/20 10:48
* @Description: 负责将Data中的数据封装到Employee数组中,
* 同时提供相关操作Employee的方法
*/
public class NameListService {
private Employee[] employees;
/**
* @author: admin
* @date: 2022/4/20 10:57
* @description:
* //根据项目提供的Data类构建相应大小的employees[]数组
* //再根据Data类中的数据构建不同的对象,包括Employee、Programmer、Designer和Architect对象,
* //以及相关联的Equipment子类的对象. 将对象存于数组中 Data类位于com.atguigu.team.service包中
* @return: null
*/
public NameListService(){
employees = new Employee[Data.EMPLOYEES.length];
for (int i=0;i<employees.length;i++){
//获取Employee的4个基本信息
int id = Integer.parseInt(Data.EMPLOYEES[i][1]);
String name = Data.EMPLOYEES[i][2];
int age = Integer.parseInt(Data.EMPLOYEES[i][3]);
Double salary = Double.parseDouble(Data.EMPLOYEES[i][4]);
Equipment equipment;
double bonus;
int stock;
int type = Integer.parseInt(Data.EMPLOYEES[i][0]);
switch (type){
case EMPLOYEE:
employees[i] = new Employee(id,name,age,salary);
break;
case PROGRAMMER:
equipment = creatEquipment(i);
employees[i] = new Programmer(id,name,age,salary,equipment);
break;
case DESIGNER:
equipment = creatEquipment(i);
bonus = Double.parseDouble(EMPLOYEES[i][5]);
employees[i] = new Designer(id,name,age,salary,equipment,bonus);
break;
case ARCHITECT:
equipment = creatEquipment(i);
bonus = Double.parseDouble(EMPLOYEES[i][5]);
stock = Integer.parseInt(EMPLOYEES[i][6]);
employees[i] = new Architect(id,name,age,salary,equipment,bonus,stock);
break;
}
}
}
/**
* @author: admin
* @date: 2022/4/20 11:22
* @description: 获取指定员工index的设备
* @Param index:
* @return: team.domain.Equipment
*/
private Equipment creatEquipment(int index){
int key = Integer.parseInt(Data.EQUIPMENTS[index][0]);
String modelOrName = EQUIPMENTS[index][1];
switch (key){
case PC://PC代表常量21
String display = EQUIPMENTS[index][2];
return new PC(modelOrName,display);
case NOTEBOOK://22
double price = Double.parseDouble(EQUIPMENTS[index][2]);
return new NoteBook(modelOrName,price);
case PRINTER://23
String type = EQUIPMENTS[index][2];
return new Printer(modelOrName,type);
}
return null;
}
/**
* @author: admin
* @date: 2022/4/20 15:13
* @description: 获取所有的员工
* @return: team.domain.Employee[]
*/
public Employee[] getAllEMployees(){
return employees;
}
/**
* @author: admin
* @date: 2022/4/20 15:17
* @description: 返回指定ID的员工
* @Param id:
* @return: team.domain.Employee[]
*/
public Employee getEmployees(int id) throws TeamException {
for (int i=0;i<employees.length;i++){
if (employees[i].getId() == id){
return employees[i];
}
}
//现在先抛出,不处理,在整个系统最后面再进行处理
throw new TeamException("找不到指定的员工!");
}
}
TeamService:
package team.service;
import team.domain.Architect;
import team.domain.Designer;
import team.domain.Employee;
import team.domain.Programmer;
/**
* @BelongsProject: myproject3
* @BelongsPackage: team.service
* @Classname: TeamService
* @Author: admin
* @Date: 2022/4/20 18:55
* @Description: 关于开发团队成员的管理:添加、删除等
*/
public class TeamService {
//定义属性
private static int counter = 1;//给memberID赋值使用
private final int MAX_MEMBER = 5;//限制开发团队的人数最多为5个
private Programmer[] team = new Programmer[MAX_MEMBER];
private int total;//记录开发团队的实际的人数
public TeamService(){
super();
}
//定义方法
/**
* @author: admin
* @date: 2022/4/20 19:03
* @description: 获取开发团队中的所有成员
* @return: team.domain.Programmer[]
*/
public Programmer[] getTeam(){
Programmer[] team = new Programmer[total];
for (int i=0;i<team.length;i++){
team[i] = this.team[i];
}
return team;
}
/**
* @author: admin
* @date: 2022/4/20 19:04
* @description: 将指定的员工添加到开发团队当中
* @Param e:
* @return: void
*/
public void addMember(Employee e) throws TeamException {
/*各种不正确的情况*/
//成员已满,无法添加
if (total >= MAX_MEMBER){
throw new TeamException("成员已满,无法添加!");
}
//该成员不是开发成员,无法添加
if (!(e instanceof Programmer)){
throw new TeamException("该成员不是开发成员,无法添加");
}
//该员工已经在开发团队当中
if (isExist(e)){
throw new TeamException("该员工已经在开发团队当中");
}
//该员工已经是某团队的成员了
//该成员正在休假,无法添加
Programmer p = (Programmer)e;//一定不会出现ClassCastException
if ("BUSY".equalsIgnoreCase(p.getStatus().getNAME())){
//比 p.getStatus().getNAME().equals"BUSY 这样更好,避免出现空指针
throw new TeamException("该员工已经是某团队的成员了!");
}else if ("VOCATION".equalsIgnoreCase(p.getStatus().getNAME())){
throw new TeamException("该成员正在休假,无法添加了!");
}
//团队中至多只能有一名架构师
//团队中至多只能有两名设计师
//团队中至多只能有三名程序员
//获取团队当中已有成员的架构师,设计师,程序员的人数
int numOfPro = 0,numOfDes = 0,numOfArch = 0;
for(int i = 0;i < total;i++){
if(team[i] instanceof Architect){
numOfArch++;
}else if(team[i] instanceof Designer){
numOfDes++;
}else if(team[i] instanceof Programmer){
numOfPro++;
}
}
if (p instanceof Architect){
if (numOfArch >= 1){
throw new TeamException("团队中至多只能有一名架构师,无法添加了!");
}else if (numOfDes >=2){
throw new TeamException("团队中至多只能有两名设计师,无法添加了!");
}else if (numOfPro>=3){
throw new TeamException("团队中至多只能有三名程序员,无法添加了!");
}
}
//这种方法是不行的
//例如,有两名设计师,再添加一名架构师,会报"团队中至多只能有两名设计师"的异常
// if(p instanceof Architect && numOfArch >= 1){
// throw new TeamException("团队中至多只能有一名架构师");
// }else if(p instanceof Designer && numOfDes >= 2){
// throw new TeamException("团队中至多只能有两名设计师");
// }else if(p instanceof Programmer && numOfPro >= 3){
// throw new TeamException("团队中至多只能有三名程序员");
// }
/*讲p(或者e)添加到现有的团队当中*/
team[total++] = p;
//p的属性赋值
p.setStatus(Status.BUSY);
p.setMemberId(counter++);
}
/**
* @author: admin
* @date: 2022/4/20 19:16
* @description: 判断指定的员工是否存在于现有的开发团队当中
* true:存在 false:不存在,可以添加
* @Param e:
* @return: boolean
*/
private boolean isExist(Employee e) {
for (int i=0;i<total;i++){
if (e.getId() == team[i].getId()){
return true;
}
}
return false;
}
/**
* @author: admin
* @date: 2022/4/20 19:04
* @description: 从团队当中删除成员
* @Param memberID:
* @return: void
*/
public void removeMember(int memberID) throws TeamException {
int i;
for (i=0;i<total;i++){
if (team[i].getMemberId() == memberID){
team[i].setStatus(Status.FREE);
break;
}
}
if (i == total){
throw new TeamException("未找到指定的memberId的情况,删除失败!");
}
//后面的元素前移,覆盖前一个元素
for (int j=i+1;j<total;j++){
team[j-1] = team[j];
}
//方式一:
// team[total - 1] = null;
// total--;
//方式二
team[total--] = null;
//未找到指定的memberId的情况
}
}
3.实现view包中类
TimeView:
package team.view;
import team.domain.Employee;
import team.domain.Programmer;
import team.service.NameListService;
import team.service.TeamException;
import team.service.TeamService;
/**
* @BelongsProject: myproject3
* @BelongsPackage: team.view
* @Classname: TeamView
* @Author: admin
* @Date: 2022/4/20 20:12
* @Description:
*/
public class TeamView {
private NameListService listSvc = new NameListService();
private TeamService teamSvc = new TeamService();
public void enterMainMenu(){
boolean isFlag = true;
char menu = 0;
while (isFlag){
if (menu != '1'){
listAllEmployees();
}
System.out.print("1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 请选择(1-4):");
menu = TSUtility.readMenuSelection();
switch (menu) {
case '1':
getTeam();
break;
case '2':
addMember();
break;
case '3':
deleteMember();
break;
case '4':
System.out.print("确认是否退出(Y/N):");
char isExit = TSUtility.readConfirmSelection();
if(isExit == 'Y'){
isFlag = false;
}
break;
}
}
}
/**
* @author: admin
* @date: 2022/4/20 20:27
* @description: 显示所有员工信息
* @return: void
*/
public void listAllEmployees(){
System.out.println("-------------------------------开发团队调度软件--------------------------------\n");
Employee[] employees = listSvc.getAllEMployees();
if(employees == null || employees.length == 0){
System.out.println("员工列表为空");
System.out.println("--------------------------------------------------------------");
}else{
System.out.println("ID\t姓名\t年龄\t工资\t职位\t状态\t奖金\t股票\t领用设备");
//方法一
for (int i=0;i<employees.length;i++){
System.out.println(employees[i]);
}
//方法二
// for(Employee emp : employees){
// System.out.println(emp);
// }
System.out.println("--------------------------------------------------------------");
}
}
/**
* @author: admin
* @date: 2022/4/20 20:42
* @description: 查看开发团队的情况
* @return: void
*/
public void getTeam(){
System.out.println("--------------------团队成员列表---------------------");
Programmer[] team = teamSvc.getTeam();
if(team == null || team.length == 0){
System.out.println("开发团队目前没有成员!");
}else{
System.out.println("TDI/ID\t姓名\t年龄\t工资\t职位\t奖金\t股票");
for(int i = 0;i < team.length;i++){
System.out.println(team[i].getDetailsForTeam());
}
}
System.out.println("-----------------------------------------------------");
}
/**
* @author: admin
* @date: 2022/4/20 20:55
* @description: 添加团队成员
* @return: void
*/
public void addMember(){
System.out.println("---------------------添加成员---------------------");
System.out.print("请输入要添加的员工ID:");
int id = TSUtility.readInt();
try {
Employee emp = listSvc.getEmployees(id);
teamSvc.addMember(emp);
System.out.println("添加成功");
} catch (TeamException e) {
e.printStackTrace();
System.out.println("添加失败。失败的原因:" + e.getMessage());
}
//按回车键继续...
TSUtility.readReturn();
}
/**
* @author: admin
* @date: 2022/4/20 21:05
* @description: 实现删除成员操作
* @return: void
*/
public void deleteMember(){
System.out.println("---------------------删除成员---------------------");
System.out.print("请输入要删除员工的TID:");
int memberId = TSUtility.readInt();
System.out.println("确认是否删除(Y/N):");
char isDelete = TSUtility.readConfirmSelection();
if (isDelete == 'N'){
return;
}
try {
teamSvc.removeMember(memberId);
System.out.println("删除成功!");
} catch (TeamException e) {
System.out.println("删除失败,原因是:" + e.getMessage());;
}
//按回车键继续...
TSUtility.readReturn();
}
public static void main(String[] args) {
TeamView view = new TeamView();
view.enterMainMenu();
}
}