JavaSE基础部分今天完结撒花了,做了一个人员调度软件检验学习成果。话不多说,贴代码!!!!
- 软件代码我分别写在了四个Package下
- 其中TeamJunit 是一个单元测试类,可以不用写上去
1.1 domain package < Architect>
package domain;
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 int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
@Override
public String toString() {
return getDetails()+"\t架构师"+"\t"+getStatus()+"\t"+getBonus()+"\t"+stock+"\t"+getEquipment().getDescription();
}
public String getDatilesForTeam() {
return getMemberId()+"/" +getId()+"\t"+getName()+"\t"+getAge()+"\t"+getSalary()+"\t架构师\t"+getBonus()+"\t"+getStock();
}
}
1.2 domain package < Designer>
package domain;
import java.io.ObjectInputFilter.Status;
public class Designer extends Programmer {
private double 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 double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
@Override
public String toString() {
return getDetails()+"\t设计师"+"\t"+getStatus()+"\t"+bonus+"\t"+"\t"+getEquipment().getDescription();
}
public String getDatilesForTeam() {
return getMemberId()+"/" +getId()+"\t"+getName()+"\t"+getAge()+"\t"+getSalary()+"\t设计师\t"+getBonus();
}
}
1.3 domain package < Empolyee>
package domain;
public class Empolyee {
private int Id;
private String name;
private int age;
private double salary;
public Empolyee() {
super();
}
public Empolyee(int id, String name, int age, double salary) {
super();
Id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public int getId() {
return Id;
}
public void setId(int id) {
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 getDetails() {
return Id+"\t"+name+"\t"+age+"\t"+salary+"\t";
}
@Override
public String toString() {
return getDetails();
}}
1.4 domain package < Equipment>
package domain;
public interface Equipment {
public abstract String getDescription();//抽象方法
}
1.5 domain package < NoteBook>
package domain;
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+")";
}
}
1.6 domain package < Pc>
package domain;
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() {
return model+"("+ display+")";
}
}
1.7 domain package < Printer>
package domain;
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+")";
}
}
1.8 domain package < Programmer>
package domain;
import service.*;
public class Programmer extends Empolyee {
private int memberId;//团队ID
private Status status=Status.FREE;//员工的状态 枚举类(类里面有有限个对象,对象是有限的)
private Equipment equipment;//设备
public Programmer() {
super();
}
public Programmer(int id, String name, int age, double salary,Equipment equipment) {
super(id, name, age, salary);
this.equipment = equipment;
}
public int getMemberId() {
return memberId;
}
public void setMemberId(int memberId) {
this.memberId = memberId;
}
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 getDetails()+"\t程序员\t"+status+"\t\t\t"+equipment.getDescription();
}
public String getDatilesForTeam() {
return memberId+"/" +getId()+"\t"+getName()+"\t"+getAge()+"\t"+getSalary()+"\t程序员";
}
}
2.1 service Package < Data>
package service;
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;
// 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", "史玉柱", "26", "26", "9800", "5500" }, { "11", "9", "丁磊", "21", "6600" },
{ "11", "10", "张朝阳", "25", "7100" }, { "12", "12", "杨致远", "27", "9600", "4800" } };
// 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", "佳能", "激光" }, { "21", "华硕", "三星17寸" }, { "21", "华硕", "三星17寸" },
{ "23", "爱普生", "针式" }, { "22", "惠普m6", "5800" }, { "21", "戴尔", "NEC17寸" }, { "21", "华硕", "三星17寸" },
{ "22", "惠普m6", "5800" },
};
}
2.2 service Package < NmaeListservice>
package service;
import static service.Data.*;//导入所有静态的方法和属性
/**
* @Description 负责将Data 中的数据封装到Employee[]数组中,同时提供相关操作Employee[]的方法。
* @author 23147
*
*/
import org.w3c.dom.NameList;
import domain.Architect;
import domain.Designer;
import domain.Empolyee;
import domain.Equipment;
import domain.NoteBook;
import domain.Pc;
import domain.Printer;
import domain.Programmer;
public class NmaeListService {
private Empolyee[] empolyees;
/**
* 给数组及数组元素初始化
*
*/
public NmaeListService() {
//根据项目提供的Data类构建相应的employees数组
//再根据Data类中的数据构建不同的对象,包括Employee,programmer,Designer,和Architect
//将对象存于数组中
empolyees = new Empolyee[Data.EMPLOYEES.length];
for ( int i = 0;i < empolyees.length; i++) {
int type = Integer.parseInt(Data.EMPLOYEES[i][0]);
// 获取Employee 的四个基本信息
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:
empolyees[i]=new Empolyee(id,name,age,salary);
break;
case PROGRAMMER:
equipment=createEquipment(i);
empolyees[i]=new Programmer(id, name, age, salary, equipment);
break;
case DESIGNER:
equipment=createEquipment(i);
bonus =Double.parseDouble(EMPLOYEES[i][5]);
empolyees[i]=new Designer(id, name, age, salary, equipment, bonus);
break;
case ARCHITECT:
equipment=createEquipment(i);
bonus =Double.parseDouble(EMPLOYEES[i][5]);
stock=Integer.parseInt(EMPLOYEES[i][6]);
empolyees[i]=new Architect(id, name, age, salary, equipment, bonus, stock);
break;
default:
break;
}
}
}
/**
* 获取指定index 位置上的员工的设备
* @param i
* @return
*/
private Equipment createEquipment(int index) {
int type=Integer.parseInt(EQUIPMENTS[index][0]);
String model=EQUIPMENTS[index][1];
switch (type) {
case PC://21
String display=EQUIPMENTS[index][2];
return new Pc(model,EQUIPMENTS[index][2]);
case NOTEBOOK://22
double price=Double.parseDouble(EQUIPMENTS[index][2]);
return new NoteBook(model, price);
case PRINTER://23
String type1=EQUIPMENTS[index][2];
return new Printer(model, type1);
default:
}return null;
}
/**
* 获取当前所有的员工
* @return
*/
public Empolyee[] getAllEmpolyees() {
return empolyees;
}
/**
* 获取指定员工的ID的对象
* @param id
* @return
* @throws TeamException
*/
public Empolyee getEmpolyee(int id) throws TeamException {
for (int i = 0; i < empolyees.length; i++) {
if(empolyees[i].getId()==id) {
return empolyees[i];
}
}
throw new TeamException("找不到指定员工");
}
}
2.3 service Package < Status>
package service;
/**
* @Description 表示员工的状态
* @author 23147
*
*/
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 BUSY = new Status("BUSY");
public static final Status VOCATION = new Status("VOCATION");
public String getNAME() {
return NAME;
}
@Override
public String toString() {
return NAME;
}
}
2.4 service Package < TeamException>
package service;
/**
* 自定义异常类
* @author 23147
*
*/
public class TeamException extends Exception {
static final long serialVersionUID=-1545562554;
public TeamException() {
super();
}
public TeamException(String msg) {
super(msg);
}
}
2.5 service Package < TeamService >
package service;
import domain.Architect;
import domain.Designer;
import domain.Empolyee;
import domain.Programmer;
/**
* 关于开发团队的增加,删除
*
* @author 23147
*
*/
public class TeamService {
private static int counter = 1;// 给member id 赋值使用
private final int MAX_MEMBER = 5;// 限制开发团队的人数
private Programmer[] team = new Programmer[MAX_MEMBER];// 保存开发团队成员
private int total;// 记录开发团队中的实际人数
/**
* 获取开发团队中的所有成员
*
* @return
*/
public Programmer[] getTeam() {
Programmer[] teamProgrammers = new Programmer[total];
for (int i = 0; i < teamProgrammers.length; i++) {
teamProgrammers[i] = this.team[i];
}
return teamProgrammers;
}
/**
* 将指定的员工添加到开发团队中
*
* @param empolyee
* @throws TeamException
*/
public void addMember(Empolyee empolyee) throws TeamException {
// 成员已满无法添加
if (total >= MAX_MEMBER) {
throw new TeamException("成员已满,无法添加");
}
// 该成员是否为开发人员
if (!(empolyee instanceof Programmer)) {
throw new TeamException("该人员不是开发人员,无法添加");
}
// 该人员已在开发团队中
if (isExist(empolyee)) {
throw new TeamException("该人员已在开发团队在,无法添加");
}
// 该员工已是某团队的成员 (看状态)
// 该员工正在休假
Programmer programmer = (Programmer) empolyee;// 一定不会出现类型转换异常
// if(programmer.getStatus().getNAME().equals("BUSY"))
if ("BUSY".equalsIgnoreCase(programmer.getStatus().getNAME()))// 优化写法,避免出现空指针
{
throw new TeamException("已经是某团队的成员");
} else if ("VOCATION".equalsIgnoreCase(programmer.getStatus().getNAME())) {
throw new TeamException("该员工正在休假");
}
// 团队中最多只能有一名架构师
// 团队中只能由两名设计师。
// 团队中至多三名程序员
// 获取team中已有的成员中架构师,设计师,程序员的个数
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 {
numOfPro++;
}
}
if(programmer instanceof Architect) {
if(numOFArch>=1) {
throw new TeamException("团队中最多只能有一名架构师");
}
else if(numOfDes>=2) {
throw new TeamException("团队中只能由两名设计师。");
}else if (numOfPro>=3) {
throw new TeamException(" 团队中至多三名程序员");
}
}
//将employee 添加现有的团队中
team[total++]=programmer;
//开发团队中的属性赋值
programmer.setStatus(Status.BUSY);
programmer.setMemberId(counter++);
}
/**
* 判断指定的员工是否,已经存在于开发团队中
*
* @param empolyee
* @return
*/
private boolean isExist(Empolyee empolyee) {
for (int i = 0; i > total; i++) {
if (team[i].getId() == empolyee.getId()) {
return true;
}
}
return false;
}
/**
* 从团队中删除成员
* @param Memberid
* @throws TeamException
*/
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;
}
}
//删除一个元素,后面的元素覆盖前一个元素,实现删除操作,最后一个元素置空
for(int j=i+1;j<total;j++) {
team[j-1]=team[j];
}
team[total-1]=null;
total--;
//判断是否找到指定memberid
if(i==total) {
throw new TeamException("未找到指定员工");
}
}
}
3.1 View Package < Teamview >
package view;
import domain.Empolyee;
import domain.Programmer;
import service.TeamService;
import service.NmaeListService;
import service.TeamException;
public class TeamView {
private NmaeListService nameListService = new NmaeListService();
private TeamService teamService = new TeamService();
char menu = 0;
public void enterMainMenu() {
boolean loopFlag = true;
while (loopFlag) {
if (menu != '1') {
ListAllEmployees();
}
System.out.println("1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 请选择(1-4)");
menu = TSUtility.readMenuSelection();
switch (menu) {
case '1':
ListAllEmployees();
getTeam();
break;
case '2':
addMember();
break;
case '3':
deleteMember();
break;
case '4':
System.out.println("是否要退出(Y/N)");
char isExit = TSUtility.readConfirmSelection();
if (isExit == 'Y') {
loopFlag = false;
}
break;
}
}
}
/**
* 显示所有员工信息
*/
private void ListAllEmployees() {
System.out.println("------------------------开发团队调度软件----------------------------/n");
Empolyee[] empolyees = nameListService.getAllEmpolyees();
if (empolyees.length == 0 || empolyees == null) {
System.out.println("公司中没有任何员工信息");
} else {
System.out.println("ID\t姓名\t年龄\t工资\t职位\t状态\t奖金\t股票\t领用设备");
for (int i = 0; i < empolyees.length; i++) {
System.out.println(empolyees[i]);
}
}
System.out.println("-------------------------------------------------------------");
}
private void getTeam() {
System.out.println("-------------------------团队成员列表------------------------");
Programmer[] teamProgrammers = teamService.getTeam();// 获取团队成员
if (teamProgrammers == null || teamProgrammers.length == 0) {
System.out.println("团队中没有成员!");
} else {
System.out.println("TID/ID\t姓名\t年龄\t工资\t职位\t奖金\t股票\n");
for (int i = 0; i < teamProgrammers.length; i++) {
System.out.println(teamProgrammers[i].getDatilesForTeam());
}
}
System.out.println("------------------------------------------------------");
}
private void addMember() {
System.out.println("--------------------------添加成员----------------------");
System.out.println("输入添加成员ID:");
int id1 = TSUtility.readInt();
try {
Empolyee empolyee = nameListService.getEmpolyee(id1);
teamService.addMember(empolyee);
System.out.println("添加成功");
} catch (TeamException e) {
System.out.println("添加失败,原因" + e.getMessage());
}
TSUtility.readReturn();
}
private void deleteMember() {
System.out.println("--------------------------删除团队成员----------------------");
System.out.println("输入删除团队成员ID:");
int memnberid = TSUtility.readInt();
System.out.println("确认是否删除 Y 或N");
char isDelete = TSUtility.readConfirmSelection();
if (isDelete == 'Y') {
try {
teamService.removeMember(memnberid);
System.out.println("删除成功");
} catch (TeamException e) {
System.out.println("删除失败,原因"+e.getMessage());
}
TSUtility.readReturn();
}return;
}
public static void main(String[] args) {
TeamView teamView = new TeamView();
teamView.enterMainMenu();
}
}
3.2 View Package < Tsutility >
package view;
import java.util.Scanner;
/**
* @Description 项目中提供了TSUtility .java 类,可以用来方便的实现键盘访问。
* @author 23147
* @version
* @date 10点36分
*/
public class TSUtility {
private static Scanner scanner = new Scanner(System.in);
/**
* @Description 该方法读取键盘,如果用户键入‘1’-‘4’ 中任意的字符,则方法返回,返回值为用户键入的数值
*/
public static char readMenuSelection() {
char c;
for (;;) {
String string = readKeyBoard(1, false);
c = string.charAt(0);
if (c != '1' && c != '2' && c != '3' && c != '4') {
System.out.println("输入有误,请重新输入");
} else
break;
}
return c;
}
/**
*
* @Description 该方法提示并等待,直到用户按回车后返回。
* @author
* @return
*/
public static void readReturn() {
System.out.println("按回车键继续...");
readKeyBoard(100, true);
}
/**
* @Description 该方法从键盘上读取一个长度不超过2位的整数,并将其作为方法的返回值。
* @return
*/
public static int readInt() {
int n;
for (;;) {
String string = readKeyBoard(2, false);
try {
n = Integer.parseInt(string);
break;
} catch (NumberFormatException e) {
System.out.println("数字输入错误,请重新输入");
}
}return n;
}
/**
* @Description 从键盘读取’Y‘或’N‘,并将其作为方法的返回值
* @return
*/
public static char readConfirmSelection() {
char c;
for (;;) {
String string = readKeyBoard(1, false);
c = string.charAt(0);
if (c == 'Y' || c == 'N') {
break;
} else {
System.out.println("输入错误,请重新输入");
}
}
return c;
}
private static String readKeyBoard(int limit, boolean blankReturn) {
String line = "";
while (scanner.hasNext()) {
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;
}
}