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

Java实现员工姓名及对应多个银行账户名称的展示功能

Java 功能实现代码

1. 定义银行账户类 BankAccount

public class BankAccount {
    // 账户类型 示例值:saving
    private String accountType;
    // 银行名称 示例值:citi bank、sbi
    private String accountName;
    // 账户号
    private String accountNo;
    // 账户所属员工
    private Employee accountHolder;

    // 构造方法
    public BankAccount(String accountType, String accountName, String accountNo) {
        this.accountType = accountType;
        this.accountName = accountName;
        this.accountNo = accountNo;
    }

    // Getter & Setter
    public String getAccountType() {
        return accountType;
    }

    public void setAccountType(String accountType) {
        this.accountType = accountType;
    }

    public String getAccountName() {
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    public String getAccountNo() {
        return accountNo;
    }

    public void setAccountNo(String accountNo) {
        this.accountNo = accountNo;
    }

    public Employee getAccountHolder() {
        return accountHolder;
    }

    public void setAccountHolder(Employee accountHolder) {
        this.accountHolder = accountHolder;
    }
}

2. 定义员工类 Employee

import java.util.ArrayList;

public class Employee {
    private String name;
    private String id;
    // 名下银行账户列表
    private ArrayList<BankAccount> bankAccountList;

    // 构造方法
    public Employee(String name, String id) {
        this.name = name;
        this.id = id;
        // 初始化账户列表
        this.bankAccountList = new ArrayList<>();
    }

    // 新增银行账户方法,自动维护双向关联
    public void addBankAccount(BankAccount account) {
        bankAccountList.add(account);
        account.setAccountHolder(this);
    }

    // 按要求格式打印员工及账户信息
    public void printAccountInfo() {
        System.out.println("Name: " + name);
        System.out.print("bank accounts:");
        for (int i = 0; i < bankAccountList.size(); i++) {
            // 第一个账户直接跟在提示后,后续账户前面补和提示等长的空格对齐
            if (i != 0) {
                System.out.print("              ");
            }
            System.out.println((i+1) + ". " + bankAccountList.get(i).getAccountName());
        }
    }

    // Getter & Setter
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public ArrayList<BankAccount> getBankAccountList() {
        return bankAccountList;
    }

    public void setBankAccountList(ArrayList<BankAccount> bankAccountList) {
        this.bankAccountList = bankAccountList;
    }
}

3. 测试运行类

public class Main {
    public static void main(String[] args) {
        // 1. 创建员工对象
        Employee john = new Employee("john", "E001");
        // 2. 创建多个银行账户
        BankAccount citiAccount = new BankAccount("saving", "citi bank", "12345");
        BankAccount sbiAccount = new BankAccount("saving", "sbi", "12346");
        BankAccount canaraAccount = new BankAccount("saving", "canara bank", "12347");
        // 3. 给员工绑定账户
        john.addBankAccount(citiAccount);
        john.addBankAccount(sbiAccount);
        john.addBankAccount(canaraAccount);
        // 4. 打印输出
        john.printAccountInfo();
    }
}

运行输出结果

Name: john
bank accounts:1. citi bank
              2. sbi
              3. canara bank

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 03:27:03