开发人员调度软件
程序员文章站
2022-03-20 12:42:10
开发人员调度软件 这几天在学校弄毕设,异常那边找了个简单数组l类项目做了一下 只为记录,好记性不如烂笔头 有误请指正 ありがとうございます。 我的公众号 作者:晨钟暮鼓c个人微信公众号:程序猿的月光宝盒 1.首先,项目名字是开发人员调动软件,基于控制台,需求如下 2.涉及知识点 1. 类的继承性和多 ......
开发人员调度软件
这几天在学校弄毕设,异常那边找了个简单数组l类项目做了一下
只为记录,好记性不如烂笔头
有误请指正
ありがとうございます。
我的公众号
作者:晨钟暮鼓c
个人微信公众号:程序猿的月光宝盒
个人微信公众号:程序猿的月光宝盒
1.首先,项目名字是开发人员调动软件,基于控制台,需求如下
2.涉及知识点
- 类的继承性和多态性
- 对象的值传递、接口
- static和final修饰符
- 特殊类的使用:包装类、抽象类、内部类
- 异常处理
3.源码实现
类
employee.java
package pers.jsc.dispatch.domain; /** * @author 金聖聰 * @title: employee * @projectname teamdispatchapp * @description: todo * @date 2019/5/8 23:48 */ public class employee { private int id; private string name; private int age; private double salary; 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 employee(int id, string name, int age, double salary) { this.id = id; this.name = name; this.age = age; this.salary = salary; } protected string getdetails(){ return id +"\t"+ name +"\t"+ age +"\t\t"+ salary ; } @override public string tostring() { return getdetails(); } }
programmer.java
package pers.jsc.dispatch.domain.domainexte; import pers.jsc.dispatch.domain.employee; import pers.jsc.dispatch.domain.equipment; import pers.jsc.dispatch.service.status; /** * @author 金聖聰 * @title: programmer * @projectname teamdispatchapp * @description: todo * @date 2019/5/8 23:50 */ public class programmer extends employee { /** * 用来记录成员加入开发团队后在团队中的id */ private int memberid; /** * 成员状态 */ private status status = status.free; private 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; } public programmer(int id, string name, int age, double salary, equipment equipment) { super(id, name, age, salary); this.equipment = equipment; } protected string getmembernumdetails() { return memberid + "/" + getdetails(); } public string getdetails4team() { return getmembernumdetails() + "\t程序员\t"; } @override public string tostring() { return getdetails() + "\t程序员\t" + status + "\t\t\t\t\t" + equipment.getdescription(); } }
designer.java
package pers.jsc.dispatch.domain.domainexte; import pers.jsc.dispatch.domain.equipment; /** * @author 金聖聰 * @title: designer * @projectname teamdispatchapp * @description: todo * @date 2019/5/8 23:55 */ public class designer extends programmer { /** * 奖金 */ private double bonus; public double getbonus() { return bonus; } public void setbonus(double bonus) { this.bonus = bonus; } public designer(int id, string name, int age, double salary, equipment equipment, double bonus) { super(id, name, age, salary, equipment); this.bonus = bonus; } @override public string getdetails4team() { return getmembernumdetails() + "\t设计师\t" + bonus; } @override public string tostring() { return getdetails() + "\t设计师\t" + getstatus() + "\t" + bonus + "\t\t\t" + getequipment().getdescription(); } }
architect.java
package pers.jsc.dispatch.domain.domainexte; import pers.jsc.dispatch.domain.equipment; /** * @author 金聖聰 * @title: architect * @projectname teamdispatchapp * @description: todo * @date 2019/5/8 23:58 */ public class architect extends designer { /** * 股票数量 */ private int stock; public int getstock() { return stock; } public void setstock(int stock) { this.stock = stock; } 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; } @override public string getdetails4team() { return getmembernumdetails() + "\t架构师\t" + getbonus() + "\t" + stock; } @override public string tostring() { return getdetails() + "\t架构师\t" + getstatus() + "\t" + getbonus() + "\t" + stock + "\t" + getequipment().getdescription(); } }
接口:
equipment.java
package pers.jsc.dispatch.domain; /** * @author 金聖聰 * @title: equipment * @projectname teamdispatchapp * @description: 设备 * @date 2019/5/8 23:54 */ public interface equipment { string getdescription (); }
pc.java
package pers.jsc.dispatch.domain.domainimpl; import pers.jsc.dispatch.domain.equipment; /** * @author 金聖聰 * @title: pc * @projectname teamdispatchapp * @description: todo * @date 2019/5/9 0:01 */ public class pc implements equipment { /** * 表示机器的型号 */ private string model; /** * 表示显示器名称 */ private string 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; } public pc(string model, string display) { this.model = model; this.display = display; } @override public string getdescription() { return model+"("+display+")"; } }
printer.java
package pers.jsc.dispatch.domain.domainimpl; import pers.jsc.dispatch.domain.equipment; /** * @author 金聖聰 * @title: printer * @projectname teamdispatchapp * @description: todo * @date 2019/5/9 0:04 */ public class printer implements equipment { /** * 机器的名字 */ private string name; /** * 表示机器的类型 */ private string 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; } public printer(string name, string type) { this.name = name; this.type = type; } @override public string getdescription() { return name+"("+type+")"; } }
notebook.java
package pers.jsc.dispatch.domain.domainimpl; import pers.jsc.dispatch.domain.equipment; /** * @author 金聖聰 * @title: notebook * @projectname teamdispatchapp * @description: todo * @date 2019/5/9 0:02 */ public class notebook implements equipment { /** * 表示机器的型号 */ private string model; /** * 价格 */ private double 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; } public notebook(string model, double price) { this.model = model; this.price = price; } @override public string getdescription() { return model+"("+price+")"; } }
异常类
teamexception.java
package pers.jsc.dispatch.exception; /** * @author 金聖聰 * @title: teamexception * @projectname teamdispatchapp * @description: todo * @date 2019/5/9 0:19 */ public class teamexception extends runtimeexception{ public teamexception() { } public teamexception(string message) { super(message); } }
service
data.java
package pers.jsc.dispatch.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 */ 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
package pers.jsc.dispatch.service; import pers.jsc.dispatch.domain.employee; import pers.jsc.dispatch.domain.equipment; import pers.jsc.dispatch.domain.domainexte.architect; import pers.jsc.dispatch.domain.domainexte.designer; import pers.jsc.dispatch.domain.domainexte.programmer; import pers.jsc.dispatch.domain.domainimpl.notebook; import pers.jsc.dispatch.domain.domainimpl.pc; import pers.jsc.dispatch.domain.domainimpl.printer; import pers.jsc.dispatch.exception.teamexception; /** * @author 金聖聰 * @title: namelistservice * @projectname teamdispatchapp * @description: 负责将data中的数据封装到employee[]数组中,同时提供相关操作employee[]的方法。 * @date 2019/5/9 0:17 */ public class namelistservice { private employee[] employees; public employee[] getemployees() { return employees; } public void setemployees(employee[] employees) { this.employees = employees; } public namelistservice() { // 根据项目提供的data类构建相应大小的employees数组 employees = new employee[data.employees.length]; for (int i = 0; i < employees.length; i++) { //获取通用数据 int type = integer.parseint(data.employees[i][0]); 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; // 再根据data类中的数据,构建不同的对象,包括employee、programmer、designer和architect对象,以及相关联的equipment子类的对象 // 将对象存于数组中 switch (type) { case data.employee: employees[i] = new employee(id, name, age, salary); break; case data.programmer: equipment = getcreatequipment(i); employees[i] = new programmer(id, name, age, salary, equipment); break; case data.designer: equipment = getcreatequipment(i); bonus = double.parsedouble(data.employees[i][5]); employees[i] = new designer(id, name, age, salary, equipment, bonus); break; case data.architect: equipment = getcreatequipment(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; default: system.out.println("无职位"); } } } private equipment getcreatequipment(int index) { int type = integer.parseint(data.equipments[index][0]); switch (type) { case data.pc: return new pc(data.equipments[index][1], data.equipments[index][2]); case data.notebook: return new notebook(data.equipments[index][1], double.parsedouble(data.equipments[index][2])); case data.printer: return new printer(data.equipments[index][1], data.equipments[index][2]); default: system.out.println("没有此设备"); } return null; } /** * 返回所有员工 * @return 所有员工 */ public employee[] getallemployees() { return employees; } /** * * @param id 员工的id * @return 对应员工 * @throws teamexception 找不到指定员工 */ public employee getemployee(int id) { for (employee employee : employees ) { if (employee.getid() == id){ return employee; } } throw new teamexception("can not found id="+id+"'s employee!"); } }
status.java
package pers.jsc.dispatch.service; /** * @author 金聖聰 * @title: status * @projectname teamdispatchapp * @description: 成员状态 * @date 2019/5/8 23:53 */ 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; } }
teamservice.java
package pers.jsc.dispatch.service; import pers.jsc.dispatch.domain.employee; import pers.jsc.dispatch.domain.domainexte.architect; import pers.jsc.dispatch.domain.domainexte.designer; import pers.jsc.dispatch.domain.domainexte.programmer; import pers.jsc.dispatch.exception.teamexception; /** * @author 金聖聰 * @title: teamservice * @projectname teamdispatchapp * @description: 关于开发团队成员的管理:添加、删除等 * @date 2019/5/9 16:34 */ public class teamservice { /** * 静态变量,用来为开发团队新增成员自动生成团队中的唯一id,即memberid。(提示:应使用增1的方式) */ private static int counter = 1; /** * 表示开发团队最大成员数 */ private final int max_member = 5; /** * 用来保存当前团队中的各成员对象 */ private programmer[] team = new programmer[max_member]; /** * 记录团队成员的实际人数 */ private int total = 0; public static int getcounter() { return counter; } public static void setcounter(int counter) { teamservice.counter = counter; } public int getmax_member() { return max_member; } public void setteam(programmer[] team) { this.team = team; } public int gettotal() { return total; } public void settotal(int total) { this.total = total; } /** * 返回当前团队的所有对象 * @return 包含所有成员对象的数组,数组大小与成员人数一致 */ public programmer[] getteam() { if (total != 0){ programmer[] t = new programmer[total]; for (int i = 0; i < t.length; i++) { t[i] = team[i]; } return t; } throw new teamexception("nobody in team"); } /** * 向团队中添加成员 * @param e 待添加成员的对象 * @throws teamexception 添加失败, teamexception中包含了失败原因 */ public void addmember(employee e) throws teamexception { // 成员已满,无法添加 if (total >= max_member){ throw new teamexception("成员已满,无法添加"); } // 该成员不是开发人员,无法添加 if (!(e instanceof programmer)){ throw new teamexception("该成员不是开发人员,无法添加"); } programmer p = (programmer) e; // 该员工已在本开发团队中 if (isexit(p)){ throw new teamexception("该员工已在本开发团队中"); } // 该员工已是某团队成员 // 该员正在休假,无法添加 string busy = "busy"; string vocation = "vocation"; if (busy.equals(p.getstatus().getname())){ throw new teamexception("该员工已是某团队成员"); }else if (vocation.equals(p.getstatus().getname())){ throw new teamexception("该员正在休假,无法添加"); } int numofarch = 0; int numofdesr = 0; int numofpror = 0; for (int i = 0; i < total; i++) { if (team[i] instanceof architect){ numofarch++; }else if (team[i] instanceof designer){ numofdesr++; }else if (team[i] instanceof programmer){ numofpror++; } } // 团队中至多只能有一名架构师 // 团队中至多只能有两名设计师 // 团队中至多只能有三名程序员 if (p instanceof architect){ if (numofarch >= 1){ throw new teamexception("团队中至多只能有一名架构师"); } }else if (p instanceof designer){ if (numofdesr >= 2){ throw new teamexception("团队中至多只能有两名设计师"); } }else if (p instanceof programmer){ if (numofpror >= 3){ throw new teamexception("团队中至多只能有三名程序员"); } } p.setstatus(status.busy); p.setmemberid(counter++); team[total++] = p; } /** * 从团队中删除成员 * @param memberid 待删除成员的memberid * @throws teamexception 找不到指定memberid的员工,删除失败 */ 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; } } if ( i == total){ throw new teamexception("找不到该成员,删除失败!"); } for (int j = i+1; j < total; j++) { //往前覆盖 team[j-1] = team[j]; } team[--total] = null; } private boolean isexit(programmer p){ for (int i = 0; i < total; i++) { if (team[i].getid() == p.getid()){ return true; } } return false; } }
utils
tsutility.java
package pers.jsc.dispatch.utils; import java.util.*; /** * @author shkstart email:shkstart@126.com * @description 项目中提供了tsutility.java类,可用来方便地实现键盘访问。 * @date 2019年2月12日上午12:02:58 */ public class tsutility { private static scanner scanner = new scanner(system.in); /** * @return 返回值为用户键入1’-’4’中的字符。 * @description 该方法读取键盘,如果用户键入’1’-’4’中的任意字符,则方法返回。 * @author shkstart * @date 2019年2月12日上午12:03:30 */ 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; } /** * 该方法提示并等待,直到用户按回车键后返回。 */ public static void readreturn() { system.out.print("按回车键继续..."); readkeyboard(100, true); } /** * @return 返回键盘读取不超过2位的整数 * @description 该方法读一个长度不超过2位的整数。 * @author shkstart * @date 2019年2月12日上午12:04:04 */ 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 返回键盘读取‘y’或’n’ * @description 从键盘读取‘y’或’n’,并将其作为方法的返回值。 * @author shkstart * @date 2019年2月12日上午12:04:45 */ 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; } }
view
teamview.java
package pers.jsc.dispatch.view; import pers.jsc.dispatch.domain.employee; import pers.jsc.dispatch.domain.domainexte.programmer; import pers.jsc.dispatch.exception.teamexception; import pers.jsc.dispatch.service.namelistservice; import pers.jsc.dispatch.service.teamservice; import pers.jsc.dispatch.utils.tsutility; /** * @author 金聖聰 * @title: teamview * @projectname teamdispatchapp * @description: todo * @date 2019/5/9 18:21 */ public class teamview { /** * 供类中的方法使用 */ private namelistservice listsvc = new namelistservice(); private teamservice teamsvc = new teamservice(); /** * 主界面显示及控制方法 */ public void entermainmenu() { boolean flag = true; char key = '0'; do { char loop = '1'; if (key != loop) { listallemployees(); } system.out.print("1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 请选择(1-4):"); key = tsutility.readmenuselection(); switch (key) { case '1': getteam(); break; case '2': addmember(); break; case '3': deletemember(); break; case '4': system.out.print("确认是否退出(y/n):"); char notexit = 'n'; flag = tsutility.readconfirmselection() == notexit; break; default: system.out.println("输入错误"); } } while (flag); } /** * 以表格形式列出公司所有成员 */ private void listallemployees() { system.out.println("-------------------------------开发团队调度软件--------------------------------\n"); system.out.println("id\t姓名\t\t年龄\t\t工资\t\t职位\t\t状态\t\t奖金\t\t股票\t\t领用设备"); namelistservice namelistservice = new namelistservice(); printnamelist(namelistservice.getallemployees()); system.out.println("-----------------------------------------------------------------------------"); } private void printnamelist(employee[] employees) { for (employee e : employees ) { system.out.println(e); } } /** * 显示团队成员列表操作 */ private void getteam() { system.out.println("\n--------------------团队成员列表---------------------"); system.out.println("tid/id\t姓名\t\t年龄\t\t工资\t\t职位\t\t奖金\t\t股票"); try { programmer[] team = teamsvc.getteam(); for (programmer p : team) { system.out.println(" " + p.getdetails4team()); } } catch (teamexception t) { system.out.println(t.getmessage()); } finally { system.out.println("-----------------------------------------------------"); } } /** * 实现添加成员操作 */ private void addmember() { system.out.println("\n---------------------添加成员---------------------"); system.out.println("请输入要添加的员工id:"); int id = tsutility.readint(); try { teamsvc.addmember(listsvc.getemployee(id)); system.out.println("添加成功"); } catch (teamexception t) { system.out.println("添加失败,原因:" + t.getmessage()); } finally { tsutility.readreturn(); } } /** * 实现删除成员操作 */ private void deletemember() { system.out.println("---------------------删除成员---------------------"); system.out.println("请输入要删除员工的tid:"); int tid = tsutility.readint(); system.out.println("确认是否删除(y/n):"); char delete = tsutility.readconfirmselection(); switch (delete) { case 'y': try { teamsvc.removemember(tid); system.out.println("删除成功!"); break; } catch (teamexception t) { system.out.println(t.getmessage()); } finally { tsutility.readreturn(); } case 'n': break; default: tsutility.readreturn(); } } public static void main(string[] args) { new teamview().entermainmenu(); } }
test类
namelistservicetest.java
package pers.jsc.dispatch.service; import pers.jsc.dispatch.domain.employee; import org.junit.test; /** * @author 金聖聰 * @title: namelistservicetest * @projectname teamdispatchapp * @description: todo * @date 2019/5/9 3:03 */ public class namelistservicetest { @test public void testnamelistservice() { namelistservice service = new namelistservice(); for (employee employee : service.getemployees() ) { system.out.println(employee); } } @test public void testgetallemployees() { namelistservice service = new namelistservice(); for (employee employee : service.getallemployees() ) { system.out.println(employee); } } @test public void testgetemployee() { namelistservice service = new namelistservice(); try { system.out.println(service.getemployee(12)); }catch (exception e){ e.printstacktrace(); system.out.println(e.getmessage()); } } }
teamservicetest.java
package pers.jsc.dispatch.service; import org.junit.test; import pers.jsc.dispatch.domain.employee; import pers.jsc.dispatch.domain.domainexte.programmer; /** * @author 金聖聰 * @title: teamservicetest * @projectname teamdispatchapp * @description: todo * @date 2019/5/9 16:45 */ public class teamservicetest { @test public void getteam() { teamservice teamservice = new teamservice(); teamservice.settotal(5); programmer[] programmers = teamservice.getteam(); for (programmer p: programmers ) { system.out.println(p); } } @test public void addmember() { teamservice teamservice = new teamservice(); teamservice.addmember(new namelistservice().getemployee(12)); programmer[] programmers = teamservice.getteam(); for (programmer p: programmers ) { system.out.println(p); } } @test public void removemember() { teamservice teamservice = new teamservice(); teamservice.addmember(new namelistservice().getemployee(12)); teamservice.addmember(new namelistservice().getemployee(11)); teamservice.removemember(2); programmer[] programmers = teamservice.getteam(); for (programmer p: programmers ) { system.out.println(p); system.out.println(p.getmemberid()); } } }
项目结构:
下一篇: 5G进阶之路