1 class Dept //部门信息
2 {
3 private int did;
4 private String dname;
5 private Emp[] emps;//一个部门有多个雇员
6 private Role role;//一个部分有一个角色
7 public Dept(int did,String dname){
8 this.did=did;
9 this.dname=dname;
10 }
11 public void setEmps(Emp[] emps){
12 this.emps=emps;
13 }
14 public Emp[] getEmps(){
15 return this.emps;
16 }
17 public void setRole(Role role){
18 this.role=role;
19 }
20 public Role getRole(){
21 return this.role;
22 }
23 public String getInfo(){
24 return "【部门】did="+this.did+",dname="+this.dname;
25 }
26 }
27 class Emp //雇员信息
28 {
29 private int eid;
30 private String ename;
31 private Dept dept;
32 public Emp(int eid,String ename){
33 this.eid=eid;
34 this.ename=ename;
35 }
36 public void setDept(Dept dept){
37 this.dept=dept;
38 }
39 public Dept getDept(){
40 return this.dept;
41 }
42 public String getInfo(){
43 return "【雇员】eid="+this.eid+",ename="+this. ename;
44 }
45 }
46 class Role //角色信息
47 {
48 private int rid;
49 private String title;
50 private Dept[] depts;
51 private Action[] actions;
52 public Role(int rid,String title){
53 this.rid=rid;
54 this.title=title;
55 }
56 public void setDepts(Dept[] depts){
57 this.depts=depts;
58 }
59 public Dept[] getDepts(){
60 return this.depts;
61 }
62 public void setActions(Action[] actions){
63 this.actions=actions;
64 }
65 public Action[] getActions(){
66 return this.actions;
67 }
68 public String getInfo(){
69 return "【角色】rid="+this.rid+",title="+this.title;
70 }
71 }
72 class Action //权限信息
73 {
74 private int aid;
75 private String title;
76 private String flag;
77 private Role[] roles;
78 public Action(int aid,String title,String flag){
79 this.aid=aid;
80 this.title=title;
81 this.flag=flag;
82 }
83 public void setRoles(Role[] roles){
84 this.roles=roles;
85 }
86 public Role[] getRoles(){
87 return this.roles;
88 }
89 public String getInfo(){
90 return "【权限】aid="+this.aid+",title="+this.title+",flag="+this.flag;
91 }
92 }
93 public class Newbegin{
94 public static void main(String args[]) {
95 //第一步:设置数据之间的关系
96 //1.创建部门数据
97 Dept d10=new Dept(10,"财务部");
98 Dept d20=new Dept(20,"市场部");
99 //2.创建雇员数据
100 Emp e7369=new Emp(7369,"SMITH");
101 Emp e7566=new Emp(7369,"ALLEN");
102 Emp e7902=new Emp(7369,"FORD");
103 Emp e7839=new Emp(7369,"KIND");
104 Emp e7788=new Emp(7788,"SCOTT");
105 //3.创建角色信息
106 Role r100=new Role(100,"管理者");
107 Role r200=new Role(200,"职员层");
108 //4.创建权限数据
109 Action a1000=new Action(1000,"雇员入职","emp:add");
110 Action a2000=new Action(2000,"雇员晋升","emp:edit");
111 Action a3000=new Action(3000,"发布公告","news:add");
112 Action a6000=new Action(6000,"查看客户信息","customer:list");
113 Action a7000=new Action(7000,"回防记录","customer:list");
114 //5.设置角色与权限的关系
115 r100.setActions(new Action[]{a1000,a2000,a3000});
116 r200.setActions(new Action[]{a6000,a7000});
117 //6.设置权限与角色的关系
118 a1000.setRoles(new Role[]{r100});
119 a2000.setRoles(new Role[]{r100});
120 a3000.setRoles(new Role[]{r100});
121 a6000.setRoles(new Role[]{r200});
122 a7000.setRoles(new Role[]{r200});
123 //7.设置部门和角色的关系
124 d10.setRole(r100);
125 d20.setRole(r200);
126 //8.设置角色和部门的关系
127 r100.setDepts(new Dept[]{d10});
128 r200.setDepts(new Dept[]{d20});
129 //9.设置雇员和部门的关系
130 e7369.setDept(d10);
131 e7566.setDept(d10);
132 e7902.setDept(d20);
133 e7839.setDept(d20);
134 e7788.setDept(d20);
135 //10.设置部门与雇员的关系
136 d10.setEmps(new Emp[]{e7369,e7566});
137 d20.setEmps(new Emp[]{e7902,e7839,e7788});
138 //第二步:取出相应数据
139 //要求可以根据一个员工找到相应的部门,以及对应的角色,以及每个角色的所有权限
140 System.out.println("1.要求可以根据一个员工找到相应的部门,以及对应的角色,以及每个角色的所有权限");
141 System.out.println(e7369.getInfo());
142 System.out.println(" |-"+e7369.getDept().getInfo());
143 System.out.println(" |-"+e7369.getDept().getRole().getInfo());
144 for(int x=0;x<e7369.getDept().getRole().getActions().length;x++){
145 System.out.println(" |-"+e7369.getDept().getRole().getActions()[x].getInfo());
146 }
147 //----------------------
148 System.out.println("2.可以根据一个角色找到具备此角色的所有部门,以及部门的所有员工");
149 System.out.println(r200.getInfo());
150 for(int x=0;x<r200.getDepts().length;x++){
151 System.out.println(" |-"+r200.getDepts()[x].getInfo());
152 for(int y=0;y<r200.getDepts()[x].getEmps().length;y++){
153 System.out.println(" |-"+r200.getDepts()[x].getEmps()[y].getInfo());
154 }
155 }
156 //-----------------------
157 System.out.println("3.根据一个权限列出所有具备该权限的角色以及角色的部门,部门的员工");
158 System.out.println(a2000.getInfo());
159 for(int x=0;x<a2000.getRoles().length;x++){
160 System.out.println(" |-"+a2000.getRoles()[x].getInfo());
161 for(int y=0;y<a2000.getRoles()[x].getDepts().length;y++){
162 System.out.println(" |-"+a2000.getRoles()[x].getDepts()[y].getInfo());
163 for(int z=0;z<a2000.getRoles()[x].getDepts()[y].getEmps().length;z++){
164 System.out.println(" |-"+a2000.getRoles()[x].getDepts()[y].getEmps()[z].getInfo());
165 }
166 }
167 }
168 }
169 }