安全的用户

简介: 安全的用户

安全的用户

题目

自定义异常类

package Safe;
public class AIException  extends Exception{
    public AIException(){}
    public AIException(String message){
        super(message);
    }
}

X类

package Safe;
import java.util.Comparator;
import java.util.TreeSet;
class X {
    // flag的状态为0,表示没有用户,flag为1表示只有user1登录,flag为2表示只有user2登录,flag为3表示user1与user2都登录
    private int flag;
    // 用户的数据库 存放的是用户的id与密码
    private User userDataBase[] = new User[2];
    // 存放登录的用户的信息
    // 这里按照id正序排序
    private TreeSet<User> users = new TreeSet<User>(new Comparator<User>() {
        @Override
        public int compare(User o1, User o2) {
            int num = 0;
            if (o1.getId() > o2.getId()) num = 1;
            else num = -1;
            return num;
        }
    });
    public TreeSet<User> getUsers() {
        return users;
    }
    public void setUsers(TreeSet<User> users) {
        this.users = users;
    }
    // 用户登录系统 
    boolean loginUser(User anyUser) {
        // 如果是user1那么就是判断登录用户的密码是否相同
        if (anyUser.getId() == 1 && anyUser.getPwd() == this.userDataBase[0].getPwd()) {
            // 登录成功之后 判断 如果user2在线的话 那么flag变为3 否则为1
            if (this.flag == 2) this.flag = 3;
            else this.flag = 1;
            // 然后设置anyUser的状态为true
            // 登录成功
            anyUser.setStatus(true);
            return true;
        } else if (anyUser.getId() == 2 && anyUser.getPwd() == this.userDataBase[1].getPwd()) {
            // 这是登录user2的情况
            if (this.flag == 1) this.flag = 3;
            else this.flag = 2;
            anyUser.setStatus(true);
            return true;
        }
        // 登录失败返回false
        return false;
    }
    public User[] getUserDataBase() {
        return userDataBase;
    }
    public void setUserDataBase(User[] userDataBase) {
        this.userDataBase = userDataBase;
    }
    public int getFlag() {
        return flag;
    }
    public void setFlag(int flag) {
        this.flag = flag;
    }
    public X() {
        // 默认的构造方法 就是初始化数据库 和 flag
        this.flag = 0;
        this.userDataBase[0] = new User(1, "123");
        this.userDataBase[1] = new User(2, "123");
    }
    public void f1(User user) throws AIException{
        // 判断登录的用户是谁 然后这个用户在线吗
        // 判断用户在线吗的时候 需要判断系统中的用户存储是否为空 然后再判断第一个用户是否为id=1
        if (user.getId() == 1 && this.getUsers().first().getId() == 1 && this.getUsers().isEmpty() == false) {
            System.out.println("是f1啊");
        } else {
            // 否则抛出异常
            throw new AIException("Illegal User Action Exception!");
        }
    }
    public void f2(User user) throws AIException{
        // 判断登录的用户是谁 然后这个用户在线吗
        // 判断用户在线吗的时候 需要判断系统中的用户存储是否为空 然后再判断最后一个用户是否为id=2
        if (user.getId() == 2 && this.getUsers().last().getId() == 2 && this.getUsers().isEmpty() == false) {
            System.out.println("是f2啊");
        } else {
            // 否则抛出异常
            throw new AIException("Illegal User Action Exception!");
        }
    }
    public void f3(User user) throws AIException{
        // 如果登录的用户的大小为2的话代表两个用户都登录了 然后就可以使用f3了
        if (this.getUsers().size() == 2) {
            System.out.println("是f3啊");
        } else {
            // 否则抛出异常
            throw new AIException("Illegal User Action Exception!");
        }
    }
}

User类

package Safe;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.TreeSet;
class User {
    // 用户的id
    private int id;
    // 用户的密码
    private String pwd;
    // 用户的状态
    private boolean status;
    public boolean isStatus() {
        return status;
    }
    public void setStatus(boolean status) {
        this.status = status;
    }
    public User() {
    }
    public User(int id, String pwd) {
        this.id = id;
        this.pwd = pwd;
        this.status = false;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}

Test类

package Safe;
public class Test {
    public static void main(String[] args) throws AIException {
        X x = new X();
        User user1 = new User(1, "123");
        User user2 = new User(2, "123");
        x.getUsers().add(user1);
        x.f1(user1);
        x.f1(user2);
        x.getUsers().add(user2);
        x.f2(user1);
        x.f2(user2);
        x.f3(user1);
        x.f3(user2);
    }
}


相关文章
|
小程序
如何让用户给我们做推荐?
AARRR里的最后一个R讲的就是传播。传播就是想办法让用户给我们做推荐。主要有两种方式:用户主动推荐、产品推动用户推荐。
174 0
如何让用户给我们做推荐?
|
数据安全/隐私保护
用户注册登录系统 V2.0
# 准备空列表 users = [] # 准备当前在线用户 online_user = {} while True: # 打印系统提示 print("欢迎使用 用户注册登录系统V2.
1206 0
|
SQL 关系型数据库 数据库
|
关系型数据库 数据库 数据安全/隐私保护
用户
一、创建用户 1、语法 CREATE USER IDENTIFIED BY DEFAULT TABLESPACE TEMPORARY TABLESPACE QUOAT K|M |UNLIMITED ON PROFILE ABC ACCOUNT lock; 2、参数说...
1307 0

热门文章

最新文章