package august.domain;
/**
* 设备领取
*
* @author : Crazy_August
* @Date: 2021-02-28
* @Time: 16:58
*/
public interface Equipment {
public abstract String getDescription();
}
package august.domain;
/**
* Created with IntelliJ IDEA.
*
* @author : Crazy_August
* @Date: 2021-02-28
* @Time: 17:41
*/
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 String getDisplay() {
return display;
}
@Override
public String getDescription() {
return model +"(" + display + ")";
}
}
package august.domain;
import august.domain.Equipment;
/**
* Created with IntelliJ IDEA.
*
* @author : Crazy_August
* @Date: 2021-02-28
* @Time: 17:43
*/
public class NoteBook implements Equipment {
//机器的型号
private String model;
private double price;
public NoteBook() {
}
public NoteBook(String model, double price) {
this.model = model;
this.price = 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;
}
@Override
public String getDescription() {
return model + "(" + price + ")";
}
}
package august.domain;
/**
* Created with IntelliJ IDEA.
*
* @author : Crazy_August
* @Date: 2021-02-28
* @Time: 17:45
*/
public class Printer implements Equipment {
private String name;
//机器的机型
private String type;
public Printer() {
}
public Printer(String name, String type) {
this.name = name;
this.type = 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;
}
@Override
public String getDescription() {
return name + "(" + type + ")";
}
}
package august.domain;
/**
* Created with IntelliJ IDEA.
*
* @author : Crazy_August
* @Date: 2021-02-28
* @Time: 16:47
*/
public class Employee {
private int id;
private String name;
private int age;
private double salary;
public Employee(int id, String name, int age, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = 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 String getshow(){
return id + "\t" + name + "\t"+ age + "\t"+ salary ;
}
@Override
public String toString() {
return getshow();
}
}
package august.domain;
import august.service.Status;
/**
* Created with IntelliJ IDEA.
*
* @author : Crazy_August
* @Date: 2021-02-28
* @Time: 16:51
*/
public class Programer extends Employee implements Equipment {
//用来记录成员加入开发团队后在团队中的ID
private int memberld;
private Status status = Status.FREE;
// 表示该成员领用的设备
private Equipment equipment;
public Programer(int id, String name, int age, double salary, Equipment equipment) {
super(id, name, age, salary);
this.equipment = equipment;
}
public Programer(int id, String name, int age, double salary) {
super(id, name, age, salary);
}
@Override
public String getDescription() {
return null;
}
public int getMemberld() {
return memberld;
}
public void setMemberld(int memberld) {
this.memberld = memberld;
}
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 getshow() + "\t\t" + "程序员" + "\t" + getStatus().getNAME() + "\t\t" + "\t\t\t\t\t\t\t" + getEquipment().getDescription();
}
public String getDetailsTeam() {
return memberld + "/" + getId() + "\t\t\t" + getName() + "\t" + getAge() + "\t\t" + getSalary() + "\t\t程序员";
}
}
package august.domain;
import august.service.Status;
/**
* Created with IntelliJ IDEA.
*
* @author : Crazy_August
* @Date: 2021-02-28
* @Time: 16:51
*/
public class Designer extends Programer {
//表示奖金
private double bonus;
public Designer(int id, String name, int age, double salary, Equipment equipment, double bonus) {
super(id, name, age, salary, equipment);
this.bonus = bonus;
}
public Designer(int id, String name, int age, double salary) {
super(id, name, age, salary);
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
@Override
public String toString() {
return getshow() + "\t\t" + "设计师" + "\t" + getStatus().getNAME() + "\t\t" + bonus + "\t\t\t\t\t\t" + getEquipment().getDescription();
}
@Override
public String getDetailsTeam() {
return getMemberld() + "/" + getId() + "\t\t" + getName() + "\t" + getAge() + "\t\t" + getSalary() + "\t\t设计师\t" +bonus;
}
}
package august.domain;
import august.service.Status;
/**
* Created with IntelliJ IDEA.
*
* @author : Crazy_August
* @Date: 2021-02-28
* @Time: 16:52
*/
public class Architect extends Designer {
//表示公司奖励的股票数量
private int stock;
public Architect(int id, String name, int age, double salary, Equipment equipment, double bonus, int stock) {
super(id, name, age, salary, equipment, bonus);
this.stock = stock;
}
public Architect(int id, String name, int age, double salary) {
super(id, name, age, salary);
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
@Override
public String toString() {
return getshow() + "\t\t" + "架构师" + "\t" + getStatus().getNAME()+ "\t\t" + getBonus() + "\t\t\t" + stock + "\t\t" +getEquipment().getDescription();
}
@Override
public String getDetailsTeam() {
return getMemberld() + "/" + getId() + "\t\t" + getName() + "\t" + getAge() + "\t\t" + getSalary() + "\t\t架构师\t" +getBonus() + "\t\t" + getStock();
}
}
package august.view;
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* 目中提供了TSUtility.java类,可用来方便地实现键盘访问。
*
* @author : Crazy_August
* @Date: 2021-02-28
* @Time: 17:06
*/
public class TSUtility {
private static Scanner scanner = new Scanner(System.in);
/**
* @return
* @Description 该方法读取键盘,如果用户键入’1’-’4’中的任意字符,则方法返回。返回值为用户键入字符。
* @author shkstart
* @date 2019年2月12日上午12:03:30
*/
public static char readMenuSelection() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false);
c = str.charAt(0);
if (c != '1' && c != '2' &&
c != '3' && c != '4') {
System.out.print("选择错误,请重新输入:");
} else {
break;
}
}
return c;
}
/**
* @Description 该方法提示并等待,直到用户按回车键后返回。
* @author shkstart
* @date 2019年2月12日上午12:03:50
*/
public static void readReturn() {
System.out.print("按回车键继续...");
readKeyBoard(100, true);
}
/**
* @return
* @Description 该方法从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
* @author shkstart
* @date 2019年2月12日上午12:04:04
*/
public static int readInt() {
int n;
for (; ; ) {
String str = readKeyBoard(2, false);
try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}
/**
* @return
* @Description 从键盘读取‘Y’或’N’,并将其作为方法的返回值。
* @author shkstart
* @date 2019年2月12日上午12:04:45
*/
public static char readConfirmSelection() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false).toUpperCase();
c = str.charAt(0);
if (c == 'Y' || c == 'N') {
break;
} else {
System.out.print("选择错误,请重新输入:");
}
}
return c;
}
private static String readKeyBoard(int limit, boolean blankReturn) {
String line = "";
while (scanner.hasNextLine()) {
line = scanner.nextLine();
if (line.length() == 0) {
if (blankReturn) {
return line;
} else {
continue;
}
}
if (line.length() < 1 || line.length() > limit) {
System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
continue;
}
break;
}
return line;
}
}
package august.service;
import august.domain.*;
/**
* 功能:负责将Data中的数据封装到Employee[]数组中,同时提供相关操作Employee[]的方法。
*
* @author : Crazy_August
* @Date: 2021-02-28
* @Time: 16:48
*/
public class NameListService {
//用来保存公司所有员工对象
private Employee[] employees;
//给Employee及数组元素进行初始化
public NameListService() {
//1.根据项目提供的Data类构建相应大小的employees数组
employees = new Employee[Data.EMPLOYEES.length];
//动态绑定
Equipment equipment;
//2.再根据Data类中的数据构建不同的对象,包括Employee、Programmer、Designet ,Architect对象,以及相关联的Equipment子类的对象
//3.将对象存于数组中
for (int i = 0; i < employees.length; i++) {
int type = Integer.parseInt(Data.EMPLOYEES[i][0]);
if (Data.EMPLOYEE == type) {
employees[i] = new Employee(Integer.parseInt(Data.EMPLOYEES[i][1]), Data.EMPLOYEES[i][2], Integer.parseInt(Data.EMPLOYEES[i][3]), Double.parseDouble(Data.EMPLOYEES[i][4]));
} else if (Data.PROGRAMMER == type) {
//创建员工的设备
equipment = creativeEquipment(i);
employees[i] = new Programer(Integer.parseInt(Data.EMPLOYEES[i][1]), Data.EMPLOYEES[i][2], Integer.parseInt(Data.EMPLOYEES[i][3]), Double.parseDouble(Data.EMPLOYEES[i][4]), equipment);
} else if (Data.DESIGNER == type) {
equipment = creativeEquipment(i);
employees[i] = new Designer(Integer.parseInt(Data.EMPLOYEES[i][1]), Data.EMPLOYEES[i][2], Integer.parseInt(Data.EMPLOYEES[i][3]), Double.parseDouble(Data.EMPLOYEES[i][4]), equipment, Double.parseDouble(Data.EMPLOYEES[i][5]));
} else if (Data.ARCHITECT == type) {
equipment = creativeEquipment(i);
employees[i] = new Architect(Integer.parseInt(Data.EMPLOYEES[i][1]), Data.EMPLOYEES[i][2], Integer.parseInt(Data.EMPLOYEES[i][3]), Double.parseDouble(Data.EMPLOYEES[i][4]), equipment, Double.parseDouble(Data.EMPLOYEES[i][5]), Integer.parseInt(Data.EMPLOYEES[i][6]));
}
}
}
/**
* @Description :获取员工的设备
* @Author Crazy_August
* @Date 2021-02-28 20:23
* @Since version-1.0
*/
public Equipment creativeEquipment(int index) {
int Employeestype = Integer.parseInt(Data.EQIPMENTS[index][0]);
switch (Employeestype) {
case Data.PC:
return new PC(Data.EQIPMENTS[index][1], Data.EQIPMENTS[index][2]);
case Data.NOTEBOOK:
return new NoteBook(Data.EQIPMENTS[index][1], Double.parseDouble(Data.EQIPMENTS[index][2]));
case Data.PRINTER:
return new Printer(Data.EQIPMENTS[index][1], Data.EQIPMENTS[index][2]);
default:
}
return null;
}
public Employee[] getAllEmployee() {
return employees;
}
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("找不到指定的员工");
}
}
package august.service;
/**
* Created with IntelliJ IDEA.
*
* @author : Crazy_August
* @Date: 2021-02-28
* @Time: 16:49
*/
public class TeamException extends Exception{
static final long serialVersionUID = -3387516229948L;
public TeamException() {
super();
}
public TeamException(String msg) {
super(msg);
}
}
package august.service;
import august.domain.Architect;
import august.domain.Designer;
import august.domain.Employee;
import august.domain.Programer;
/**
* 功能:关于开发团队成员的管理:添加、删除等。
*
* @author : Crazy_August
* @Date: 2021-02-28
* @Time: 16:48
*/
public class TeamService {
//用来为开发团队新增成员自动生成团队中的唯一ID
private static int counter = 1;
private final int MAX_MEMBER = 5;
//用来保存当前团队中的各成员对象
private Programer[] team = new Programer[MAX_MEMBER];
//记录团队成员的实际人数
private int total = 0;
//获取team中架构师、设计师、程序员中的人数
private int numArc = 0;
private int numDes = 0;
private int numPro = 0;
//得到团队
public Programer[] getTeam() {
Programer[] team = new Programer[total];
for (int i = 0; i < team.length; i++) {
team[i] = this.team[i];
}
return team;
}
/**
* @Description :将指定的员工加到开发团队中
* @Param [employee]
* @Return [august.domain.Employee]
* @Author Crazy_August
* @Date 2021-03-01 12:59
* @Since version-1.0
*/
public void addMember(Employee employee) throws TeamException {
//失败信息包含以下几种:
//成员已满,无法添加
if (total == MAX_MEMBER) {
throw new TeamException("成员已满,无法添加");
}
//该成员不是开发人员,无法添加
//判断employee是否是程序员
if (!(employee instanceof Programer)) {
throw new TeamException("该成员不是开发人员,无法添加");
}
//该员工已在本开发团队中
if (isExist(employee)) {
throw new TeamException("该员工已在本开发团队中,无法添加");
}
//该员工已是某团队成员
//需要强转,因为上方if已经过滤掉employee没有Status属性
Programer programer = (Programer) employee;
// 注意:判断字符串内容是否相同用.equals()
// if ("BUSY".equalsIgnoreCase(programer.getStatus().getNAME())) {
// throw new TeamException("该员工已是某团队成员,无法添加");
// } else if ("VOCATION".equalsIgnoreCase(programer.getStatus().getNAME())) {
// //该员正在休假,无法添加
// throw new TeamException("该员正在休假,无法添加");
// }
if (programer.getStatus() == Status.BUSY) {
throw new TeamException("该员工已是某团队成员,无法添加");
} else if (programer.getStatus() == Status.VOCATION) {
//该员正在休假,无法添加
throw new TeamException("该员正在休假,无法添加");
}
//团队中至多只能有一名架构师
//团队中至多只能有两名设计师
//团队中至多只能有三名程序员
// for (int i = 0; i < total; i++) {
//
// if (employee instanceof Architect) {
//
// } else if (employee instanceof Designer) {
//
// } else if (employee instanceof Programer){
//
// }
// }
//小范围开始,因为都继承关系为 program > Desiger >Architect
if (employee instanceof Architect) {
if (numArc >= 1) {
throw new TeamException("团队中至多只能有一名架构师,无法添加");
}
numArc++;
} else if (employee instanceof Designer) {
if (numDes >= 2) {
throw new TeamException("团队中至多只能有两名设计师,无法添加");
}
numDes++;
} else {
if (numPro >= 3) {
throw new TeamException("团队中至多只能有三名程序员,无法添加");
}
numPro++;
}
//添加无异常,添加 employee(需要强转,因为team是Program类型)
// 或者 programer(上方已经转型 Programer programer = (Programer) employee;)
// 操作
//方式一:
//team[total] = (Programer) employee;
//方式二:
team[total] = programer;
total++;
//更改开发团队员工的Status
programer.setStatus(Status.BUSY);
programer.setMemberld(counter++);
}
/**
* @Description :判断员工是否存在开发团队中
* @Param [employee]
* @Return [august.domain.Employee]
* @Author Crazy_August
* @Date 2021-03-01 13:23
* @Since version-1.0
*/
private boolean isExist(Employee employee) {
for (int i = 0; i < total; i++) {
if (team[i].getId() == employee.getId()) {
return true;
}
}
return false;
}
/**
* @Description :删除指定成员
* @Param [menberld]
* @Return [int]
* @Author Crazy_August
* @Date 2021-03-01 18:34
* @Since version-1.0
*/
public void removeMember(int menberld) throws TeamException {
for (int i = 0; i < total; i++) {
if (team[i].getMemberld() == menberld) {
//修改状态
team[i].setStatus(Status.FREE);
Programer programer = team[i];
if (programer instanceof Architect) {
numArc--;
} else if (programer instanceof Designer) {
numDes--;
} else {
numPro--;
}
// 后一个元素覆盖前一个元素,实现删除操作
for (int j = i; j < total - 1; j++) {
team[j] = team[j + 1];
}
//最后一个元素需要置空
// team[total - 1] = null;
// total--;
//或
team[--total]=null;
return;
}
}
throw new TeamException("删除失败找不到指定成员");
}
}
package august.service;
/**
*
* 表示员工的状态
* @author : Crazy_August
* @Date: 2021-02-28
* @Time: 18:24
*/
public class Status {
private final String STATUS;
private Status(String STATUS){
this.STATUS = STATUS;
}
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 STATUS;
}
}
package august.service;
/**
* Created with IntelliJ IDEA.
*
* @author : Crazy_August
* @Date: 2021-02-28
* @Time: 17:11
*/
public class Data {
public static final int EMPLOYEE = 10;
public static final int PROGRAMMER = 11;
public static final int DESIGNER = 12;
public static final int ARCHITECT = 13;
public static final int PC = 21;
public static final int NOTEBOOK = 22;
public static final int PRINTER = 23;
//Employee : 10, id, name, age, salary
//Programmer: 11, id, name, age, salary
//Designer : 12, id, name, age, salary, bonus
//Architect : 13, id, name, age, salary, bonus, stock
public static final String[][] EMPLOYEES = {
{"10", "1", "马 云", "22", "3000"},
{"13", "2", "马化腾", "32", "18000", "15000", "2000"},
{"11", "3", "李彦宏", "23", "7000"},
{"11", "4", "刘强东", "24", "7300"},
{"12", "5", "雷 军", "28", "10000", "5000"},
{"11", "6", "任志强", "22", "6800"},
{"12", "7", "柳传志", "29", "10800", "5200"},
{"13", "8", "杨元庆", "30", "19800", "15000", "2500"},
{"12", "9", "史玉柱", "26", "9800", "5500"},
{"11", "10", "丁 磊", "21", "6600"},
{"11", "11", "张朝阳", "25", "7100"},
{"12", "12", "杨致远", "27", "9600", "4800"}
};
//如下的EQIPMENTS数组与上面的EMPLOYEES数组元素一一对应
//PC :21, model, display
//NoteBook:22, model, price
//Printer :23, type, name
public static final String[][] EQIPMENTS = {
{},
{"22", "联想T4", "6000"},
{"21", "戴尔", "NEC17寸"},
{"21", "戴尔", "三星 17寸"},
{"23", "激光", "佳能 2900"},
{"21", "华硕", "三星 17寸"},
{"21", "华硕", "三星 17寸"},
{"23", "针式", "爱普生20K"},
{"22", "惠普m6", "5800"},
{"21", "戴尔", "NEC 17寸"},
{"21", "华硕", "三星 17寸"},
{"22", "惠普m6", "5800"}
};
}
package august.view;
/**
* view
*
* @author : Crazy_August
* @Date: 2021-02-28
* @Time: 16:40
*/
import august.domain.Employee;
import august.domain.Programer;
import august.service.NameListService;
import august.service.TeamException;
import august.service.TeamService;
/**
* @Description :负责菜单的显示和处理用户操作
* @Author Crazy_
* August
* @Date 2021-02-28 16:42
* @Since version-1.0
*/
public class TeamView {
private NameListService listSvc = new NameListService();
private TeamService teamSvc = new TeamService();
public static void main(String[] args) {
TeamView teamView = new TeamView();
teamView.entermMainMenu();
}
//主界面显示及控制方法。
public void entermMainMenu() {
boolean isFlag = true;
TeamView teamView = new TeamView();
char menu = 0;
do {
if (menu != '1') {
teamView.listAllEmployees();
}
menu = TSUtility.readMenuSelection();
switch (menu) {
case '1':
teamView.getTeam();
break;
case '2':
teamView.addMember();
break;
case '3':
teamView.deleteMember();
break;
case '4':
System.out.println("确认是否退出( Y/N ):");
char key = TSUtility.readConfirmSelection();
if ('Y' == key) {
isFlag = false;
System.out.println("退出成功");
}
break;
default:
}
} while (isFlag);
}
//以表格形式列出公司所有成员
private void listAllEmployees() {
System.out.println("------------------------------------开发团队调度软件---------------------------------------");
System.out.println("ID\t姓名\t\t年龄\t工资\t\t\t职位\t\t状态\t\t\t奖金\t\t\t\t股票\t\t\t领用设备");
//显示员工详细信息
for (int i = 0; i < listSvc.getAllEmployee().length; i++) {
System.out.println(listSvc.getAllEmployee()[i]);
}
System.out.println("-----------------------------------------------------------------------------------------");
System.out.println("1- 团队成员 2- 添加团队成员 3-删除团队成员 4-退出 请选择( 1 - 4 ) ");
}
//显示团队成员列表操作
private void getTeam() {
System.out.println("------------------------------------团队列表----------------------------------------------");
Programer[] team = teamSvc.getTeam();
if (team == null || team.length == 0){
System.out.println("开发团队还没有成员");
}else {
System.out.println("TID/ID\t\t姓名\t\t年龄\t\t工资\t\t\t职位\t\t奖金\t\t\t股票");
for (int i = 0; i < team.length; i++) {
System.out.println(team[i].getDetailsTeam());
}
System.out.println("-----------------------------------------------------------------------------------------");
}
}
//实现添加成员操作
private void addMember() {
System.out.println("------------------------------------添加员工----------------------------------------------");
System.out.print("请输入要添加的员工ID:");
int id = TSUtility.readInt();
try {
Employee employees = listSvc.getEmployees(id);
teamSvc.addMember(employees);
System.out.println("添加成功");
} catch (TeamException e) {
System.out.println("添加失败,原因 :" + e.getMessage() + "\n");
}
System.out.println("-----------------------------------------------------------------------------------------");
//按回车继续
TSUtility.readReturn();
}
//实现删除成员操作
private void deleteMember() {
System.out.println("------------------------------------删除成员----------------------------------------------");
System.out.print("请输入要删除员工的TID:");
int id = TSUtility.readInt();
System.out.print("确认是否删除( Y/N ):");
char key = TSUtility.readConfirmSelection();
try {
if (key == 'Y') {
teamSvc.removeMember(id);
System.out.println("删除成功");
}
} catch (TeamException e) {
System.out.print("删除失败,原因 :" + e.getMessage() + "\n");
}
TSUtility.readReturn();
System.out.println("-----------------------------------------------------------------------------------------");
}
}
自定义异常类:
① 继承异常类 extends Exception
② 序列号 static final long serialVersionUID = -3387516229948L;
③ 提供构造器
public TeamException() { super(); } public TeamException(String msg) { super(msg); }
判断字符串内容是否相同用.equals()
通常字符串放在前,避免空指针异常
// if ("BUSY".equalsIgnoreCase(programer.getStatus().getNAME())) { // throw new TeamException("该员工已是某团队成员,无法添加"); // } else if ("VOCATION".equalsIgnoreCase(programer.getStatus().getNAME())) { // //该员正在休假,无法添加 // throw new TeamException("该员正在休假,无法添加"); // }--
使用 instanceof 用来判断A是否是B的实例对象或者B子类的实例对象。
A instanceof B ,返回值为boolean类型。 从小范围开始,因为都继承关系为 program > Desiger >Architect
Person p = new Person() ; Man m = new Man() ; //Man是Person的子类 Animal a = new Animal() ; m instanceof Man //返回true m instanceof Animal//返回false m instanceof Person//返回true
``` //小范围开始,因为都继承关系为 program > Desiger >Architect if (employee instanceof Architect) { if (numArc >= 1) { throw new TeamException("团队中至多只能有一名架构师,无法添加"); } numArc++; } else if (employee instanceof Designer) { if (numDes >= 2) { throw new TeamException("团队中至多只能有两名设计师,无法添加"); } numDes++; } else { if (numPro >= 3) { throw new TeamException("团队中至多只能有三名程序员,无法添加"); } numPro++; } ```
removeMember方法有点难度
删除操作
1.判断输入是否合法
2.后一个元素覆盖前一个元素,实现删除操作
3.//最后一个元素需要置空 null
慢慢来,会很快。