SCAU软件体系结构实验四 组合模式
目录
一、题目
二、源码
一、题目
个人(Person)与团队(Team)可以形成一个组织(Organization):组织有两种:个人组织和团队组织,多个个人可以组合成一个团队,不同的个人与团队可以组合成一个更大的团队。
使用控制台或者JavaFx界面完成以下功能:维护团队,并递归显示团队的所以成员。
控制台客户端功能提示:简化起见,采用自顶向下的方式创建团队,例如,创建“t1”团队,然后创建“t1”的子团队“t1.1”团队、“t1.2”团队,创建“t1.1”的子团队“t1.1.1”,子团队“t1.1.1”由“p1”、“p2”和“p3”三个人。创建完成后,递归输出这些对象;例如,当前情况下输出这样的消息:
团队t1有t1.1, t1.2, t1.3三个子对象;团队t1.1有t1.1.1一个子对象;团队t1.1.1有p1, p2, p3三个子对象。
在此基础上修改一个对象,例如,把p3对象从子团队“t1.1.1”中删除,然后添加到t1.1团队中;完成这些操作后,递归输出这些对象,结果应该是
团队t1有t1.1, t1.2, t1.3三个子对象;团队t1.1有p3, t1.1.1两个子对象;团队t1.1.1有p1, p2两个子对象。
JavaFx界面提示:JavaFx的基本界面如下,同学们在此基础上适当修改.
左边是创建对象的树状结构展示,右边是选中某一个节点后的操作。例如,如果选中T12。然后就可以输入T121,输入后直接刷新树状结构。
二、源码
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class OrganizationManager {
static class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Person person = (Person) obj;
return name.equals(person.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public String toString() {
return name;
}
}
static class Team {
private String name;
private List<Team> subTeams = new ArrayList<>();
private List<Person> members = new ArrayList<>();
public Team(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void addSubTeam(Team team) {
subTeams.add(team);
}
public void addMember(Person person) {
members.add(person);
}
public boolean removeMember(Person person) {
return members.remove(person);
}
public void moveMember(Person person, Team destination) {
if (removeMember(person)) {
destination.addMember(person);
} else {
System.out.println("成员 " + person + " 不在团队 " + this.name + " 中。");
}
}
public Team findSubTeam(String name) {
for (Team subTeam : subTeams) {
if (subTeam.getName().equals(name)) {
return subTeam;
}
}
for (Team subTeam : subTeams) {
Team found = subTeam.findSubTeam(name);
if (found != null) {
return found;
}
}
return null;
}
public Person findMember(String name) {
for (Person member : members) {
if (member.getName().equals(name)) {
return member;
}
}
for (Team subTeam : subTeams) {
Person found = subTeam.findMember(name);
if (found != null) {
return found;
}
}
return null;
}
public void display(int level) {
System.out.println(getIndent(level) + "团队" + name + "有" + subTeams.size() + "个子团队;");
for (Team subTeam : subTeams) {
subTeam.display(level + 1);
}
System.out.println(getIndent(level) + "团队" + name + "有" + members.size() + "个成员;");
for (Person member : members) {
System.out.println(getIndent(level + 1) + member);
}
}
private String getIndent(int level) {
return " ".repeat(level);
}
}
public static void main(String[] args) {
Team t1 = new Team("t1");
Team t1_1 = new Team("t1.1");
Team t1_2 = new Team("t1.2");
Team t1_1_1 = new Team("t1.1.1");
Person p1 = new Person("p1");
Person p2 = new Person("p2");
Person p3 = new Person("p3");
t1.addSubTeam(t1_1);
t1.addSubTeam(t1_2);
t1_1.addSubTeam(t1_1_1);
t1_1_1.addMember(p1);
t1_1_1.addMember(p2);
t1_1_1.addMember(p3);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\n请选择操作:");
System.out.println("1. 显示团队结构");
System.out.println("2. 删除成员");
System.out.println("3. 移动成员");
System.out.println("4. 退出");
String choice = scanner.nextLine();
switch (choice) {
case "1":
t1.display(0);
break;
case "2":
System.out.println("请输入要删除的成员名称:");
String memberNameToDelete = scanner.nextLine();
Person memberToDelete = t1.findMember(memberNameToDelete);
if (memberToDelete != null) {
Team teamToDelete = findTeamForMember(t1, memberToDelete);
if (teamToDelete != null) {
teamToDelete.removeMember(memberToDelete);
System.out.println("成员 " + memberNameToDelete + " 已删除。");
} else {
System.out.println("未找到该成员所在的团队。");
}
} else {
System.out.println("未找到该成员。");
}
break;
case "3":
System.out.println("请输入要移动的成员名称:");
String memberNameToMove = scanner.nextLine();
System.out.println("请输入目标团队名称:");
String targetTeamName = scanner.nextLine();
Person memberToMove = t1.findMember(memberNameToMove);
Team targetTeam = t1.findSubTeam(targetTeamName);
if (memberToMove != null && targetTeam != null) {
Team currentTeam = findTeamForMember(t1, memberToMove);
if (currentTeam != null) {
currentTeam.moveMember(memberToMove, targetTeam);
System.out.println("成员 " + memberNameToMove + " 已移动到团队 " + targetTeamName + "。");
} else {
System.out.println("未找到该成员所在的团队。");
}
} else {
System.out.println("未找到指定的成员或目标团队。");
}
break;
case "4":
scanner.close();
return;
default:
System.out.println("无效的操作。");
break;
}
}
}
private static Team findTeamForMember(Team root, Person member) {
for (Person m : root.members) {
if (m.equals(member)) {
return root;
}
}
for (Team subTeam : root.subTeams) {
Team found = findTeamForMember(subTeam, member);
if (found != null) {
return found;
}
}
return null;
}
}