首先我们需要建立三个包分别存放我们的实体类,主方法,和其他方法
在domain里面我们需要有类Employee,子类program,program的子类Designer,Designer的子类Arcjitect
Employee类
public class Employee { private String name; private int age; private int id; private double salary; public Employee(){} public Employee(String name, int age, int id, double salary) { this.name = name; this.age = age; this.id = id; this.salary = salary; } 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 int getId() { return id; } public void setId(int id) { this.id = id; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public String getDetails(){ return id+" "+name+" "+age+" "+salary; } @Override public String toString() { return getDetails(); } }
programmer
public class Programmer extends Employee { private int memberId; private boolean status=true; private Equipment equipment; public Programmer(){}; public Programmer(String name, int age, int id, double salary, Equipment equipment) { super(name, age, id, salary); this.equipment = equipment; } public int getMemberId() { return memberId; } public void setMemberId(int memberId) { this.memberId = memberId; } public boolean getStatus() { return status; } public void setStatus(boolean status) { this.status = status; } public Equipment getEquipment() { return equipment; } public void setEquipment(Equipment equipment) { this.equipment = equipment; } public String getMemberDetails(){ return getMemberId()+getDetails(); } public String getDetailsForTeam(){ return getMemberDetails()+"程序员"; } @Override public String toString() { return getDetails()+" "+"程序员"+" "+status+" "+equipment.getDescription(); } }
Designer
public class Designer extends Programmer{ private double bonus; public Designer(){}; public Designer(String name, int age, int id, double salary, Equipment equipment, double bonus) { super(name, age, id, salary, equipment); this.bonus = bonus; } public double getBonus() { return bonus; } public void setBonus(double bonus) { this.bonus = bonus; } @Override public String getDetailsForTeam() { return getMemberDetails()+"设计师"+getBonus(); } @Override public String toString() { return getDetails()+" "+"设计师"+" "+getStatus()+" "+getBonus()+" "+getEquipment().getDescription(); }
Architect
public class Architect extends Designer{ private int stock; public Architect(){}; public int getStock() { return stock; } public void setStock(int stock) { this.stock = stock; } public Architect(String name, int age, int id, double salary, Equipment equipment, double bonus, int stock) { super(name, age, id, salary, equipment, bonus); this.stock = stock; } @Override public String getDetailsForTeam() { return getMemberDetails()+"架构师"+getStock(); } @Override public String toString() { return getDetails()+" "+"架构师"+" "+getStatus()+" "+getStock()+" "+getBonus()+" "+getEquipment().getDescription(); } } 还需要一个接口用来配置员工的设备
public interface Equipment { public String getDescription(); }
设计pc notebook printer类来实现接口
public class PC implements Equipment{ private String model; private String display; public PC(String model, String display) { 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; } }
public class NoteBook implements Equipment{ private String model; private double price; public NoteBook(String model, double price) { 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; } }
public class Printer implements Equipment{ private String name; private String type; public Printer(String name, String type) { 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() { return name+type; } }
最后实体类我们还需要一个项目实体类
public class Project { private int ProId; private String ProName; private String DesName; private String TeamName; private Programmer[]team; private boolean status=false; public Project(){} public Project(int proId, String proName, String desName, String teamName, Programmer[] team, boolean status) { ProId = proId; ProName = proName; DesName = desName; TeamName = teamName; this.team = team; this.status = status; } public int getProId() { return ProId; } public void setProId(int proId) { ProId = proId; } public String getProName() { return ProName; } public void setProName(String proName) { ProName = proName; } public String getDesName() { return DesName; } public void setDesName(String desName) { DesName = desName; } public String getTeamName() { return TeamName; } public void setTeamName(String teamName) { TeamName = teamName; } public Programmer[] getTeam() { return team; } public void setTeam(Programmer[] team) { this.team = team; } public boolean isStatus() { return status; } public void setStatus(boolean status) { this.status = status; } public String toString(){ return ProId+ProName+DesName+TeamName+team+status; } }
然后是登录方法
package com.team.view; import java.util.Random; import java.util.Scanner; public class LoginView { Scanner scanner = new Scanner(System.in); private String name=""; private String password=""; public void register(){ System.out.println("-------------------注册界面-------------------"); System.out.println("账号:"); String s = scanner.next(); name=s; this.name=name; System.out.println("密码:"); String p = scanner.next(); password=p; this.password=password; System.out.println("注册成功请登录"); } public void login(){ int count=5; for (;;){if(this.name.length()==0||this.password.length()==0){ System.out.println("还没有注册先去注册"); register(); } System.out.println("账号:"); String shurunname = scanner.next(); System.out.println("密码:"); String shurupass = scanner.next(); if (this.name.equals(shurunname)&&this.password.equals(shurupass)){ System.out.println("登录成功"); return; } else if (this.name.equals(shurunname) && this.password.equals(shurupass)){ System.out.println("登录成功! " + name); return; }else if(count<=0){ System.out.println("请重新输入,还有"+count+"次机会"); }else{ count--; System.out.println("账号密码错误"); } } } public void update()throws InterruptedException{ for (;;) { System.out.println("1.对账号进行修改"); System.out.println("2.对密码进行修改"); System.out.println("3.对帐号密码进行修改"); System.out.println("4.退出"); int a = scanner.nextInt(); if (a == 1) { System.out.println("请输入修改后的账号"); name = scanner.next(); System.out.println("修改成功"); } else if (a==2) { System.out.println("请输入修改后的密码"); password = scanner.next(); System.out.println("修改成功"); } else if (a==3) { System.out.println("请输入修改后的账号"); name = scanner.next(); System.out.println("修改成功"); System.out.println("请输入修改后的密码"); password = scanner.next(); System.out.println("修改成功"); } else if (a==4) { System.out.println("退出中"); return; } } } }
开发人员管理查看代码
import com.team.domain.*; import java.net.SocketTimeoutException; import java.util.Scanner; import java.util.ArrayList; public class NameListService { private static ArrayList<Employee> employees = new ArrayList<>(); private int count = 1; //初始化默认值 { employees.add(new Employee("马云", 56, count, 3000)); employees.add(new Architect("马化腾", 53, ++count, 7000, new PC("戴尔", "NEC 17寸"), 15000, 6000)); employees.add(new Programmer("李彦宏", 23, ++count, 7000, new PC("戴尔", "NEC 17寸"))); employees.add(new Programmer("刘强东", 24, ++count, 7300, new PC("戴尔", "三星 17寸"))); employees.add(new Designer("雷军 ", 50, ++count, 10000, new Printer("激光", "佳能2900"), 5000)); employees.add(new Programmer("任志强", 30, ++count, 16800, new PC("华硕", "三星 17寸"))); employees.add(new Designer("柳传志", 45, ++count, 35500, new PC("华硕", "三星 17寸"), 8000)); employees.add(new Architect("杨元庆", 35, ++count, 6500, new Printer("针式", "爱普生20k"), 15500, 1200)); employees.add(new Designer("史玉柱", 27, ++count, 7800, new NoteBook("惠普m6", 5800), 1500)); employees.add(new Programmer("丁磊 ", 26, ++count, 6600, new PC("戴尔", "NEC17寸"))); employees.add(new Programmer("张朝阳 ", 35, ++count, 7100, new PC("华硕", "三星 17寸"))); employees.add(new Designer("杨致远", 38, ++count, 9600, new NoteBook("惠普m6", 5800), 3000)); } // 查询所有员工 public void queryId() { for (int i = 0; i < employees.size(); i++) { System.out.println(employees.get(i)); } } public Employee queryId1() throws TeamException { Employee employee = new Employee(); boolean flag = true; Scanner sc = new Scanner(System.in); int id = sc.nextInt(); for (int x = 0; x < employees.size(); x++) { // try { // if (employees.size() < id) { // throw new TeamException(); // } // } catch (TeamException e) { // System.out.println("没有该员工"); // System.out.println("重新输入对象"); // int s = sc.nextInt(); // id = s; // x--; // continue; if (employees.get(x).getId() == id) { System.out.println(employees.get(x)); return employees.get(x); } } throw new TeamException("没有该员工"); } // // //员工的增加 public void addId ()throws InterruptedException { Scanner sc = new Scanner(System.in); System.out.println("请输入需要添加的雇员的职位:"); System.out.println("1(无职位)"); System.out.println("2(程序员)"); System.out.println("3(设计师)"); System.out.println("4(架构师)"); int choice = sc.nextInt(); if (choice == 1) { System.out.println("请输入当前要添加的员工姓名:"); String name = sc.next(); System.out.println("请输入员工年龄:"); int age = sc.nextInt(); System.out.println("请输入员工的工资:"); double salary = sc.nextDouble(); employees.add(new Employee(name, age, ++count, salary)); } else if (choice == 2) { System.out.println("请输入当前要添加的员工姓名:"); String name = sc.next(); System.out.println("请输入员工年龄:"); int age = sc.nextInt(); System.out.println("请输入员工的工资:"); double salary = sc.nextDouble(); System.out.println("请给当前员工员工配置设备:"); String model = sc.next(); String display = sc.next(); Equipment pc = new PC(model, display); employees.add(new Programmer(name, age, ++count, salary, pc)); } else if (choice == 3) { System.out.println("请输入当前要添加的员工姓名:"); String name = sc.next(); System.out.println("请输入员工年龄:"); int age = sc.nextInt(); System.out.println("请输入员工的工资:"); double salary = sc.nextDouble(); System.out.println("请给当前员工员工配置设备:"); String model = sc.next(); String display = sc.next(); Equipment pc = new PC(model, display); System.out.println("奖金数量:"); double bonus = sc.nextDouble(); employees.add(new Designer(name, age, ++count, salary, pc, bonus)); } else if (choice == 4) { System.out.println("请输入当前要添加的员工姓名:"); String name = sc.next(); System.out.println("请输入员工年龄:"); int age = sc.nextInt(); System.out.println("请输入员工的工资:"); double salary = sc.nextDouble(); System.out.println("请给当前员工员工配置设备:"); String model = sc.next(); String display = sc.next(); Equipment pc = new PC(model, display); System.out.println("股票数量:"); int stock = sc.nextInt(); System.out.println("奖金数量:"); double bonus = sc.nextDouble(); employees.add(new Architect(name, age, ++count, salary, pc, bonus, stock)); } } public void deleteByid () { Scanner scanner = new Scanner(System.in); System.out.println("请输入你要删除的员工Id"); int id = scanner.nextInt(); for (int y = 0; y < employees.size(); y++) { try { if (employees.size() < id) { throw new Exception(); } } catch (Exception e) { System.out.println("没有该员工"); System.out.println("重新输入对象"); int s = scanner.nextInt(); id = s; y--; continue; } if (employees.get(y).getId() == id) { employees.remove(employees.get(y)); for (int dete1 = id; dete1 <= employees.size(); dete1++) { employees.get(dete1 - 1).setId(employees.get(dete1 - 1).getId() - 1); } } } } public void showAll ()throws InterruptedException { System.out.println("ID\t 姓名\t年龄\t 工资\t 职位\t 状态\t 奖金\t 股票\t 领用设备"); for (int i = 0; i < employees.size(); i++) { System.out.println(" " + employees.get(i)); } } public void modify ()throws InterruptedException { boolean flag = false; System.out.println("请输入要修改的员工id"); Scanner scanner = new Scanner(System.in); int modifyId = scanner.nextInt(); for (int g = 0; g < employees.size(); g++) { try { if (employees.size() < modifyId) { throw new Exception(); } } catch (Exception e) { System.out.println("没有该员工"); System.out.println("重新输入对象"); int s = scanner.nextInt(); modifyId = s; g--; continue; } if (employees.get(g).getId() == modifyId) { System.out.println("请输入要修改的姓名"); String name = scanner.next(); System.out.println("请输入要修改的年龄"); int age = scanner.nextInt(); System.out.println("请输入要修改的工资"); double salay = scanner.nextDouble(); employees.get(modifyId - 1).setName(name); employees.get(modifyId - 1).setAge(age); employees.get(modifyId - 1).setSalary(salay); flag = true; } } if (flag) { System.out.println("修改成功"); } } }
对团队的添加,删除,查看设计方法
import com.team.domain.*; import java.nio.charset.CoderMalfunctionError; import java.util.Scanner; public class TeamService { private static int counter=1; // 团队人员上限是5 private final int max=5; // 建立程序员数组 private Programmer[]team=new Programmer[max]; // 团队当前人数 private int total=0; public Programmer[] getTeam() { Programmer[] team = new Programmer[total]; for (int i = 0; i < total; i++) { team[i] = this.team[i]; System.out.println(team[i]); } return team; } //初始化当前团队成员数组 public void clearTeam() { team = new Programmer[max]; counter=1; total=0; this.team = team; } public void addMan(Employee employee)throws TeamException{ try { if (total>=max) { throw new TeamException(); } }catch (TeamException e){ System.out.println("成员已满不能添加"); return; } try { if (!(employee instanceof Programmer)) { throw new TeamException(); } }catch (TeamException e){ System.out.println("该成员不是开发人员,不能添加"); return; } Programmer p=(Programmer)employee; try { if (tofind(p)){ throw new TeamException(); } }catch (TeamException e){ System.out.println("该成员已在本团队中"); return; } try { if (!p.getStatus()) { throw new TeamException(); } }catch (TeamException e){ System.out.println("该成员已在其他团队中"); return; } int numArch=0; int numpro=0; int numDsign=0; for(int i=0;i<=total;i++){ if (team[i] instanceof Architect){ numArch++; } if (team[i] instanceof Designer){ numDsign++; } if (team[i] instanceof Programmer) { numpro++; } } if (p instanceof Architect) { try {if (numArch>=1){ throw new TeamException(); } }catch (TeamException e){ System.out.println("一个团队只能有一个架构师"); return; } } else if (p instanceof Designer) { try { if (numDsign>=2) { throw new TeamException(); } }catch (TeamException e){ System.out.println("一个团队只能有两个设计师"); return; } } else if (p instanceof Programmer) { try { if (numpro>=3) { throw new TeamException(); } }catch (TeamException e){ System.out.println("一个团队只能有三个程序员"); return; } } p.setStatus(false); p.setMemberId(counter++); team[total++]=p; } public boolean tofind(Programmer programmer){ for (int i=0;i<total;i++){ if (team[i].getId()==programmer.getId()){return true;} } return false; } public void deletemeber()throws TeamException{ int n=0; Scanner scanner=new Scanner(System.in); System.out.println("请输入要删除员工的团队id"); int memberid=scanner.nextInt(); for(;n<total;n++){ if (team[n].getMemberId()==memberid) { team[n].setStatus(true); break; } } try {for(;n<total;n++){ if (team[n].getMemberId()!=memberid) { throw new TeamException(); } } }catch (TeamException e){ System.out.println("没有该成员"); } for(int i=n+1;i<total;i++){ team[i-1]=team[i]; } team[--total]=null; } } 自定义异常
public class TeamException extends Exception { public TeamException(){} public TeamException(String info){ super(info); } }
项目添加以及开发团队管理
import com.team.domain.Programmer; import com.team.domain.Project; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class ProjectService { Scanner scanner=new Scanner(System.in); private ArrayList<Project> pro=new ArrayList<>(); private int count=1; public void addProject() throws TeamException{ System.out.println("项目参考:--------------------------------------------------"); System.out.println("1.小米官网:开发完成类似于小米官网的web项目."); System.out.println("2.公益在线商城:猫宁Morning公益商城是中国公益性在线电子商城."); System.out.println("3.博客系统:Java博客系统,让每一个有故事的人更好的表达想法!"); System.out.println("4.在线协作文档编辑系统:一个很常用的功能,适合小组内的文档编辑。"); System.out.println("---------------------------"); System.out.println("请输入你想添加的项目名: "); char dd=scanner.nextLine().charAt(0); switch (dd){ case '1': Project p1=new Project(); p1.setProId(count++); p1.setProName("小米官网"); p1.setDesName("开发完成类似于小米官网的web项目."); pro.add(p1); System.out.println("添加完成"); break; case '2': Project p2=new Project(); p2.setProId(count++); p2.setProName("公益在线商城"); p2.setDesName("猫宁Morning公益商城是中国公益性在线电子商城"); pro.add(p2); System.out.println("添加完成"); break; case '3': Project p3=new Project(); p3.setProId(count++); p3.setProName("博客系统"); p3.setDesName("Java博客系统,让每一个有故事的人更好的表达想法!"); System.out.println("添加完成"); break; case '4': Project p4=new Project(); p4.setProId(count++); p4.setProName("在线协作文档编辑系统"); p4.setDesName("J一个很常用的功能,适合小组内的文档编辑"); System.out.println("添加完成"); break; } } public void dealingPro(Programmer[] team){ System.out.println("当前团队有人员:"); for (int i=0;i<team.length;i++){ System.out.println(team[i]); } System.out.println("请为当前团队创建一个团队名称:"); String str=scanner.next(); System.out.println("请选择项目:"); int ranNum = scanner.nextInt(); Project project = this.pro.get(ranNum); project.setTeam(team); project.setStatus(true); project.setTeamName(str); } public void showPro() throws TeamException { for (int i = 0; i < pro.size(); i++) { System.out.println(pro.get(i)); } } // 删除代码 public void delPro(){ System.out.println("选择要删除的项目"); int id=scanner.nextInt(); boolean flag = false; for (int i = 0; i < pro.size(); i++) { if (pro.get(i).getProId() == id) { pro.remove(i); for (i = id; i <= pro.size(); i++) { pro.get(i - 1).setProId(pro.get(i - 1).getProId() - 1); } flag = true; } } if (flag) { System.out.println("删除成功!"); count--; } else { try { throw new TeamException("该项目不存在"); } catch (TeamException e) { e.printStackTrace(); } } } } 最后是main方法
public class IndexView { public static void main(String[] args) throws TeamException { IndexView indexView = new IndexView(); indexView.mact(); } public void mact() throws TeamException { IndexView indexView = new IndexView(); NameListService nameListService = new NameListService(); TeamService teamService = new TeamService(); ProjectService projectService = new ProjectService(); LoginView loginView=new LoginView(); System.out.print("注册-1"); System.out.print("----------------------"); System.out.print("登录-2"); System.out.println(); Scanner sc = new Scanner(System.in); int c = sc.nextInt(); LoginView demo1 = new LoginView(); switch (c) { case 1: demo1.register(); case 2: demo1.login(); } indexView.mact1(); } // switch (c){ // case 1: // loginView.rigster(); // case 2:loginView.login(); // } loginView.update(); public void mact1()throws TeamException{ IndexView indexView = new IndexView(); Scanner sc = new Scanner(System.in); NameListService nameListService = new NameListService(); TeamService teamService = new TeamService(); ProjectService projectService = new ProjectService(); Programmer[] manyTeam=null; System.out.println("1. <用户信息修改> "); System.out.println("2. <开发人员管理> "); System.out.println("3. <开发团队调度管理> "); System.out.println("4. <开发项目管理> "); System.out.println("5. <退出软件> "); System.out.println("请选择: "); int xuanze = sc.nextInt(); switch (xuanze) { case 1: try { LoginView loginView = new LoginView(); loginView.update(); } catch (InterruptedException e) { e.printStackTrace(); } break; case 2: // try { // NameListService nameListService =new NameListService(); // nameListService.showAll(); // } catch (InterruptedException e) { // e.printStackTrace(); // } boolean flage = true; do { System.out.println(" ~开发人员管理主菜单~ "); System.out.println("1. <开发人员的添加> "); System.out.println("2. <开发人员的查看> "); System.out.println("3. <开发人员的修改> "); System.out.println("4. <开发人员的删除> "); System.out.println("5. <退出当前菜单> "); System.out.println("?请选择: "); int abc = sc.nextInt(); switch (abc) { case 1: try { nameListService.addId(); } catch (InterruptedException e) { e.printStackTrace(); } break; case 2: try { nameListService.showAll(); } catch (InterruptedException e) { e.printStackTrace(); } break; case 3: try { nameListService.modify(); } catch (InterruptedException e) { e.printStackTrace(); } break; case 4: nameListService.deleteByid(); break; case 5: System.out.print("确认是否退出(确认1/取消0):"); int a = sc.nextInt(); if (a == 1) { flage = false; } indexView.mact1(); break; default: System.out.println("输入有误!请重新输入!"); break; } } while (flage); break; case 3: boolean flagb = true; do { System.out.println(" ~团队人员管理菜单~ "); System.out.println("1. <团队人员的添加> "); System.out.println("2. <团队人员的查看> "); System.out.println("3. <团队人员的删除> "); System.out.println("4. <退出当前菜单> "); System.out.println("请选择: "); int abcd = sc.nextInt(); // switch (abcd) { case 1: try { System.out.println("请输入要添加的员工id"); teamService.addMan(nameListService.queryId1()); } catch (TeamException e) { e.printStackTrace(); } break; case 2: teamService.getTeam(); break; case 3: teamService.deletemeber(); break; case 4: System.out.print("确认是否退出(1/2):"); int yn = sc.nextInt(); if (yn == 1) { flagb = false; } indexView.mact1(); break; default: System.out.println("输入有误!请重新输入!"); break; } } while (flagb); break; case 4: boolean flagc = true; do { System.out.println(" ~开发项目管理主菜单~ "); System.out.println("1. <项目的添加> "); System.out.println("2. <项目分配开发团队> "); System.out.println("3. <项目的查看> "); System.out.println("4. <项目的删除> "); System.out.println("5. <退出当前菜单> "); System.out.println("请选择: "); int kaifa = sc.nextInt(); switch (kaifa) { case 1: projectService.addProject(); break; case 2: projectService.dealingPro(teamService.getTeam()); break; case 3: projectService.showPro(); break; case 4: projectService.delPro(); break; case 5: System.out.print("确认是否退出(1/2):"); int yn = sc.nextInt(); if (yn == 1) { flagc = false; } indexView.mact1(); break; default: System.out.println("输入有误!请重新输入!"); break; } } while (flagc); } } } 该项目仍有不足,仍需修改,该项目可以帮助熟练前面学习过的知识