mirror of
https://github.com/Dichgrem/Java.git
synced 2026-02-05 01:31:57 -05:00
update:demo7
This commit is contained in:
37
com/study/demo7/BankWithdraw.java
Normal file
37
com/study/demo7/BankWithdraw.java
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
24
com/study/demo7/DivisionCalculator.java
Normal file
24
com/study/demo7/DivisionCalculator.java
Normal 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();
|
||||
}
|
||||
}
|
||||
72
com/study/demo7/UserServiceMain.java
Normal file
72
com/study/demo7/UserServiceMain.java
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user