mirror of
https://github.com/Dichgrem/Java.git
synced 2026-02-05 05:51:57 -05:00
41 lines
1.2 KiB
Java
41 lines
1.2 KiB
Java
package com.study.demo9;
|
|
|
|
|
|
public class PermissionSystem {
|
|
public static void main(String[] args) {
|
|
User admin = new User("admin", "admin");
|
|
User guest = new User("guest", "guest");
|
|
|
|
System.out.println("Admin can delete: " + admin.checkPermission("delete"));
|
|
System.out.println("Admin can read: " + admin.checkPermission("read"));
|
|
System.out.println("Guest can read: " + guest.checkPermission("read"));
|
|
System.out.println("Guest can delete: " + guest.checkPermission("delete"));
|
|
}
|
|
}
|
|
|
|
class User {
|
|
private String username;
|
|
private String role;
|
|
|
|
public User(String username, String role) {
|
|
this.username = username;
|
|
this.role = role;
|
|
}
|
|
|
|
public boolean checkPermission(String operation) {
|
|
PermissionChecker checker = new PermissionChecker();
|
|
return checker.hasPermission(operation);
|
|
}
|
|
|
|
class PermissionChecker {
|
|
boolean hasPermission(String operation) {
|
|
if ("admin".equals(role)) {
|
|
return true;
|
|
} else if ("guest".equals(role)) {
|
|
return "read".equals(operation);
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
}
|