还是接着某谷的java天30基础的练习项目。我是照着敲的,注释都是我自己加的,当个复习基础的项目还不错!此项目为学完第七章异常之后做的项目,里面用到了JavaSE的核心基础,大家加油!
项目包接口如下图:
domain包
员工类(Employee)
public class Employee {
private int id; //员工id
private String name; //员工姓名
private int age; //员工年龄
private Integer salary; //员工薪水
public Employee() {
}
public Employee(int id, String name, int age, Integer salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setSalary(Integer salary) {
this.salary = salary;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getSalary() {
return salary;
}
/**
* 员工详细信息
* @return
*/
public String getDetails(){
return id + "\t" + name + "\t" + age+ "\t\t" +salary;
}
@Override
public String toString() {
return getDetails();
}
}
程序员继承员工类(Programmer)
public class Programmer extends Employee {
private int memberId; //员工id
private Status status = Status.FREE; //员工状态为自由
private Equipment equipment; //实现设备接口类型的对象
public Programmer() {
}
public Programmer(int id, String name, int age,
Integer salary, Equipment equipment) {
//调用父类全参构造器
super(id, name, age, salary);
//调用当前类的属性
this.equipment = equipment;
}
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;
}
public int getMemberId() {
return memberId;
}
public void setMemberId(int memberId) {
this.memberId = memberId;
}
protected String getMemberDetails() {
return getMemberId() + "/" + getDetails();
}
public String getDetailsForTeam() {
return getMemberDetails() + "\t程序员";
}
@Override
public String toString() {
return getDetails() + "\t程序员\t" + status + "\t\t\t\t\t" + equipment.getDescription();
}
}
设计师继承程序员类(Designer)
public class Designer extends Programmer{
private Integer bonus;
public Designer() {
}
public Designer(int id, String name, int age, Integer salary,
Equipment equipment, Integer bonus) {
super(id, name, age, salary, equipment);
this.bonus=bonus;
}
public Integer getBonus() {
return bonus;
}
public void setBonus(Integer bonus) {
this.bonus = bonus;
}
public String getDetailsForTeam() {
return getMemberDetails() + "\t设计师\t" + getBonus();
}
@Override
public String toString() {
return getDetails() + "\t设计师\t" + getStatus() + "\t" +
getBonus() +"\t\t\t" + getEquipment().getDescription();
}
}
架构师继承设计师类(Architect)
public class Architect extends Designer {
private int stock; //股票
public Architect() {
}
public Architect(int id, String name, int age, Integer salary,
Equipment equipment, Integer bonus, int stock) {
super(id, name, age, salary, equipment, bonus);
this.stock = stock;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
public String getDetailsForTeam() {
return getMemberDetails() + "\t架构师\t" +
getBonus() + "\t" + getStock();
}
@Override
public String toString() {
return getDetails() + "\t架构师\t" + getStatus() + "\t" +
getBonus() + "\t" + getStock() + "\t" + getEquipment().getDescription();
}
}
设备信息接口(Equipment)
public interface Equipment {
//描述设备信息方法
String getDescription();
}
台式电脑实现设备接口类(Desktop)
public class Desktop implements Equipment{
private String model;//机器型号
private String displayBrand;//显示器品牌
public Desktop() {
}
public Desktop(String model, String displayBrand) {
this.model = model;
this.displayBrand = displayBrand;
}
public void setModel(String model) {
this.model = model;
}
public String getModel() {
return model;
}
public String getDisplayBrand() {
return displayBrand;
}
public void setDisplayBrand(String displayBrand) {
this.displayBrand = displayBrand;
}
@Override
public String getDescription() {
return model +"("+displayBrand+")";
}
}
笔记本实现设备接口类(Laptop)
public class Laptop implements Equipment{
private String model;//机器的型号
private double price;//价格
public Laptop() {
}
public Laptop(String model, double price) {
this.model = model;
this.price = price;
}
public void setModel(String model) {
this.model = model;
}
public void setPrice(double price) {
this.price = price;
}
public String getModel() {
return model;
}
public double getPrice() {
return price;
}
//重写设备接口里的描述方法
@Override
public String getDescription() {
return model+"("+price+")";
}
}
打印机实现设备接口类(Printer)
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 void setName(String name) {
this.name = name;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
@Override
public String getDescription() {
return name + "(" + type + ")";
}
}
service包
所有数据类(Data)
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 DESKTOP = 21; //台式机全局常量
public static final int LAPTOP = 22; //笔记本全局常量
public static final int PRINTER = 23; //打印机全局常量
//String型员工二维数组全局常量
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"}
};
//String型设备二维数组全局常量
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"}
};
}
状态类(Status)
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 VOCATION = new Status("VOCATION");
//休假状态的常量
public static final Status BUSY = new Status("BUSY");
public String getNAME() {
return NAME;
}
@Override
public String toString() {
return NAME;
}
}
团队异常继承异常Exception类(TeamException)
public class TeamException extends Exception{
static final long serialVersionUID=-33875169124229948L;
public TeamException() {
}
public TeamException(String messsage){
super(messsage);
}
}
姓名列表的服务层类(NameListService)
public class NameListService {
private Employee[] employees; //员工类型数组
//空参构造器的实例
public NameListService() {
//一维员工数组的长度为Data类里定义的员工二维数组的常量员工数
employees = new Employee[EMPLOYEES.length];
//遍历员工数并且赋值员工信息
for (int i = 0; i < employees.length; i++) {
//员工类型
int type = Integer.parseInt(EMPLOYEES[i][0]);
//员工id
int id = Integer.parseInt((EMPLOYEES[i][1]));
//员工姓名
String name = EMPLOYEES[i][2];
//员工年龄
int age = Integer.parseInt(EMPLOYEES[i][3]);
//员工薪水
Integer salary = Integer.parseInt(EMPLOYEES[i][4]);
Equipment equipment; //实现设备接口类型的对象
Integer bonus; //补贴奖金数
int stock; //股票数量
switch (type) {
//员工的常量被赋值为11.所以直接写case后面加常量名字
case EMPLOYEE:
employees[i] = new Employee(id, name, age, salary);
break;
//程序员的常量赋值为12
case PROGRAMMER:
equipment = createEquipment(i);
employees[i] = new Programmer(id, name, age, salary, equipment);
break;
//设计师的常量赋值为13
case DESIGNER:
equipment = createEquipment(i);
bonus=Integer.parseInt(EMPLOYEES[i][5]);
employees[i] = new Designer(id, name, age, salary, equipment, (int) bonus);
employees[i]=new Designer(id,name,age,salary,equipment,bonus);
break;
//架构师的常量赋值为14
case ARCHITECT:
equipment = createEquipment(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;
}
}
}
/**
* 根据索引返回实现设备接口类型的属性
*
* @param index
* @return
*/
private Equipment createEquipment(int index) {
int type = Integer.parseInt(EQIPMENTS[index][0]);
switch (type) {
//台式机的常量值被定义为21
case DESKTOP:
return new Desktop(EQIPMENTS[index][1], EQIPMENTS[index][2]);
//笔记本电脑的常量值被定义为22
case LAPTOP:
int price = Integer.parseInt(EQIPMENTS[index][2]);
return new Laptop(EQIPMENTS[index][1], price);
//打印机的常量值被定义为23
case PRINTER:
return new Printer(EQIPMENTS[index][1], EQIPMENTS[index][2]);
}
return null;
}
/**
* 返回获得所有员工数组的方法
*/
public Employee[] getAllEmplouyees() {
return employees;
}
/**
* 根据员工id查询员工信息,手动抛出一个自定义Team异常
*/
public Employee getEmployee(int id) throws TeamException {
for (Employee e : employees) {
if (e.getId() == id) {
return e;
}
}
//处理抛出异常
throw new TeamException("该员工不存在");
}
}
团队服务层类(TeamService)
public class TeamService {
private static int counter = 1; //用于自动生成团队成员的memberId
private final int MAX_MEMBER = 6; //团队人数上限
private Programmer[] team = new Programmer[MAX_MEMBER]; //保存当前团队成员
private int total = 0; //团队实际人数
public TeamService() {
}
/**
* 返回team中所有程序员构成的数组
*/
public Programmer[] getTeam() {
Programmer[] team = new Programmer[total];
for (int i = 0; i < total; i++) {
team[i] = this.team[i];
}
return team;
}
/**
* 填加成员(员工类型)&手动抛出异常
*
* @param e
* @throws TeamException
*/
public void addMember(Employee e) throws TeamException {
if (total >= MAX_MEMBER) {
throw new TeamException("成员已满,无法添加");
}
// 对象(object) instanceof 类(class)判断对象是否属于该类,是返回true,否返回false。
if (!(e instanceof Programmer)) {
throw new TeamException(("该成员不是开发人员,无法添加"));
}
//Employee向下转型(父类对象调用子类方法)
Programmer p = (Programmer) e;
if (isExist(p)) {
throw new TeamException("该员工已在本团队中");
}
if (p.getStatus().getNAME().equals("BUSY")) {
throw new TeamException("该员工已是团队成员");
} else if (p.getStatus().getNAME().equals("VOCATION")) {
throw new TeamException("该员工正在休假,无法添加");
}
//架构师,设计师,程序员数量
int numOfArchitect = 0, numOfDesigner = 0, numOfProgrammer = 0;
for (int i = 0; i < total; i++) {
//if里面如果只有一条语句,则可以省略括号
if (team[i] instanceof Architect) numOfArchitect++;
else if (team[i] instanceof Designer) numOfDesigner++;
else if (team[i] instanceof Programmer) numOfProgrammer++;
}
if (p instanceof Architect) {
if (numOfArchitect >= 1) throw new TeamException("团队中只能有一名架构师");
} else if (p instanceof Designer) {
if (numOfDesigner >= 2) throw new TeamException("团队中只能有两名设计师");
} else if (p instanceof Programmer) {
if (numOfProgrammer >= 3) throw new TeamException("团队中至多只能有三名程序员");
}
//添加到数组
p.setStatus(Status.BUSY);
p.setMemberId(counter++);
team[total++] = p;
}
/**
* 判断员工是否属于程序员
*/
private boolean isExist(Programmer programmer) {
for (int i = 0; i < total; i++) {
if (team[i].getId() == programmer.getId()) {
return true;
}
}
return false;
}
/**
* 删除指定memberId的程序员
*
* @param menberId
* @throws TeamException
*/
public void removeMember(int menberId) throws TeamException {
int n = 0;
//找到指定memberId的员工,并删除
for (; n < total; n++) {
if (team[n].getMemberId() == menberId) {
team[n].setStatus(Status.FREE);
break;
}
}
//如果遍历一遍,都找不到,则报异常
if (n == total)
throw new TeamException("找不到该成员,无法删除");
//后面的元素覆盖前面的元素
for (int i = n + 1; i < total; i++) {
team[i - 1] = team[i];
}
team[--total] = null;
}
}
view包
团队调度工具类(TeamServiceuUtility)
public class TeamServiceuUtility {
private static Scanner scanner = new Scanner(System.in);
/**
* 根据用户键入字符选择菜单
* @return
*/
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;
}
/**
* 返回用户键入的100个字符的字符串
*/
public static void readReturn() {
System.out.print("按回车键继续...");
readKeyBoard(100, true);
}
/**
* 返回用户键入的数字
* @return
*/
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
*/
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;
}
/**
* 返回用户键入的字符串,长度为limit
* @param limit
* @param blankReturn
* @return
*/
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;
}
}
应用主界面(TeamView)
public class TeamView {
//封装的姓名列表类
private NameListService nameListService = new NameListService();
//封装的团队服务层
private TeamService teamService = new TeamService();
/**
* 应用主菜单
*/
public void enterMainMenu() {
boolean loogFlag = true;
char key = 0;
while (loogFlag) {
if (key != '1') {
listAllEmployees();
System.out.print("1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 请选择(1-4):");
key=TeamServiceuUtility.readMenuSelection();
System.out.println();
switch (key){
case '1':
listTeam();
break;
case '2':
addMember();
break;
case '3':
deleteMember();
break;
case '4':
System.out.println("确认是否退出(Y/N):");
char isExit=TeamServiceuUtility.readConfirmSelection();
if(isExit=='Y'){
loogFlag=false;
}
break;
}
}
}
}
/**
* 显示所有的员工成员
*/
private void listAllEmployees() {
System.out
.println("\n-------------------------------开发团队调度软件--------------------------------\n");
Employee[] employees = nameListService.getAllEmplouyees();
if (employees.length == 0) {
System.out.println("没有客户记录!");
} else {
System.out.println("ID\t姓名\t年龄\t工资\t职位\t状态\t奖金\t股票\t领用设备");
}
//增强for循环遍历员工信息
for (Employee e : employees) {
System.out.println(" " + e);
}
System.out
.println("-------------------------------------------------------------------------------");
}
/**
* 显示开发团队成员列表
*/
private void listTeam() {
System.out
.println("\n--------------------团队成员列表---------------------\n");
Programmer[] team = teamService.getTeam();
if (team.length == 0) {
System.out.println("开发团队目前没有成员!");
} else {
System.out.println("TID/ID\t姓名\t年龄\t工资\t职位\t奖金\t股票");
}
for (Programmer p : team) {
System.out.println(" " + p.getDetailsForTeam());
}
System.out
.println("-----------------------------------------------------");
}
/**
* 添加成员到团队
*/
private void addMember() {
System.out.println("---------------------添加成员---------------------");
System.out.print("请输入要添加的员工ID:");
int id = TeamServiceuUtility.readInt();
try {
Employee e = nameListService.getEmployee(id);
teamService.addMember(e);
System.out.println("添加成功");
} catch (TeamException e) {
System.out.println("添加失败,原因:" + e.getMessage());
}
//按回车键继续...
TeamServiceuUtility.readReturn();
}
/**
* 从团队中删除指定id的成员
*/
public void deleteMember() {
System.out.println("---------------------删除成员---------------------");
System.out.print("请输入要删除员工的TID:");
int id = TeamServiceuUtility.readInt();
System.out.println("确认是否删除(Y/N):");
//调用验证删除方法
char isDelete = TeamServiceuUtility.readConfirmSelection();
if (isDelete == 'N') {
return;
}
try {
teamService.removeMember(id);
System.out.println("删除成功");
} catch (TeamException e) {
System.out.println("删除失败,原因:" + e.getMessage());
}
//按回车键继续...
TeamServiceuUtility.readReturn();
}
public static void main(String[] args) {
//程序入口
TeamView teamView = new TeamView();
teamView.enterMainMenu();
}
}
End,至此结束,新手们有啥不懂的问我就好(大佬勿喷),一起加油啊!