我们可以做一个简易银行账户类,支持存款、取款、查看交易记录等。
示例:BankAccount 类
java
复制
编辑
public class BankAccount {
private String accountNumber; // 账号
private String ownerName; // 开户人姓名
private double balance; // 余额
private String[] transactions; // 交易记录
private int transactionCount; // 已记录交易数量
// 构造方法
public BankAccount(String accountNumber, String ownerName, double initialBalance, int maxTransactions) {
this.accountNumber = accountNumber;
this.ownerName = ownerName;
this.balance = initialBalance;
this.transactions = new String[maxTransactions];
this.transactionCount = 0;
addTransaction("开户,初始余额: " + initialBalance);
}
// 获取余额
public double getBalance() {
return balance;
}
// 存款
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
addTransaction("存款: +" + amount);
} else {
System.out.println("存款金额必须大于 0");
}
}
// 取款
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
addTransaction("取款: -" + amount);
} else {
System.out.println("取款失败,金额无效或余额不足");
}
}
// 添加交易记录
private void addTransaction(String record) {
if (transactionCount < transactions.length) {
transactions[transactionCount] = record;
transactionCount++;
} else {
System.out.println("交易记录已满,无法记录更多交易");
}
}
// 打印交易记录
public void printTransactions() {
System.out.println("账户 [" + accountNumber + "] 的交易记录:");
for (int i = 0; i < transactionCount; i++) {
System.out.println((i + 1) + ". " + transactions[i]);
}
}
}
使用示例
java
复制
编辑
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount(“1001”, “Alice”, 1000.0, 10);
account.deposit(500.0);
account.withdraw(300.0);
account.withdraw(1500.0); // 测试余额不足
System.out.println("当前余额: " + account.getBalance());
account.printTransactions();
}
}
你可以用它练习的业务场景
多账户管理
创建多个 BankAccount 对象,存款、取款,最后统计所有账户总余额。
账户转账功能
编写一个方法 transfer(BankAccount target, double amount) 实现账户之间转账。
交易明细查询
根据交易记录查找某一天的交易(可以用字符串包含日期判断)。
存取款限额系统
限制每天的存款或取款次数,测试业务规则。
您说:
麻烦帮我实现一个账户转账功能
ChatGPT 说:
好的 👍
我们就在刚才的 BankAccount 类里加一个 transfer() 方法,实现账户之间的转账功能,并且让交易记录两边都能保存。
更新版 BankAccount 类(带转账功能)
java
复制
编辑
public class BankAccount {
private String accountNumber; // 账号
private String ownerName; // 开户人姓名
private double balance; // 余额
private String[] transactions; // 交易记录
private int transactionCount; // 已记录交易数量
// 构造方法
public BankAccount(String accountNumber, String ownerName, double initialBalance, int maxTransactions) {
this.accountNumber = accountNumber;
this.ownerName = ownerName;
this.balance = initialBalance;
this.transactions = new String[maxTransactions];
this.transactionCount = 0;
addTransaction("开户,初始余额: " + initialBalance);
}
// 获取余额
public double getBalance() {
return balance;
}
// 存款
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
addTransaction("存款: +" + amount);
} else {
System.out.println("存款金额必须大于 0");
}
}
// 取款
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
addTransaction("取款: -" + amount);
} else {
System.out.println("取款失败,金额无效或余额不足");
}
}
// 转账
public void transfer(BankAccount target, double amount) {
if (target == null) {
System.out.println("转账失败:目标账户不存在");
return;
}
if (amount <= 0) {
System.out.println("转账金额必须大于 0");
return;
}
if (amount > this.balance) {
System.out.println("转账失败:余额不足");
return;
}
// 扣款
this.balance -= amount;
addTransaction("向账户[" + target.accountNumber + "] 转账: -" + amount);
// 对方账户增加余额
target.balance += amount;
target.addTransaction("来自账户[" + this.accountNumber + "] 的转账: +" + amount);
System.out.println("转账成功:" + this.accountNumber + " → " + target.accountNumber + " 金额: " + amount);
}
// 添加交易记录
private void addTransaction(String record) {
if (transactionCount < transactions.length) {
transactions[transactionCount] = record;
transactionCount++;
} else {
System.out.println("交易记录已满,无法记录更多交易");
}
}
// 打印交易记录
public void printTransactions() {
System.out.println("账户 [" + accountNumber + "] 的交易记录:");
for (int i = 0; i < transactionCount; i++) {
System.out.println((i + 1) + ". " + transactions[i]);
}
}
}
测试代码
java
复制
编辑
public class Main {
public static void main(String[] args) {
BankAccount acc1 = new BankAccount(“1001”, “Alice”, 1000.0, 10);
BankAccount acc2 = new BankAccount(“1002”, “Bob”, 500.0, 10);
acc1.transfer(acc2, 300.0); // 成功
acc1.transfer(acc2, 2000.0); // 余额不足
acc1.transfer(acc2, -50); // 金额非法
System.out.println("\n---账户余额---");
System.out.println(acc1.getBalance());
System.out.println(acc2.getBalance());
System.out.println("\n---交易记录---");
acc1.printTransactions();
acc2.printTransactions();
}
}
运行示例输出
markdown
复制
编辑
转账成功:1001 → 1002 金额: 300.0
转账失败:余额不足
转账金额必须大于 0
—账户余额—
700.0
800.0
—交易记录—
账户 [1001] 的交易记录:
- 开户,初始余额: 1000.0
- 向账户[1002] 转账: -300.0
账户 [1002] 的交易记录: - 开户,初始余额: 500.0
- 来自账户[1001] 的转账: +300.0
我这里还可以帮你加一个转账限额功能,比如每天最多转 3 次、每次不超过 1000 元,这样更接近真实银行业务逻辑。
你要我帮你加这个限制版本吗?这样就能练习更多业务规则的判断。
您说:
帮我加这个限制版本吧
ChatGPT 说:
好,那我们就在 BankAccount 里加上每天最多转 3 次,每次不超过 1000 元的转账限制,
这里我用简单的计数器来模拟每天的限制,不涉及真实日期(方便初学者练习)。
更新版 BankAccount(带转账次数 & 金额限制)
java
复制
编辑
public class BankAccount {
private String accountNumber; // 账号
private String ownerName; // 开户人姓名
private double balance; // 余额
private String[] transactions; // 交易记录
private int transactionCount; // 已记录交易数量
// 转账限制
private int transferCountToday; // 今日已转账次数
private static final int MAX_TRANSFER_PER_DAY = 3; // 每天最多转 3 次
private static final double MAX_TRANSFER_AMOUNT = 1000.0; // 每次最多 1000 元
public BankAccount(String accountNumber, String ownerName, double initialBalance, int maxTransactions) {
this.accountNumber = accountNumber;
this.ownerName = ownerName;
this.balance = initialBalance;
this.transactions = new String[maxTransactions];
this.transactionCount = 0;
this.transferCountToday = 0;
addTransaction("开户,初始余额: " + initialBalance);
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
addTransaction("存款: +" + amount);
} else {
System.out.println("存款金额必须大于 0");
}
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
addTransaction("取款: -" + amount);
} else {
System.out.println("取款失败,金额无效或余额不足");
}
}
// 转账方法(带限制)
public void transfer(BankAccount target, double amount) {
if (target == null) {
System.out.println("转账失败:目标账户不存在");
return;
}
if (amount <= 0) {
System.out.println("转账金额必须大于 0");
return;
}
if (amount > this.balance) {
System.out.println("转账失败:余额不足");
return;
}
if (amount > MAX_TRANSFER_AMOUNT) {
System.out.println("转账失败:单笔转账金额不能超过 " + MAX_TRANSFER_AMOUNT + " 元");
return;
}
if (transferCountToday >= MAX_TRANSFER_PER_DAY) {
System.out.println("转账失败:今天的转账次数已达上限(" + MAX_TRANSFER_PER_DAY + " 次)");
return;
}
// 执行转账
this.balance -= amount;
target.balance += amount;
transferCountToday++;
addTransaction("向账户[" + target.accountNumber + "] 转账: -" + amount);
target.addTransaction("来自账户[" + this.accountNumber + "] 的转账: +" + amount);
System.out.println("转账成功:" + this.accountNumber + " → " + target.accountNumber + " 金额: " + amount);
}
private void addTransaction(String record) {
if (transactionCount < transactions.length) {
transactions[transactionCount] = record;
transactionCount++;
} else {
System.out.println("交易记录已满,无法记录更多交易");
}
}
public void printTransactions() {
System.out.println("账户 [" + accountNumber + "] 的交易记录:");
for (int i = 0; i < transactionCount; i++) {
System.out.println((i + 1) + ". " + transactions[i]);
}
}
// 模拟新的一天(重置计数)
public void resetDailyTransferLimit() {
transferCountToday = 0;
System.out.println("已重置今日转账次数");
}
}
测试代码
java
复制
编辑
public class Main {
public static void main(String[] args) {
BankAccount acc1 = new BankAccount(“1001”, “Alice”, 5000.0, 20);
BankAccount acc2 = new BankAccount(“1002”, “Bob”, 2000.0, 20);
acc1.transfer(acc2, 500); // 成功
acc1.transfer(acc2, 200); // 成功
acc1.transfer(acc2, 1500); // 超过单笔限额
acc1.transfer(acc2, 300); // 成功
acc1.transfer(acc2, 100); // 超过每日限额
System.out.println("\n---账户余额---");
System.out.println("Alice: " + acc1.getBalance());
System.out.println("Bob: " + acc2.getBalance());
System.out.println("\n---交易记录---");
acc1.printTransactions();
acc2.printTransactions();
// 模拟第二天
acc1.resetDailyTransferLimit();
acc1.transfer(acc2, 800); // 新的一天又可以转了
}
}
运行示例输出
yaml
复制
编辑
转账成功:1001 → 1002 金额: 500.0
转账成功:1001 → 1002 金额: 200.0
转账失败:单笔转账金额不能超过 1000.0 元
转账成功:1001 → 1002 金额: 300.0
转账失败:今天的转账次数已达上限(3 次)
—账户余额—
Alice: 4000.0
Bob: 3000.0
—交易记录—
账户 [1001] 的交易记录:
- 开户,初始余额: 5000.0
- 向账户[1002] 转账: -500.0
- 向账户[1002] 转账: -200.0
- 向账户[1002] 转账: -300.0
账户 [1002] 的交易记录: - 开户,初始余额: 2000.0
- 来自账户[1001] 的转账: +500.0
- 来自账户[1001] 的转账: +200.0
- 来自账户[1001] 的转账: +300.0
已重置今日转账次数
转账成功:1001 → 1002 金额: 800.0
这样你就能练习:
成员变量与静态常量的结合使用
方法参数对象的交互(transfer() 里两个账户对象互相修改)
状态限制与业务规则(转账次数、金额限制)
模拟一天重置操作(resetDailyTransferLimit())
如果你想,我还可以帮你把这个转账限制改成真正基于日期判断,这样就不需要手动调用 resetDailyTransferLimit() 了,更贴近真实银行逻辑。