update:demo7

This commit is contained in:
dichgrem
2025-12-19 16:55:22 +08:00
parent 9285e13f89
commit e43d2075ba
4 changed files with 135 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
package com.study.demo7;
// 自定义异常
class InsufficientBalanceException extends Exception {
public InsufficientBalanceException(String message) {
super(message);
}
}
// 账户类
class Account {
private double balance;
public Account(double balance) {
this.balance = balance;
}
public void withdraw(double amount) throws InsufficientBalanceException {
if (amount > balance) {
throw new InsufficientBalanceException("余额不足,当前余额: " + balance);
}
balance -= amount;
System.out.println("取款成功,剩余余额: " + balance);
}
}
// 主类
public class BankWithdraw {
public static void main(String[] args) {
Account account = new Account(1000.0);
try {
account.withdraw(100.0);
} catch (InsufficientBalanceException e) {
System.out.println("取款失败: " + e.getMessage());
}
}
}

View File

@@ -0,0 +1,24 @@
package com.study.demo7;
import java.util.Scanner;
public class DivisionCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
System.out.print("请输入第一个整数 a: ");
int a = Integer.parseInt(scanner.nextLine());
System.out.print("请输入第二个整数 b: ");
int b = Integer.parseInt(scanner.nextLine());
int result = a / b;
System.out.println("结果: " + result);
} catch (ArithmeticException e) {
System.out.println("错误: 除数不能为 0");
} catch (NumberFormatException e) {
System.out.println("错误: 请输入有效的整数");
} finally {
System.out.println("计算结束");
}
scanner.close();
}
}

View File

@@ -0,0 +1,72 @@
package com.study.demo7;
import java.io.*;
// 自定义异常
class InvalidUserException extends Exception {
public InvalidUserException(String message) {
super(message);
}
}
// User 类
class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
// 用户服务类
class UserService {
public void register(User user) throws InvalidUserException {
// 校验
if (user.getName() == null || user.getName().trim().isEmpty()) {
throw new InvalidUserException("用户名不能为空");
}
if (user.getAge() < 0) {
throw new InvalidUserException("年龄不能为负数");
}
// 写入文件
try (BufferedWriter writer = new BufferedWriter(new FileWriter("users.txt", true))) {
writer.write("姓名: " + user.getName() + ", 年龄: " + user.getAge());
writer.newLine();
System.out.println("用户注册成功,信息已保存");
} catch (IOException e) {
System.out.println("用户信息保存失败: " + e.getMessage());
}
}
}
// 主类
public class UserServiceMain {
public static void main(String[] args) {
UserService service = new UserService();
User user1 = new User("张三", 25);
User user2 = new User("李四", -1);
try {
service.register(user1);
} catch (InvalidUserException e) {
System.out.println("注册失败: " + e.getMessage());
}
try {
service.register(user2);
} catch (InvalidUserException e) {
System.out.println("注册失败: " + e.getMessage());
}
}
}

2
users.txt Normal file
View File

@@ -0,0 +1,2 @@
姓名: 张三, 年龄: 25
姓名: 张三, 年龄: 25