From e43d2075baee9be340ee0362c97f638dc74b45af Mon Sep 17 00:00:00 2001 From: dichgrem Date: Fri, 19 Dec 2025 16:55:22 +0800 Subject: [PATCH] update:demo7 --- com/study/demo7/BankWithdraw.java | 37 +++++++++++++ com/study/demo7/DivisionCalculator.java | 24 +++++++++ com/study/demo7/UserServiceMain.java | 72 +++++++++++++++++++++++++ users.txt | 2 + 4 files changed, 135 insertions(+) create mode 100644 com/study/demo7/BankWithdraw.java create mode 100644 com/study/demo7/DivisionCalculator.java create mode 100644 com/study/demo7/UserServiceMain.java create mode 100644 users.txt diff --git a/com/study/demo7/BankWithdraw.java b/com/study/demo7/BankWithdraw.java new file mode 100644 index 0000000..2cda6fa --- /dev/null +++ b/com/study/demo7/BankWithdraw.java @@ -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()); + } + } +} diff --git a/com/study/demo7/DivisionCalculator.java b/com/study/demo7/DivisionCalculator.java new file mode 100644 index 0000000..235844f --- /dev/null +++ b/com/study/demo7/DivisionCalculator.java @@ -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(); + } +} diff --git a/com/study/demo7/UserServiceMain.java b/com/study/demo7/UserServiceMain.java new file mode 100644 index 0000000..73259dc --- /dev/null +++ b/com/study/demo7/UserServiceMain.java @@ -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()); + } + } +} diff --git a/users.txt b/users.txt new file mode 100644 index 0000000..dafeb47 --- /dev/null +++ b/users.txt @@ -0,0 +1,2 @@ +姓名: 张三, 年龄: 25 +姓名: 张三, 年龄: 25