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"}
};
//如下的EQUIPMENTS数组与上面的EMPLOYEES数组元素一一对应
//PC :21, model, display
//NoteBook:22, model, price
//Printer :23, name, type
public static final String[][] EQUIPMENTS = {
{},
{"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"}
};
}
NameListService类
public class NameListService {
private Employee[] employees;//用来保存公司所有员工对象
public NameListService(){
employees=new Employee[EMPLOYEES.length];
for (int i = 0; i < employees.length; i++) {
int num=Integer.parseInt(EMPLOYEES[i][0]);
int id=Integer.parseInt(EMPLOYEES[i][1]);
String name=EMPLOYEES[i][2];
int age=Integer.parseInt(EMPLOYEES[i][3]);
double salary=Integer.parseInt(EMPLOYEES[i][4]);
Equipment equipment;
double bonus;
int stock;
switch (num){
case EMPLOYEE:
employees[i]=new Employee(id,name,age,salary);
break;
case PROGRAMMER:
equipment=EQ(i);
employees[i]=new Programmer(id,name,age,salary,equipment);
break;
case DESIGNER:
equipment=EQ(i);
bonus=Integer.parseInt(EMPLOYEES[i][5]);
employees[i]=new Designer(id,name,age,salary,equipment,bonus);
break;
case ARCHITECT:
equipment=EQ(i);
bonus=Integer.parseInt(EMPLOYEES[i][5]);
stock=Integer.parseInt(EMPLOYEES[i][6]);
employees[i]=new Architect(id,name,age,salary,equipment,bonus,stock);
break;
}
}
}
public Equipment EQ(int temp){
int num=Integer.parseInt(EQUIPMENTS[temp][0]);
String nameOrModel=EQUIPMENTS[temp][1];
switch (num){
case PC:
String display=EQUIPMENTS[temp][2];
return new PC(nameOrModel,display);
case NOTEBOOK:
int price=Integer.parseInt(EQUIPMENTS[temp][2]);
return new NoteBook(nameOrModel,price);
case PRINTER:
String type=EQUIPMENTS[temp][2];
return new Printer(nameOrModel,type);
}
return null;
}
}