mirror of
https://github.com/Dichgrem/Java.git
synced 2026-02-05 04:21:56 -05:00
update:demo9
This commit is contained in:
40
com/study/demo9/PermissionSystem.java
Normal file
40
com/study/demo9/PermissionSystem.java
Normal file
@@ -0,0 +1,40 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user