只是简单学完了面向对象,在视频讲解跟练下,实现一个员工管理,无关乎数据库操作,在给定数据下给员工们创建团队,对团队进行操作,增加,删除等,锻炼的是面向对象内容,接口实现,继承关系等。
1. 建立上述的包,给出TSutility工具包,实现键盘读取信息。
package com.team.view;
import java.util.*;
public class TSUtility {
private static Scanner scanner = new Scanner(System.in);
//用于读取菜单选项
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' && c!= '5') {
System.out.println("选择错误,请重新输入: ");
}else {
break;
}
}
return c;
}
//提示并等待,知道用户按回车键返回
public static void readReturn() {
System.out.println("按回车键继续。。。");
readKeyBoard(100, true);
}
//从键盘读取一个字符,将其作为方法的返回值
public static char readChar() {
String str = readKeyBoard(1,false);
return str.charAt(0);
}
//用户不输入字符而直接回车,以defaultValue作为返回值,修改时用到
public static char readChar(char defaultValue) {
String str= readKeyBoard(1,true);
return (str.length() == 0) ? defaultValue : str.charAt(0);
}
//从键盘读取不超过两位的整数
public static int readInt() {
int n;
for(;;) {
String str = readKeyBoard(2,false);
try {
n = Integer.parseInt(str);
break;
}catch(NumberFormatException e) {
System.out.println("数字输入错误,请重新输入: ");
}
}
return n;
}
//修改时,若无修改,则默认为原来值
public static int readInt(int defaultValue) {
int n;
for(;;) {
String str = readKeyBoard(2,true);
if(str.equals("")) {
return defaultValue;
}
try {
n = Integer.parseInt(str);
break;
}catch(NumberFormatException e) {
System.out.println("数字输入错误,请重新输入: ");
}
}
return n;
}
//读取字符串
public static String readString(int limit) {
return readKeyBoard(limit,false);
}
//修改
public static String readString(int limit,String defaultValue) {
String str = readKeyBoard(limit,true);
return str.equals("") ? defaultValue : str;
}
//读取Y或N
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.println("输入长度(不大于" + limit + ")错误,请重新输入: ");
continue;
}
break;
}
return line;
}
}
2.给出所用的数据,自己建立也完全ok
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;
//PROGRAMME : 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","8000","2000"},
{"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"},
};
//定义EQUIPMENT
//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"},
};
}
该数据是听尚硅谷视频时打的。
3.在domain里创建各个表示
考虑继承关系。
4.录入员工和装备信息
package com.team.service;
import com.team.domain.*;
/*import com.team.domain.Architect;
import com.team.domain.Employee;
import com.team.domain.Equipment;
import com.team.domain.NoteBook;
import com.team.domain.PC;
import com.team.domain.Printer;
import com.team.domain.Programmer;*/
import static com.team.service.Data.*;
public class NameListService {
private Employee[] employees;
/*
*构造构造器,实现功能
*根据项目提供的Data类中的数据构建不同的对象,包括Employee、Programmer、Desihner和Architect
*以及相关联的Equipment子类的对象
*将对象存于数组中
*/
//给employee数组和数组元素初始化
//根据不同的类型构造不同的对象
public NameListService() {
employees = new Employee[Data.EMPLOYEES.length];
for(int i = 0;i < employees.length;i ++) {
//获取员工类型
int type = Integer.parseInt(Data.EMPLOYEES[i][0]);
//获取员工的4个基本信息
int id = Integer.parseInt(EMPLOYEES[i][1]);
String name = EMPLOYEES[i][2];
int age = Integer.parseInt(EMPLOYEES[i][3]);
double salary = Double.parseDouble(EMPLOYEES[i][4]);
Equipment equipment;
double bonus;
int stock;
//根据所获得的员工类型去创建对象
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);
}
}
}
public Employee[] getEmployees() {
return employees;
}
public void setEmployees(Employee[] employees) {
this.employees = employees;
}
//获取指定位置index上的员工的设备
private Equipment creatEquipment(int index) {
int key = Integer.parseInt(EQUIPMENTS[index][0]);
String modelOrname = EQUIPMENTS[index][1];
switch(key) {
case PC://21
String display = EQUIPMENTS[index][2];
return new PC(modelOrname,display);
case NOTEBOOK:
double price = Double.parseDouble(EQUIPMENTS[index][2]);
return new NoteBook(modelOrname,price);
case PRINTER:
String type = EQUIPMENTS[index][2];
return new Printer(modelOrname,type);
}
return null;
}
//获取当前的所有员工
public Employee[] getAllEmployees() {
return employees;
}
//获取指定ID的员工对象
public Employee getEmployee(int id) throws TeamException {
for(int i = 0;i < employees.length;i ++) {
if(employees[i].getId() == id) {
return employees[i];
}
}
//简单抛出异常,但是不处理,等到用户调用时用try catch
throw new TeamException("找不到指定的员工!");
}
}
5.对加入团队进行操作
package com.team.service;
import com.team.domain.Architect;
import com.team.domain.Designer;
import com.team.domain.Employee;
import com.team.domain.Programmer;
public class TeamService {
private static int counter = 1;//团队ID
private final int MAX_MEMBER = 5;//最大成员数
private Programmer[] team = new Programmer[MAX_MEMBER];//programmer可以父类,创建programmer类型的数组
private int total = 0;//当前团队成员数
public TeamService() {
super();
}
public Programmer[] getTeam() {
//return team;会造成空指针异常,此时的team是全局变量的team
//新创建一个数组,将当前记录的团队成员复制到一个新数组中
Programmer[] team = new Programmer[total];
for(int i = 0; i < team.length;i ++) {
team[i] = this.team[i];
}
return team;
}
public void setTeam(Programmer[] team) {
this.team = 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("成员已经在团队中,无法添加");
}
//员工是某团队成员,按照状态status判断 或是正在休假
Programmer p = (Programmer)e;//做一个类型强转
if("busy".equalsIgnoreCase(p.getStatus().getNAME())) {
throw new TeamException("该员工已经是某团队成员");
}else if("vocation".equalsIgnoreCase(p.getStatus().getNAME())) {
throw new TeamException("该员工正在休假");
}
//-团队中至多只有一名架构师
// * -团队中至多有两名设计师
//* -团队中至多只有三名程序员
//首先要判断当前团队中已有的架构师、程序员、设计师人数
//判断时要先判断范围小的
int numOfArch = 0,numOfDes = 0,numOfPro = 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("当前已经有1名架构师,无法添加");
}
}else if(p instanceof Designer) {
if(numOfDes >= 2) {
throw new TeamException("当前已经有2名设计师,无法添加");
}
}else if(p instanceof Programmer) {
if(numOfPro >= 3) {
throw new TeamException("当前已经有3名程序员,无法添加");
}
}
//将p(或e)添加到现有team中
team[total ++ ] = p;
//p的赋值
p.setStatus(Status.BUSY);
p.setMemberId(counter++);
}
//从团队中删除成员
public void removeMember(int memberId) throws TeamException {
int i = 0;
for(;i < total; i ++) {
if(team[i].getMemberId() == memberId) {
team[i].setStatus(Status.FREE);
break;
}
}
//未找到指定id
if(i == total) {
throw new TeamException("未找到指定memberId的员工,删除失败");
}
for(int j = i + 1;j < total;j ++) {
team[j - 1] = team[j];
}
team[-- total] = null;
}
//判断员工是否已经在该团队中,遍历团队中的成员,看是否和要添加的员工id重复
public boolean isExit(Employee e) {
for(int i = 0; i < total;i ++) {
if(team[i].getId() == e.getId()) {
return true;
}
}
return false;
}
}
6.抛出异常机制
package com.team.service;
public class TeamException extends Exception {
static final long serialVersionUID = -3387514229948L;
public TeamException() {
super();
}
public TeamException(String msg) {
super(msg);
}
}
大致完成一个操作
‘