ccf认证权限查询

看到别人的代码,才知道自己的实在难看,就是循环套循环,所以一个好的数据结构很重要,面向对象也很重要,学会用类封装很重要,仔细看题目很重要。

之前自己写的有毛病,虽然结果出来了,但是没有满足一个要求就是,对于分等级权限不带等级的查询返回该用户的最高权限,所以必须遍历所有的角色的所有权限,必然涉及一个比较,然后获得最高等级的权限返回。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
import java.util.Vector;

import javax.management.Query;


public class Mai {
	static List categories=new ArrayList<>();
	static List roles=new ArrayList<>();
	static List users=new ArrayList<>();
	static int TRUE=-2;
	static int FALSE=-3;
	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		int p=Integer.parseInt(scanner.nextLine());
		for (int i = 0; i < p; i++) {
			String s=scanner.nextLine();
			String cat=getcategory(s);
			int lever=getlever(s);
			category category=new category(cat, lever);
			categories.add(category);
		}
		int r=Integer.parseInt(scanner.nextLine());
		for (int i = 0; i < r; i++) {
			String s=scanner.nextLine();
			String[] ss=s.split(" ");
			String rolename=ss[0];
			int num=Integer.parseInt(ss[1]);
			List cates=new ArrayList<>();
			for (int j = 2; j < ss.length; j++) {	
				String cat=getcategory(ss[j]);
				int lever=getlever(ss[j]);
				category category=new category(cat, lever);
				cates.add(category);
			}
			role role=new role(rolename, num, cates);
			roles.add(role);
		}
		int u=Integer.parseInt(scanner.nextLine());
		for (int i = 0; i < u; i++) {
			String s=scanner.nextLine();
			String[] ss=s.split(" ");
			String username=ss[0];
			int num=Integer.parseInt(ss[1]);
			List rolelist=new ArrayList<>();
			for (int j = 2; j < ss.length; j++) {
				rolelist.add(ss[j]);
			}
			user user=new user(username, num, rolelist);
			users.add(user);
		}
		int q=Integer.parseInt(scanner.nextLine());
		for (int i = 0; i < q; i++) {
			String s=scanner.nextLine();
			String[] ss=s.split(" ");
			String username=ss[0];
			String priviledge=ss[1];
			String cat=getcategory(priviledge);
			int lever=getlever(priviledge);
			category category=new category(cat, lever);
			int ans=query(username,category);
			if (ans==TRUE) {
				System.out.println("true");
			}else if (ans==FALSE) {
				System.out.println("false");
			}else {
				System.out.println(ans);
			}
		}
	}
	private static int query(String username, category category) {
		int ans=FALSE;
		for (user u : users) {
			if (username.equals(u.username)) {
				for (String role:u.roleList) {
					int rt=rolematch(role,category);
					if (rt>ans) {
						ans=rt;
					}
				}
			}
		}
		return ans;
	}
	private static int rolematch(String role, category category) {
		int ans=FALSE;
		for (role r:roles) {
			if (r.role.equals(role)) {
				for (category c:r.catlist) {
					int rt=categorymatch(c,category);
					if (rt>ans) {
						ans=rt;
					}
				}
			}
		}
		return ans;
	}
	private static int categorymatch(category c, category category) {
		if (!c.cat.equals(category.cat)) {
			return FALSE;
		}else if (category.lever==-1) {//不带等级的权限的查询
			if (c.lever==-1) {//不分等级的权限
				return TRUE;
			}else {
				return c.lever;
			}		
		}else {//带等级的权限的查询
			if (c.lever==-1) {
				return TRUE;
			}else if (category.lever<=c.lever) {
				return TRUE;
			}else {
				return FALSE;
			}
		}
	}
	public static String getcategory(String s){
		String category;
		if (s.contains(":")) {
			String[] ss=s.split(":");
			category=ss[0];
		}else {
			category=s;
		}
		return category;
	}
	public static int getlever(String s){
		int lever=-1;
		if (s.contains(":")) {
			String[] ss=s.split(":");
			lever=Integer.parseInt(ss[1]);
		}
		return lever;
	}
}
class category{
	String cat;
	int lever;
	public category(String s,int l) {
		cat=s;
		lever=l;
	}
}
class role{
	String role;
	int prinum;
	List catlist;
	public role(String r,int p,List list) {
		role=r;
		prinum=p;
		catlist=list;
	}
}
class user{
	String username;
	int rolenum;
	List roleList;
	public user(String u,int n,List list) {
		username=u;
		rolenum=n;
		roleList=list;
	}
}

你可能感兴趣的:(ccf,csp,java)