You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中通过注册编号查找CustomerAccount对象时自定义异常与输出问题

解决方法:让查找方法输出找到的CustomerAccount属性

没问题,咱们一步步来调整你的方法,让它既能在找不到注册编号时抛出自定义异常,找到时又能清晰输出对象的所有属性。

1. 先确认自定义异常已正确定义

如果还没创建自定义异常,先写一个继承自Exception的类,专门用来处理找不到目标注册编号的情况:

public class InvalidRegistrationNumberException extends Exception {
    public InvalidRegistrationNumberException(String message) {
        super(message);
    }
}

2. 修改查找方法,添加属性输出逻辑

核心思路是:遍历ArrayList<CustomerAccount>,找到匹配注册编号的对象后,要么手动输出每个属性,要么利用重写的toString()方法简化输出;遍历结束仍未找到则抛出咱们定义的异常。

方案一:手动输出每个属性(灵活可控)

适合需要自定义输出格式的场景:

import java.util.ArrayList;

public class AccountService {
    // 查找客户账户的方法
    public CustomerAccount findCustomerByRegistrationNumber(ArrayList<CustomerAccount> accounts, String targetRegNum) throws InvalidRegistrationNumberException {
        for (CustomerAccount account : accounts) {
            if (account.getRegistrationNumber().equals(targetRegNum)) {
                // 逐行输出所有属性
                System.out.println("✅ 找到匹配的客户账户:");
                System.out.println("注册编号:" + account.getRegistrationNumber());
                System.out.println("客户姓名:" + account.getName());
                System.out.println("联系邮箱:" + account.getEmail());
                System.out.println("账户余额:" + account.getBalance());
                // 可以继续添加其他需要输出的属性
                return account; // 找到后返回对象,方便后续业务逻辑使用
            }
        }
        // 遍历完未找到目标,抛出异常
        throw new InvalidRegistrationNumberException("❌ 未找到注册编号为 " + targetRegNum + " 的客户账户");
    }
}

方案二:重写CustomerAccount的toString()方法(简洁高效)

先给CustomerAccount类重写toString()方法,把所有属性拼接成友好的字符串:

public class CustomerAccount {
    private String registrationNumber;
    private String name;
    private String email;
    private double balance;

    // 构造方法、getter/setter方法省略...

    @Override
    public String toString() {
        return "CustomerAccount{" +
                "注册编号='" + registrationNumber + '\'' +
                ", 客户姓名='" + name + '\'' +
                ", 联系邮箱='" + email + '\'' +
                ", 账户余额=" + balance +
                '}';
    }
}

然后在查找方法里直接输出对象即可:

public CustomerAccount findCustomerByRegistrationNumber(ArrayList<CustomerAccount> accounts, String targetRegNum) throws InvalidRegistrationNumberException {
    for (CustomerAccount account : accounts) {
        if (account.getRegistrationNumber().equals(targetRegNum)) {
            System.out.println("✅ 找到匹配的客户账户:");
            System.out.println(account); // 自动调用toString()输出所有属性
            return account;
        }
    }
    throw new InvalidRegistrationNumberException("❌ 未找到注册编号为 " + targetRegNum + " 的客户账户");
}

3. 测试方法效果

写个简单的测试代码验证功能:

public class Main {
    public static void main(String[] args) {
        ArrayList<CustomerAccount> accounts = new ArrayList<>();
        accounts.add(new CustomerAccount("REG001", "张三", "zhangsan@example.com", 1500.0));
        accounts.add(new CustomerAccount("REG002", "李四", "lisi@example.com", 2300.0));

        AccountService service = new AccountService();
        try {
            // 查找有效注册编号
            service.findCustomerByRegistrationNumber(accounts, "REG001");
            // 查找无效注册编号
            service.findCustomerByRegistrationNumber(accounts, "REG999");
        } catch (InvalidRegistrationNumberException e) {
            System.err.println(e.getMessage());
        }
    }
}

运行后会看到如下输出:

✅ 找到匹配的客户账户:
CustomerAccount{注册编号='REG001', 客户姓名='张三', 联系邮箱='zhangsan@example.com', 账户余额=1500.0}
❌ 未找到注册编号为 REG999 的客户账户

这样就完美实现了你要的效果:找到目标账户时输出所有属性,找不到时抛出自定义异常。

内容的提问来源于stack exchange,提问作者Amanda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:00:14