请根据以上需求,完成本实验: 1、建立以上两张表(数据库类型不限,可以采用 Mysql 等数据库,也可以直接读取文件) 2、填写测试数据到表中 3、使用组合模式构建“机构-用户”树 4、构建迭代器: 获取某个机构下全部用户 获取某个用户对应所属的机构权
时间: 2024-11-27 11:08:30 浏览: 4
带你走进MySQL数据库(MySQL入门详细总结二)
### 实验步骤及代码示例
#### 1. 建立数据表
假设使用 MySQL 数据库,以下是创建 `t_Organization` 和 `t_User` 表的 SQL 语句:
```sql
CREATE TABLE t_Organization (
fID INT AUTO_INCREMENT PRIMARY KEY,
fName VARCHAR(50) NOT NULL,
fHigherUpfIDs VARCHAR(255) NOT NULL,
fPermission VARCHAR(255) NOT NULL,
fRemark TEXT,
fOrgGUID VARCHAR(255)
);
CREATE TABLE t_User (
fID INT AUTO_INCREMENT PRIMARY KEY,
fOrgIDs VARCHAR(255) NOT NULL,
fUserGUID VARCHAR(255),
fName VARCHAR(50) NOT NULL,
fPassword VARCHAR(255) NOT NULL,
fRemark TEXT
);
```
#### 2. 填写测试数据
以下是一些示例数据,可以直接插入到表中:
```sql
INSERT INTO t_Organization (fName, fHigherUpfIDs, fPermission, fRemark, fOrgGUID) VALUES
('Root', '0', '|3316|5581|6118|', 'Root Organization', 'ORG_001'),
('Department A', '|01|', '|3316|', 'Department A', 'ORG_002'),
('Department B', '|01|', '|5581|', 'Department B', 'ORG_003');
INSERT INTO t_User (fOrgIDs, fUserGUID, fName, fPassword, fRemark) VALUES
('|01|', 'USER_001', 'Alice', '5f4dcc3b5aa765d61d8327deb882cf99', 'User in Root'),
('|0102|', 'USER_002', 'Bob', '5f4dcc3b5aa765d61d8327deb882cf99', 'User in Department A'),
('|0103|', 'USER_003', 'Charlie', '5f4dcc3b5aa765d61d8327deb882cf99', 'User in Department B');
```
#### 3. 使用组合模式构建“机构-用户”树
首先,定义 `Component` 接口:
```java
public interface Component {
void add(Component component);
void remove(Component component);
Component getChild(int index);
String getName();
List<String> getPermissions();
void printStructure(String prefix);
}
```
然后,定义 `Composite` 类(代表机构):
```java
import java.util.ArrayList;
import java.util.List;
public class Composite implements Component {
private String name;
private List<Component> children = new ArrayList<>();
private List<String> permissions;
public Composite(String name, List<String> permissions) {
this.name = name;
this.permissions = permissions;
}
@Override
public void add(Component component) {
children.add(component);
}
@Override
public void remove(Component component) {
children.remove(component);
}
@Override
public Component getChild(int index) {
return children.get(index);
}
@Override
public String getName() {
return name;
}
@Override
public List<String> getPermissions() {
return permissions;
}
@Override
public void printStructure(String prefix) {
System.out.println(prefix + "Organization: " + name);
for (Component child : children) {
child.printStructure(prefix + "--");
}
}
}
```
接着,定义 `Leaf` 类(代表用户):
```java
public class Leaf implements Component {
private String name;
private List<String> permissions;
public Leaf(String name, List<String> permissions) {
this.name = name;
this.permissions = permissions;
}
@Override
public void add(Component component) {
throw new UnsupportedOperationException("Cannot add to a leaf node.");
}
@Override
public void remove(Component component) {
throw new UnsupportedOperationException("Cannot remove from a leaf node.");
}
@Override
public Component getChild(int index) {
throw new UnsupportedOperationException("Leaf node has no children.");
}
@Override
public String getName() {
return name;
}
@Override
public List<String> getPermissions() {
return permissions;
}
@Override
public void printStructure(String prefix) {
System.out.println(prefix + "User: " + name);
}
}
```
最后,构建树结构:
```java
public class Main {
public static void main(String[] args) {
// 创建根节点
Composite root = new Composite("Root", Arrays.asList("3316", "5581", "6118"));
// 创建子节点
Composite deptA = new Composite("Department A", Arrays.asList("3316"));
Composite deptB = new Composite("Department B", Arrays.asList("5581"));
// 添加用户
Leaf user1 = new Leaf("Alice", Arrays.asList("3316"));
Leaf user2 = new Leaf("Bob", Arrays.asList("3316"));
Leaf user3 = new Leaf("Charlie", Arrays.asList("5581"));
// 构建树结构
root.add(deptA);
root.add(deptB);
deptA.add(user1);
deptA.add(user2);
deptB.add(user3);
// 打印树结构
root.printStructure("");
}
}
```
#### 4. 构建迭代器
定义 `Iterator` 接口:
```java
public interface Iterator<T> {
boolean hasNext();
T next();
}
```
定义 `CompositeIterator` 类:
```java
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
public class CompositeIterator implements Iterator<Component> {
private Queue<Iterator<Component>> queue = new LinkedList<>();
public CompositeIterator(Iterator<Component> iterator) {
queue.offer(iterator);
}
@Override
public boolean hasNext() {
if (!queue.isEmpty()) {
Iterator<Component> current = queue.peek();
if (current.hasNext()) {
return true;
} else {
queue.poll();
return hasNext();
}
}
return false;
}
@Override
public Component next() {
if (hasNext()) {
Iterator<Component> current = queue.peek();
Component component = current.next();
if (component instanceof Composite) {
queue.offer(((Composite) component).iterator());
}
return component;
}
return null;
}
}
```
实现 `Component` 接口的 `iterator` 方法:
```java
public interface Component {
void add(Component component);
void remove(Component component);
Component getChild(int index);
String getName();
List<String> getPermissions();
void printStructure(String prefix);
Iterator<Component> iterator();
}
public class Composite implements Component {
// ... 其他方法 ...
@Override
public Iterator<Component> iterator() {
return new CompositeIterator(children.iterator());
}
}
public class Leaf implements Component {
// ... 其他方法 ...
@Override
public Iterator<Component> iterator() {
return new SingletonIterator<>(this);
}
}
class SingletonIterator<T> implements Iterator<T> {
private final T singleElement;
private boolean isReturned = false;
public SingletonIterator(T element) {
this.singleElement = element;
}
@Override
public boolean hasNext() {
return !isReturned;
}
@Override
public T next() {
if (hasNext()) {
isReturned = true;
return singleElement;
}
return null;
}
}
```
使用迭代器获取某个机构下的所有用户:
```java
public class Main {
public static void main(String[] args) {
// ... 构建树结构 ...
// 获取根节点的所有用户
System.out.println("Users under Root:");
for (Component component : new CompositeIterator<>(root.iterator())) {
if (component instanceof Leaf) {
System.out.println(((Leaf) component).getName());
}
}
// 获取特定机构的所有用户
System.out.println("Users under Department A:");
for (Component component : new CompositeIterator<>(deptA.iterator())) {
if (component instanceof Leaf) {
System.out.println(((Leaf) component).getName());
}
}
}
}
```
获取某个用户对应的机构权限:
```java
public class Main {
public static void main(String[] args) {
// ... 构建树结构 ...
// 查找特定用户的权限
String targetUserName = "Alice";
for (Component component : new CompositeIterator<>(root.iterator())) {
if (component instanceof Leaf && ((Leaf) component).getName().equals(targetUserName)) {
System.out.println("Permissions for " + targetUserName + ": " + ((Leaf) component).getPermissions());
break;
}
}
}
}
```
### 提交内容
1. 把全套代码压缩为 `.rar` 格式提交。
2. 填写实验报告并提交。
希望以上内容对你完成实验有所帮助!如果有任何问题,请随时联系我。
阅读全文