1. 程式人生 > >數據表與簡單java類(角色與權限)

數據表與簡單java類(角色與權限)

部門 aid alt 宋體 cot gin str class a name

技術分享圖片

class Dept //部門信息
{
private int did;
private String dname;
private Emp[] emps;//一個部門有多個雇員
private Role role;//一個部分有一個角色
public Dept(int did,String dname){
this.did=did;
this.dname=dname;
}
public void setEmps(Emp[] emps){
this.emps=emps;
}
public Emp[] getEmps(){
return this.emps;
}
public void setRole(Role role){


this.role=role;
}
public Role getRole(){
return this.role;
}
public String getInfo(){
return "【部門】did="+this.did+",dname="+this.dname;
}
}
class Emp //雇員信息
{
private int eid;
private String ename;
private Dept dept;
public Emp(int eid,String ename){
this.eid=eid;
this.ename=ename;
}
public void setDept(Dept dept){

this.dept=dept;
}
public Dept getDept(){
return this.dept;
}
public String getInfo(){
return "【雇員】eid="+this.eid+",ename="+this. ename;
}
}
class Role //角色信息
{
private int rid;
private String title;
private Dept[] depts;
private Action[] actions;
public Role(int rid,String title){
this.rid=rid;
this.title=title;

}
public void setDepts(Dept[] depts){
this.depts=depts;
}
public Dept[] getDepts(){
return this.depts;
}
public void setActions(Action[] actions){
this.actions=actions;
}
public Action[] getActions(){
return this.actions;
}
public String getInfo(){
return "【角色】rid="+this.rid+",title="+this.title;
}
}
class Action //權限信息
{
private int aid;
private String title;
private String flag;
private Role[] roles;
public Action(int aid,String title,String flag){
this.aid=aid;
this.title=title;
this.flag=flag;
}
public void setRoles(Role[] roles){
this.roles=roles;
}
public Role[] getRoles(){
return this.roles;
}
public String getInfo(){
return "【權限】aid="+this.aid+",title="+this.title+",flag="+this.flag;
}
}
public class Newbegin{
public static void main(String args[]) {
//第一步:設置數據之間的關系
//1.創建部門數據
Dept d10=new Dept(10,"財務部");
Dept d20=new Dept(20,"市場部");
//2.創建雇員數據
Emp e7369=new Emp(7369,"SMITH");
Emp e7566=new Emp(7369,"ALLEN");
Emp e7902=new Emp(7369,"FORD");
Emp e7839=new Emp(7369,"KIND");
Emp e7788=new Emp(7788,"SCOTT");
//3.創建角色信息
Role r100=new Role(100,"管理者");
Role r200=new Role(200,"職員層");
//4.創建權限數據
Action a1000=new Action(1000,"雇員入職","emp:add");
Action a2000=new Action(2000,"雇員晉升","emp:edit");
Action a3000=new Action(3000,"發布公告","news:add");
Action a6000=new Action(6000,"查看客戶信息","customer:list");
Action a7000=new Action(7000,"回防記錄","customer:list");
//5.設置角色與權限的關系
r100.setActions(new Action[]{a1000,a2000,a3000});
r200.setActions(new Action[]{a6000,a7000});
//6.設置權限與角色的關系
a1000.setRoles(new Role[]{r100});
a2000.setRoles(new Role[]{r100});
a3000.setRoles(new Role[]{r100});
a6000.setRoles(new Role[]{r200});
a7000.setRoles(new Role[]{r200});
//7.設置部門和角色的關系
d10.setRole(r100);
d20.setRole(r200);
//8.設置角色和部門的關系
r100.setDepts(new Dept[]{d10});
r200.setDepts(new Dept[]{d20});
//9.設置雇員和部門的關系
e7369.setDept(d10);
e7566.setDept(d10);
e7902.setDept(d20);
e7839.setDept(d20);
e7788.setDept(d20);
//10.設置部門與雇員的關系
d10.setEmps(new Emp[]{e7369,e7566});
d20.setEmps(new Emp[]{e7902,e7839,e7788});
//第二步:取出相應數據
//要求可以根據一個員工找到相應的部門,以及對應的角色,以及每個角色的所有權限
System.out.println("1.要求可以根據一個員工找到相應的部門,以及對應的角色,以及每個角色的所有權限");
System.out.println(e7369.getInfo());
System.out.println("\t|-"+e7369.getDept().getInfo());
System.out.println("\t\t|-"+e7369.getDept().getRole().getInfo());
for(int x=0;x<e7369.getDept().getRole().getActions().length;x++){
System.out.println("\t\t\t|-"+e7369.getDept().getRole().getActions()[x].getInfo());
}
//----------------------
System.out.println("2.可以根據一個角色找到具備此角色的所有部門,以及部門的所有員工");
System.out.println(r200.getInfo());
for(int x=0;x<r200.getDepts().length;x++){
System.out.println("\t|-"+r200.getDepts()[x].getInfo());
for(int y=0;y<r200.getDepts()[x].getEmps().length;y++){
System.out.println("\t\t|-"+r200.getDepts()[x].getEmps()[y].getInfo());
}
}
//-----------------------
System.out.println("3.根據一個權限列出所有具備該權限的角色以及角色的部門,部門的員工");
System.out.println(a2000.getInfo());
for(int x=0;x<a2000.getRoles().length;x++){
System.out.println("\t|-"+a2000.getRoles()[x].getInfo());
for(int y=0;y<a2000.getRoles()[x].getDepts().length;y++){
System.out.println("\t\t|-"+a2000.getRoles()[x].getDepts()[y].getInfo());
for(int z=0;z<a2000.getRoles()[x].getDepts()[y].getEmps().length;z++){
System.out.println("\t\t\t|-"+a2000.getRoles()[x].getDepts()[y].getEmps()[z].getInfo());
}
}
}
}
}

技術分享圖片

數據表與簡單java類(角色與權限)