最近忙了點别的事情,學習進度收到一點點耽誤,終于在昨天把project03給完結了,下面對project03進行複盤學習一下。project03整體還是采用MVC的設計模式,每個包的功能明顯。
M:
這裡面主要是涉及到一個父類和一個接口的問題,一個Employee類和一個Equipment接口。
其中由于接口中不能有構造器,不能對接口執行個體化,大多數還是在接口中寫抽象方法,進而再實作接口。Equipment接口代碼如下:
package project03.team.domain;
public interface Equipment {
public abstract String getDescription();
}
實作接口的代碼有PC、NoteBook和Printer類,下面是三個實作類的代碼。
PC類:有兩個屬性,一個是機器型号,另一個是顯示器名稱,另外是構造器和get/set方法
package project03.team.domain;
public class PC implements Equipment {
private String model;// 機器型号
private String display;// 顯示器名稱
// 接口實作
public String getDescription() {
return model+"("+display+")";
}
// 構造器
public PC() {
super();
}
public PC(String model, String display) {
super();
this.model = model;
this.display = display;
}
// get/set方法
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;
}
}
NoteBook類:有兩個屬性,分别是機器型号和價格,其餘是構造器和get/set方法
package project03.team.domain;
public class NoteBook implements Equipment {
private String model;//機器型号
private double price;//價格
@Override
public String getDescription() {
return model + "(" + price + ")";
}
// get/set方法
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;
}
// 構造器
public NoteBook(String model, double price) {
super();
this.model = model;
this.price = price;
}
public NoteBook() {
super();
}
}
Printer類:有連個屬性,分别是機器型号和機器類型,其餘為get/set方法和構造器
package project03.team.domain;
public class Printer implements Equipment {
private String name;//機器型号
private String type;//機器類型
public String getDescription() {
return name + "(" + type + ")";
}
// get/set方法
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;
}
// 構造器
public Printer() {
super();
}
public Printer(String name, String type) {
super();
this.name = name;
this.type = type;
}
}
接下來是Employee父類,裡面有員工的基本屬性:id,姓名,年齡,薪水,以及對應的get/set方法和構造器。
package project03.team.domain;
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;
}
// get/set方法
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 getDetails() {
return id + "\t" + name + "\t" + age + "\t" + salary;
}
// toString方法的重寫
@Override
public String toString() {
return getDetails();
}
}
有三個工種,分别是Programmer、Designer、Architect,其中Designer繼承Programmer,Architect繼承Designer。
Programmer類:有三個屬性,分别是開發團隊中的id,所處的狀态,裝置。以及對應的構造器和get/set方法。
package project03.team.domain;
import project03.team.service.Status;
public class Programmer extends Employee{
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;
}
//get/set方法
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 super.getDetails()+"\t程式員\t"+status+"\t\t\t"+equipment.getDescription();
}
public String getDetailsForTeam() {
return memberId+"/"+getId()+"\t"+getName()+"\t"+getAge()+ "\t"+getSalary()+"\t"+"程式員";
}
}
Designer類:還有自己的獎金屬性及對應的get/set方法和構造器。
package project03.team.domain;
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 Designer(double bonus) {
super();
this.bonus = bonus;
}
@Override
public String toString() {
return getDetails() + "\t設計師\t" + getStatus() + "\t" + bonus + "\t\t" + getEquipment().getDescription();
}
public String getDetailsForTeam() {
return getMemberId() + "/" + getId() + "\t" + getName() + "\t" + getAge() + "\t" + getSalary() + "\t" + "設計師"+ "\t"+getBonus();
}
}
Architect類:有自己的stock屬性及對應構造器和get/set方法
package project03.team.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;
}
// get/set方法
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 getDetailsForTeam() {
return getMemberId() + "/" + getId() + "\t" + getName() + "\t" + getAge() + "\t" + getSalary() + "\t" + "架構師"+ "\t"
+ getBonus()+getStock();
}
}
C:
主要有Data類用于存放資料,Status類表示員工狀态,TeamException類是自定義異常,NameListService類對Data資料進行封裝到employees數組中,TeamService用于對團隊成員進行增删改查。
Data類:
package project03.team.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;
//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
/**
* {"10", "1", "馬雲", "22", "3000"},
* {"13", "2", "馬化騰", "32", "18000", "15000", "2000"},
*/
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"}
};
}
Status類:這個類寫的時候是将一些常量封裝在了這個類中。
package project03.team.service;
/**
*
* @Description 表示員工的狀态
* @author
* @version
* @date 2022年1月3日上午11:17:26
*/
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;
}
}
TeamException類:自定義異常類三步走:繼承Exception或者RuntimeException、聲明辨別、構造器。
package project03.team.service;
/**
*
* @Description 自定義異常類
* @author
* @version
* @date 2022年1月5日下午4:30:19
*/
public class TeamExeption extends Exception{
static final long serialVersionUID = -33875169124229948L;
public TeamExeption() {
super();
}
public TeamExeption(String msg) {
super(msg);
}
}
NameLIstSerice類:将Data中的資料封裝到一個Employee[]類型的employees數組中。
幾步走:
1.确定employees數組的長度,根據Data中的EMPLOYEE二維數組中的外層元素數量确定。
2.周遊employees數組,對數組元素的每一個值進行指派,每個位置是Employee類型或者是它的子類,展現多态性。
3.由于不知道到底是什麼類型,是以要有一步确定成員類型的操作,根據Data.EMPLOYEES數組中的記憶體第一個元素确定類型。這裡注意到需要将String類型轉換成對應的類型,利用包裝類Integer.ParseInt或者Double.ParseInt等轉換。
4.通過switch語句可以将employees每個位置new好一個對應的對象。
5.在new對象的過程調用到帶有Equipment構造器的對象時,需要對每一個對象針對Equipment屬性進行配置設定,是以要定義一個根據索引進行配置設定裝置的方法,這裡叫createEquipment。createEquipment方法是以Equipment為傳回值類型,通過Data.EQUIPMENTS[index][0],内層元素第一個确定裝置類型,進而new裝置對象。
這樣就解決了根據Data資料new對象的問題。
此外,這個NameLIstSerice類中還有getAllEmployees方法用于傳回所有的員工,即return employees;還有一個傳回指定位置上的員工,通過比對Id進行周遊,如果周遊不到,抛出異常找不到指定員工。
代碼如下:
package project03.team.service;
import project03.team.domain.Architect;
import project03.team.domain.Designer;
import project03.team.domain.Employee;
import project03.team.domain.Equipment;
import project03.team.domain.NoteBook;
import project03.team.domain.PC;
import project03.team.domain.Printer;
import project03.team.domain.Programmer;
/**
*
* @Description 負責将Data中資料封裝到Employee[]數組中,同時提供相關操作Employee[]的方法
* @author
* @version v1.0
* @date 2022年1月3日下午12:58:36
*/
public class NameListService {
private Employee[] employees;
/*
* 給數組employees及數組元素初始化
*/
public NameListService() {
employees=new Employee[Data.EMPLOYEES.length];
for(int i=0;i<employees.length;i++) {
//擷取員工類型
int type = Integer.parseInt(Data.EMPLOYEES[i][0]);
//擷取Employee的4個基本資訊
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;
int stock;
switch(type) {
case Data.EMPLOYEE:
employees[i]=new Employee(id, name, age, salary);
break;
case Data.PROGRAMMER:
equipment=creatEquuipment(i);
employees[i]=new Programmer(id, name, age, salary, equipment);
break;
case Data.DESIGNER:
equipment=creatEquuipment(i);
bonus=Double.parseDouble(Data.EMPLOYEES[i][5]);
employees[i]=new Designer(id, name, age, salary, equipment, bonus);
break;
case Data.ARCHITECT:
equipment=creatEquuipment(i);
bonus=Double.parseDouble(Data.EMPLOYEES[i][5]);
stock=Integer.parseInt(Data.EMPLOYEES[i][6]);
employees[i]=new Architect(id, name, age, salary, equipment, bonus, stock);
break;
}
}
}
/**
*
* @Description 擷取指定index位置上的員工的裝置
* @author
* @date 2022年1月5日下午3:49:18
* @param i
* @return
*/
private Equipment creatEquuipment(int index) {
int type = Integer.parseInt(Data.EQUIPMENTS[index][0]);
String model=Data.EQUIPMENTS[index][1];
switch(type) {
case Data.PC://21
String display=Data.EQUIPMENTS[index][2];
return new PC(model, display);
case Data.NOTEBOOK://22
double price=Double.parseDouble(Data.EQUIPMENTS[index][2]);
return new NoteBook(model, price);
case Data.PRINTER://23
String printerType=Data.EQUIPMENTS[index][2];
return new Printer(model, printerType);
}
return null;
}
/**
*
* @Description 擷取目前所有員工
* @author
* @date 2022年1月5日下午4:24:59
* @return
*/
public Employee[] getAllEmployees() {
return employees;
}
/**
*
* @Description 擷取指定位置的員工
* @author
* @date 2022年1月5日下午4:26:03
* @param id
* @return
*/
public Employee getEmployee(int id) throws TeamExeption{
for(int i=0;i<employees.length;i++) {
if(employees[i].getId()==id) {
return employees[i];
}
}
throw new TeamExeption("找不到指定的員工");
}
}
TeamService類:主要完成對開發團隊成員的管理、增加、删除等。有total屬性和team屬性,total用于儲存目前開發 團隊中的人數,team用于儲存目前開發團隊的人員。
獲得團隊成員:這裡new了一個新的數組,傳回這個新new的數組
添加成員操作,這裡有幾個判斷,如果不滿足條件不能進行添加,滿足條件則team[total]=p,p是要添加的成員。
删除成員也是一個經典的操作,周遊數組,先看一看能不能找到對應的員工,找不到抛異常。删除操作是從找到的那一個開始替換為後一個,最後一個設定為null,total要--。
package project03.team.service;
import project03.team.domain.Architect;
import project03.team.domain.Designer;
import project03.team.domain.Employee;
import project03.team.domain.Programmer;
/**
*
* @Description 關于開發團隊成員的管理、增加、删除等。
* @author
* @version
* @date 2022年1月5日下午5:40:22
*/
public class TeamService {
private static int counter = 1;// 給memberid指派使用
private static final int MAX_MEMBER = 5;
private Programmer[] team = new Programmer[MAX_MEMBER];// 儲存開發團隊成員
private int total;// 記錄開發團隊中實際的人數
/**
*
* @Description 擷取開發團隊中的成員
* @author
* @date 2022年1月5日下午5:47:29
* @return
*/
public Programmer[] getTeam() {
Programmer[] team = new Programmer[total];
for (int i = 0; i < team.length; i++) {
team[i] = this.team[i];
}
return team;
}
/**
*
* @Description 将指定員工添加到開發團隊中
* @author
* @date 2022年1月5日下午6:31:33
* @param e
*/
public void addMember(Employee e) throws TeamExeption{
if (total >= MAX_MEMBER) {
throw new TeamExeption("成員已滿,無法添加");
}
if(!(e instanceof Programmer)) {
throw new TeamExeption("該成員不是開發人員,無法添加");
}
if(isExit(e)) {
throw new TeamExeption("該員工已在本開發團隊中");
}
Programmer p=(Programmer)e;//一定不會出現類型轉換異常
if("BUSY".equalsIgnoreCase(p.getStatus().getNAME())){//if(p.getStatus().getNAME().equals("BUSY")) {
throw new TeamExeption("該員工已經是某團隊成員");
}else if("VOCATION".equalsIgnoreCase(p.getStatus().getNAME())) {
throw new TeamExeption("該員工正在休假");
}
//擷取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 if(team[i] instanceof Programmer) {
numOfPro++;
}
}
if(p instanceof Architect) {
if(numOfArch>=1) {
throw new TeamExeption("團隊中至多有一個架構師");
}
}
if(p instanceof Designer) {
if(numOfDes>=2) {
throw new TeamExeption("團隊中至多有兩個設計師");
}
}
if(p instanceof Programmer) {
if(numOfPro>=3) {
throw new TeamExeption("團隊中至多有三個程式員");
}
}
//将p添加到現有的team中
team[total]=p;
total++;
//p的屬性指派
p.setStatus(Status.BUSY);
p.setMemberId(counter++);
}
/**
*
* @Description 判斷指定的員工是否已經存在于本開發團隊中
* @author
* @date 2022年1月5日下午6:41:03
* @param e
* @return
*/
private boolean isExit(Employee e) {
for(int i=0;i<total;i++) {
if(team[i].getId()==e.getId()) {
return true;
}
}
return false;
}
/**
*
* @Description 從團隊中删除成員
* @author
* @date 2022年1月5日下午7:45:41
* @param memberId
* @throws TeamExeption
*/
public void removeMember(int memberId) throws TeamExeption {
int i=0;
for(;i<total;i++) {
if(team[i].getMemberId()==memberId) {
team[i].setStatus(Status.FREE);
break;
}
}
//未找到指定id情況
if(i==total) {
throw new TeamExeption("找不到指定memberId員工,删除失敗");
}
//後一個元素覆寫前一個元素,實作删除操作
for(int j=i+1;j<total;j++) {
team[j-1]=team[j];
}
team[total-1]=null;
total--;
}
}
V:
裡面主要有兩個類,一個是TSUtility工具類,主要實作一些鍵盤操作,另外一個是TeamView類,實作與使用者的互動。
TSUtility類:
package project03.team.view;
import java.util.*;
/**
*
* @Description 項目中提供了TSUtility.java類,可用來友善地實作鍵盤通路。
* @author
* @version
* @date
*
*/
public class TSUtility {
private static Scanner scanner = new Scanner(System.in);
/**
*
* @Description 該方法讀取鍵盤,如果使用者鍵入’1’-’4’中的任意字元,則方法傳回。傳回值為使用者鍵入字元。
* @author shkstart
* @date 2019年2月12日上午12:03:30
* @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;
}
/**
*
* @Description 該方法提示并等待,直到使用者按Enter鍵後傳回。
* @author shkstart
* @date 2019年2月12日上午12:03:50
*/
public static void readReturn() {
System.out.print("按Enter鍵繼續...");
readKeyBoard(100, true);
}
/**
*
* @Description 該方法從鍵盤讀取一個長度不超過2位的整數,并将其作為方法的傳回值。
* @author shkstart
* @date 2019年2月12日上午12:04:04
* @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;
}
/**
*
* @Description 從鍵盤讀取‘Y’或’N’,并将其作為方法的傳回值。
* @author shkstart
* @date 2019年2月12日上午12:04:45
* @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;
}
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類:enterMainMenu方法進入,enterMainMenu方法中通過标簽控制循環,其中有listAllEmployees()方法,用于顯式所有的員工資訊,這裡就牽扯到toString方法的重寫,進而實作輸出。緊接着要進行選擇,通過工具類獲得鍵盤選擇,利用switch進行操作。四種選擇,三種方法,第四個為退出,退出與前面project02類似,先判斷是否要退出,然後将标志置位false。
其餘的三個方法主要是toString方法的重寫。
package project03.team.view;
import project03.team.domain.Employee;
import project03.team.domain.Programmer;
import project03.team.service.NameListService;
import project03.team.service.TeamExeption;
import project03.team.service.TeamService;
public class TeamView {
private NameListService ListSvc = new NameListService();
private TeamService teamSvc = new TeamService();
public void enterMainMenu() {
boolean loopFalg = true;
char menu = 0;
while (loopFalg) {
if (menu != '1') {
listAllEmployees();
}
System.out.print("1團隊清單 2-添加團隊成員 3-删除團隊成員 4-退出 請選擇(1-4):");
menu = TSUtility.readMenuSelection();
switch (menu) {
case '1':
getTeam();
break;
case '2':
addMember();
break;
case '3':
deleteMember();
break;
case '4':
System.out.print("确認是否退出(Y/N)");
char isExit = TSUtility.readConfirmSelection();
if (isExit == 'Y') {
loopFalg = false;
}
break;
}
}
}
/**
*
* @Description顯式所有員工資訊
* @author
* @date 2022年1月5日下午8:08:42
*/
private void listAllEmployees() {
System.out.println("-----------------------開發團隊排程軟體----------------------\n");
Employee[] allEmployees = ListSvc.getAllEmployees();
if (allEmployees == null || allEmployees.length == 0) {
System.out.println("公司中沒有任何員工!");
} else {
System.out.println("ID\t姓名\t年齡\t工資\t職位\t狀态\t獎金\t股票\t領用裝置");
for (int i = 0; i < allEmployees.length; i++) {
System.out.println(allEmployees[i]);
}
}
System.out.println("-----------------------------------------------------------\n");
}
private void getTeam() {
System.out.println("--------------------------團隊成員清單------------------------\n");
Programmer[] team = teamSvc.getTeam();
if (team == null || team.length == 0) {
System.out.println("開發團隊目前沒有員工");
} else {
System.out.println("TID/ID\t姓名\t年齡\t工資\t職位\t獎金\t股票\n");
for (int i = 0; i < team.length; i++) {
System.out.println(team[i].getDetails());
}
}
System.out.println("-------------------------------------------------------------\n");
}
private void addMember() {
System.out.println("--------------------------------添加成員-----------------------");
System.out.print("請輸入要添加的員工ID");
int id = TSUtility.readInt();
try {
Employee emp = ListSvc.getEmployee(id);
teamSvc.addMember(emp);
System.out.println("添加成功");
} catch (TeamExeption e) {
System.out.println("添加失敗:原因" + e.getMessage());
}
// 按回車繼續
TSUtility.readReturn();
}
private void deleteMember() {
System.out.println("--------------------------------删除成員-----------------------");
System.out.print("請輸入要删除員工的TID:");
int memberId = TSUtility.readInt();
System.out.print("确認是否删除(Y/N)");
char isDelete = TSUtility.readConfirmSelection();
if(isDelete=='N') {
return;
}else {
try {
teamSvc.removeMember(memberId);
System.out.println("删除成功!");
} catch (TeamExeption e) {
System.out.println("删除失敗,原因"+e.getMessage());
}
// 按回車繼續
TSUtility.readReturn();
}
}
public static void main(String[] args) {
TeamView view = new TeamView();
view.enterMainMenu();
}
}