需求:
//equipment 接口
public interface Equipment {
public abstract String getDescription();
}
//PC
public class Pc implements Equipment{
private String model;
private String display;
public Pc() {
super();
}
public Pc(String model, String display) {
super();
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() {
// TODO Auto-generated method stub
return model+"("+display+")";
}
}
// NoteBook
public class NoteBook implements Equipment{
private String model;
private double price;
public NoteBook() {
super();
}
public NoteBook(String model, double price) {
super();
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+")";
}
}
// Printer
public class Printer implements Equipment{
private String name;
private String type;
public Printer() {
super();
}
public Printer(String name, String type) {
super();
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() {
// TODO Auto-generated method stub
return name+"("+type+")";
}
// Employee
public class Employee {
private int id;
private String name;
private int age;
private double 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 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 getbasic() {
return id + "\t" + name + "\t" + age + "\t" + salary ;
}
@Override
public String toString() {
return getbasic();
}
}
// Programmer
public class Programmer extends Employee {
private int memberId;
private Status status=Status.FREE;
private Equipment equipment;
public Programmer() {
super();
}
public Equipment getEquipment() {
return equipment;
}
public void setEquipment(Equipment equipment) {
this.equipment = equipment;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public int getMemberId() {
return memberId;
}
public void setMemberId(int memberId) {
this.memberId = memberId;
}
public Programmer(int id, String name, int age, double salary, Equipment equipment) {
super(id, name, age, salary);
this.equipment = equipment;
}
public String toString() {
return getbasic()+"\t程序员\t"+equipment.getDescription();
}
public String getbaiscForTeam() {
return getId() + "\t" + getName() + "\t" + getAge() + "\t" + getSalary() ;
}
public String getDetailForTeam() {
return getbaiscForTeam() +"\t程序员\t";
}
}
//Designer
public class Designer extends Programmer{
private double bonus;
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = 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 String toString() {
return getbasic()+"\t设计师\t"+getEquipment().getDescription()+"\t"+bonus;
}
public String getDetailForTeam() {
return getbaiscForTeam() +"\t设计师\t"+"\t"+bonus;
}
}
// Architect
public class Architect extends Designer {
private int stock;
public Architect() {
super();
}
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 String toString() {
return getbasic()+"\t架构师\t"+getEquipment().getDescription()+"\t"+getBonus()+"\t"+stock;
}
public String getDetailForTeam() {
return getbaiscForTeam() +"\t架构师\t"+"\t"+getBonus()+"\t"+stock;
}
}
// NameListService
public class NameListService {
private Employee[] employees;
public NameListService() {
employees=new Employee[Data.EMPLOYEES.length];
for(int i=0;i
int type= Integer.parseInt(Data.EMPLOYEES[i][0]);
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;
switch(type) {
case EMPLOYEE:
employees[i]=new Employee(id, name, age, salary);
break;
case PROGRAMMER:
equipment=createEquipment(i);
employees[i]=new Programmer(id, name, age, salary, equipment);
break;
case DESIGNER:
bonus=Double.parseDouble(Data.EMPLOYEES[i][5]);
equipment=createEquipment(i);
employees[i]=new Designer(id, name, age, salary, equipment, bonus);
break;
case ARCHITECT:
int stock=Integer.parseInt(Data.EMPLOYEES[i][6]);
equipment=createEquipment(i);
bonus=Double.parseDouble(Data.EMPLOYEES[i][5]);
employees[i]=new Architect(id, name, age, salary, equipment, bonus, stock);
break;
}
}
}
private Equipment createEquipment(int i) {
// TODO Auto-generated method stub
int type= Integer.parseInt(EQUIPMENTS[i][0]);
switch(type) {
case PC:
return new Pc(EQUIPMENTS[i][1],EQUIPMENTS[i][2]);
case NOTEBOOK:
return new NoteBook(EQUIPMENTS[i][1],Double.parseDouble(EQUIPMENTS[i][2]));
case PRINTER:
return new Printer(EQUIPMENTS[i][1],EQUIPMENTS[i][2]);
}
return null;
}
public Employee[] getAllEmployees() {
return employees;
}
public Employee getEmployee(int id) throws TeamException {
if(id>=employees.length) {
throw new TeamException("该员工不存在!");
}
return employees[id];
}
}
// TeamException
public class TeamException extends Exception {
static final long serialVersionUID = -70348971945766939L;
public TeamException() {}
public TeamException(String msg) {
super(msg);
}
}
// TeamService
public class TeamService {
private static int counter=1;
private static final int MAX_MEMBER=5;
private Programmer[] team =new Programmer[MAX_MEMBER];
private int total=0;
public Programmer[] getTeam() {
Programmer[] team = new Programmer[total];
for(int i=0;i
}
return team;
}
public void addMember(Employee e) throws TeamException {
if(total>=MAX_MEMBER) {
throw new TeamException("团队成员已满,不能添加!");
}if (!(e instanceof Programmer)) {
throw new TeamException("员工不是程序员,不能添加!");
}
if(isexit(e)) {
throw new TeamException("员工已在团队中,不能添加!");
}
Programmer p = (Programmer)e;
if("BUSY".equals(p.getStatus().getName())) {
throw new TeamException("员工已在其他团队中,不能添加!");
}
if("VOCATION".equals(p.getStatus().getName())) {
throw new TeamException("员工正在休假,不能添加!");
}
int numOfAi=0,numOfPro=0,numOfDes=0;
for(int i=0;i
numOfAi++;
}else if(p instanceof Designer){
numOfDes++;
}else if(p instanceof Programmer) {
numOfPro++;
}
}
if(p instanceof Architect) {
if(numOfAi>=1) {
throw new TeamException("团队中最多有一名架构师!");
}
}else if(p instanceof Designer) {
if(numOfDes>=2) {
throw new TeamException("团队中最多有2名设计师!");
}
}else if(p instanceof Programmer) {
if(numOfPro>=3) {
throw new TeamException("团队中最多有3名程序员!");
}
}
team[total]=p;
total++;
p.setStatus(Status.BUSY);
counter++;
}
private boolean isexit(Employee e) {
for(int i=0;i
}
return false;
}
public void removeMember(int memberId) throws TeamException {
int i=0;
for(;i
team[i].setStatus(Status.FREE);
break;
}
}
if(i==total) {
throw new TeamException("未找到团队成员!");
}
for(int j=i;j
}
team[total-1]=null;
total--;
}
}
// TeamView
public class TeamView {
private NameListService listSvc= new NameListService();
private TeamService teamSvc =new TeamService();
public void enterMainMenu() {
boolean isflag=true;
while (isflag) {
listAllEmployees();
System.out.println("1-显示团队列表 2-添加团队成员 3-删除团队成员 4 退出");
System.out.println("请输入选项:");
int key = TSUtility.readInt();
switch (key) {
case 1:
getTeam();
break;
case 2:
addMember();
break;
case 3:
deleteMember();
break;
case 4:
System.out.println("请输入Y或N确认,Y确认,N取消!");
char readConfirmSelection = TSUtility.readConfirmSelection();
if(readConfirmSelection=='Y') {
isflag=false;
}
break;
}
System.out.println("——————————————————————结束————");
}
}
private void listAllEmployees() {
System.out.println("——————————————————————客户列表信息————");
System.out.println("编号\t姓名\t 年龄 \t薪水\t 职位 \t设备\t\t奖金 \t 股票\n");
Employee[] allEmployees = listSvc.getAllEmployees();
for(int i=0;i
}
}
private void getTeam() {
Programmer[] team = teamSvc.getTeam();
if(team==null||team.length==0) {
System.out.println("团队信息不存在");
}else {
System.out.println("团队编号/编号 \t姓名 \t年龄 \t薪水\t 职位 \t奖金 \t 股票 ");
for(int i=0;i
}
}
}
private void addMember() {
System.out.println("请输入添加的员工编号:");
int id = TSUtility.readInt();
Employee employee;
try {
employee = listSvc.getEmployee(id);
teamSvc.addMember(employee);
System.out.println("添加成功!");
} catch (TeamException e) {
// TODO Auto-generated catch block
System.out.println("添加失败,原因为:"+e.getMessage());
}
}
private void deleteMember() {
System.out.println("请输入删除的员工编号:");
int id = TSUtility.readInt();
try {
teamSvc.removeMember(id);
System.out.println("删除成功!");
} catch (TeamException e) {
// TODO Auto-generated catch block
System.out.println("删除失败,原因为:"+e.getMessage());
}
}
public static void main(String[]args) {
TeamView view =new TeamView();
view.enterMainMenu();
}
}