Compare commits
No commits in common. "cf6183c5b845816afd360d9949d3d76ff99dd019" and "761fcb4905db6331619ac787ef9ec30d6a988eb4" have entirely different histories.
cf6183c5b8
...
761fcb4905
@ -1,4 +0,0 @@
|
|||||||
package chapter4;
|
|
||||||
|
|
||||||
public class Examaple25 {
|
|
||||||
}
|
|
@ -16,8 +16,7 @@ public class Main {
|
|||||||
case 1: // 圆形
|
case 1: // 圆形
|
||||||
System.out.print("请输入圆形的半径:");
|
System.out.print("请输入圆形的半径:");
|
||||||
double radius = sc.nextDouble();
|
double radius = sc.nextDouble();
|
||||||
Circle circle = new Circle(radius);
|
Shape circle = new Circle(radius);
|
||||||
// Shape circle = new Circle(radius);
|
|
||||||
ShapeCalculate.printArea(circle);
|
ShapeCalculate.printArea(circle);
|
||||||
ShapeCalculate.printPerimeter(circle);
|
ShapeCalculate.printPerimeter(circle);
|
||||||
break;
|
break;
|
||||||
@ -25,8 +24,7 @@ public class Main {
|
|||||||
System.out.print("请输入长方形的长和宽:");
|
System.out.print("请输入长方形的长和宽:");
|
||||||
double length = sc.nextDouble();
|
double length = sc.nextDouble();
|
||||||
double width = sc.nextDouble();
|
double width = sc.nextDouble();
|
||||||
// Shape rectangle = new Rectangle(length, width);
|
Shape rectangle = new Rectangle(length, width);
|
||||||
Rectangle rectangle = new Rectangle(length, width);
|
|
||||||
ShapeCalculate.printArea(rectangle);
|
ShapeCalculate.printArea(rectangle);
|
||||||
ShapeCalculate.printPerimeter(rectangle);
|
ShapeCalculate.printPerimeter(rectangle);
|
||||||
break;
|
break;
|
||||||
|
@ -13,33 +13,12 @@ public final class ShapeCalculate {
|
|||||||
|
|
||||||
// 打印图形面积
|
// 打印图形面积
|
||||||
public static void printArea(Shape shape) {
|
public static void printArea(Shape shape) {
|
||||||
if (shape instanceof Rectangle) {
|
System.out.println("面积:"+shape.getArea());
|
||||||
System.out.println("正方形面积:"+shape.getArea());
|
|
||||||
} else if (shape instanceof Circle) {
|
|
||||||
System.out.println("圆形面积:"+shape.getArea());
|
|
||||||
} else {
|
|
||||||
System.out.println("未知图形面积:" + shape.getArea());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 打印图形周长
|
// 打印图形周长
|
||||||
// public static void printPerimeter(Shape shape) {
|
public static void printPerimeter(Shape shape) {
|
||||||
// if (shape instanceof Rectangle) {
|
System.out.println("周长:"+shape.getPerimeter());
|
||||||
// System.out.print("长方形");
|
|
||||||
// } else if (shape instanceof Circle) {
|
|
||||||
// System.out.print("圆形");
|
|
||||||
// } else {
|
|
||||||
// System.out.print("未知图形");
|
|
||||||
// }
|
|
||||||
// System.out.println("周长:"+shape.getPerimeter());
|
|
||||||
// }
|
|
||||||
|
|
||||||
public static void printPerimeter(Circle circle) {
|
|
||||||
System.out.println("圆形周长:"+circle.getPerimeter());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void printPerimeter(Rectangle rectangle) {
|
|
||||||
System.out.println("长方形周长:"+rectangle.getPerimeter());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 打印计算器菜单
|
// 打印计算器菜单
|
||||||
|
@ -1,90 +0,0 @@
|
|||||||
package chapter4.demo49;
|
|
||||||
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class Main {
|
|
||||||
// 处理用户的输入
|
|
||||||
private static Scanner scanner = new Scanner(System.in);
|
|
||||||
// 用户管理器
|
|
||||||
private static UserManager userManager = UserManager.getInstance();
|
|
||||||
// 当前登录用户
|
|
||||||
private static AbstractUser currentUser = null;
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
while (true) {
|
|
||||||
printMainMenu();
|
|
||||||
|
|
||||||
try {
|
|
||||||
int choice = scanner.nextInt();
|
|
||||||
switch (choice) {
|
|
||||||
case 1:
|
|
||||||
login();
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
register();
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
if (currentUser instanceof AdminUser) {
|
|
||||||
userManager.showAllUsers();
|
|
||||||
} else {
|
|
||||||
System.out.println("权限不足!");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
if (currentUser instanceof AdminUser) {
|
|
||||||
System.out.print("请输入要删除的用户名:");
|
|
||||||
String username = scanner.next();
|
|
||||||
userManager.removeUser(username);
|
|
||||||
System.out.println("删除用户成功!");
|
|
||||||
} else {
|
|
||||||
System.out.println("权限不足!");
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 0:
|
|
||||||
System.out.println("谢谢使用,再见!");
|
|
||||||
System.exit(0);
|
|
||||||
default:
|
|
||||||
System.out.println("无效的选择!");
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.println("发生错误:" + e.getMessage());
|
|
||||||
scanner.nextLine(); // 清除输入缓冲
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void printMainMenu() {
|
|
||||||
System.out.println("\n=====用户管理系统=====");
|
|
||||||
System.out.println("1. 用户登录");
|
|
||||||
System.out.println("2. 用户注册");
|
|
||||||
System.out.println("3. 查看用户列表(需管理员权限)");
|
|
||||||
System.out.println("4. 删除用户(需管理员权限)");
|
|
||||||
System.out.println("0. 退出系统");
|
|
||||||
System.out.println("请选择功能:");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void login() throws UserException {
|
|
||||||
System.out.print("请输入用户名:");
|
|
||||||
String username = scanner.next();
|
|
||||||
System.out.print("请输入密码:");
|
|
||||||
String password = scanner.next();
|
|
||||||
|
|
||||||
currentUser = userManager.login(username, password);
|
|
||||||
System.out.println("登录成功!欢迎 " + currentUser.getUsername());
|
|
||||||
|
|
||||||
if (currentUser instanceof AdminUser) {
|
|
||||||
System.out.println("您以管理员身份登录");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void register() throws UserException {
|
|
||||||
System.out.print("请输入用户名:");
|
|
||||||
String username = scanner.next();
|
|
||||||
System.out.print("请输入密码:");
|
|
||||||
String password = scanner.next();
|
|
||||||
|
|
||||||
NormalUser newUser = new NormalUser(username, password);
|
|
||||||
userManager.addUser(newUser);
|
|
||||||
System.out.println("注册成功!");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,73 +0,0 @@
|
|||||||
package chapter4.demo49;
|
|
||||||
|
|
||||||
// 自定义异常类
|
|
||||||
class UserException extends Exception {
|
|
||||||
public UserException(String message) {
|
|
||||||
super(message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 用户操作接口
|
|
||||||
interface UserOperation {
|
|
||||||
void changePassword(String newPassword) throws UserException;
|
|
||||||
void showInfo();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 抽象用户类
|
|
||||||
abstract class AbstractUser implements UserOperation {
|
|
||||||
protected String username; // 用户名
|
|
||||||
protected String password; // 密码
|
|
||||||
protected String userType; // 用户类型
|
|
||||||
|
|
||||||
// 构造方法
|
|
||||||
public AbstractUser(String username, String password) {
|
|
||||||
this.username = username;
|
|
||||||
this.password = password;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getUsername() {
|
|
||||||
return username;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查密码是否正确
|
|
||||||
public boolean checkPassword(String password) {
|
|
||||||
return this.password.equals(password);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void showInfo() {
|
|
||||||
System.out.println("用户名:" + username + ",用户类型:" + userType);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 普通用户类
|
|
||||||
class NormalUser extends AbstractUser {
|
|
||||||
public NormalUser(String username, String password) {
|
|
||||||
super(username, password);
|
|
||||||
this.userType = "普通用户";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void changePassword(String newPassword) throws UserException {
|
|
||||||
if (newPassword.length() < 6) {
|
|
||||||
throw new UserException("密码长度不能小于6位");
|
|
||||||
}
|
|
||||||
this.password = newPassword;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 管理员类
|
|
||||||
class AdminUser extends AbstractUser {
|
|
||||||
public AdminUser(String username, String password) {
|
|
||||||
super(username, password);
|
|
||||||
this.userType = "管理员";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void changePassword(String newPassword) throws UserException {
|
|
||||||
if (newPassword.length() < 8) {
|
|
||||||
throw new UserException("管理员密码长度不能小于8位");
|
|
||||||
}
|
|
||||||
this.password = newPassword;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,54 +0,0 @@
|
|||||||
package chapter4.demo49;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
class UserManager {
|
|
||||||
// 保证单例模式
|
|
||||||
private static UserManager instance;
|
|
||||||
// 存储所有用户
|
|
||||||
private Map<String, AbstractUser> users;
|
|
||||||
|
|
||||||
private UserManager() {
|
|
||||||
users = new HashMap<>();
|
|
||||||
// 初始化一个管理员账号
|
|
||||||
users.put("admin", new AdminUser("admin", "admin123"));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 单例模式
|
|
||||||
public static UserManager getInstance() {
|
|
||||||
if (instance == null) {
|
|
||||||
instance = new UserManager();
|
|
||||||
}
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addUser(AbstractUser user) throws UserException {
|
|
||||||
if (users.containsKey(user.getUsername())) {
|
|
||||||
throw new UserException("用户名已存在!");
|
|
||||||
}
|
|
||||||
users.put(user.getUsername(), user);
|
|
||||||
}
|
|
||||||
|
|
||||||
public AbstractUser login(String username, String password) throws UserException {
|
|
||||||
AbstractUser user = users.get(username);
|
|
||||||
if (user == null) {
|
|
||||||
throw new UserException("用户不存在!");
|
|
||||||
}
|
|
||||||
if (!user.checkPassword(password)) {
|
|
||||||
throw new UserException("用户名或密码错误!");
|
|
||||||
}
|
|
||||||
return user;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void removeUser(String username) {
|
|
||||||
users.remove(username);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void showAllUsers() {
|
|
||||||
System.out.println("当前系统用户列表:");
|
|
||||||
for (AbstractUser user : users.values()) {
|
|
||||||
user.showInfo();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,2 +0,0 @@
|
|||||||
package chapter4.example14;public class Example14 {
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
package chapter4.object;
|
|
||||||
|
|
||||||
class Student {
|
|
||||||
private String name;
|
|
||||||
private int age;
|
|
||||||
|
|
||||||
public Student(String name, int age) {
|
|
||||||
this.name = name;
|
|
||||||
this.age = age;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String toString() {
|
|
||||||
return "Student {name=" + name + ", age=" + age + "}";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class Test {
|
|
||||||
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user