1 changed files with 90 additions and 0 deletions
@ -0,0 +1,90 @@ |
|||||
|
package w3; |
||||
|
|
||||
|
|
||||
|
public class BackAccount { |
||||
|
// 内部类,不需要 public
|
||||
|
static class BankAccount { |
||||
|
private final String accountNumber; |
||||
|
private String ownerName; |
||||
|
private double balance; |
||||
|
|
||||
|
public BankAccount(String accountNumber, String ownerName) { |
||||
|
this.accountNumber = accountNumber; |
||||
|
this.ownerName = ownerName; |
||||
|
this.balance = 0; |
||||
|
} |
||||
|
|
||||
|
public void deposit(double amount) { |
||||
|
if (amount > 0) { |
||||
|
balance += amount; |
||||
|
System.out.println("存款成功!当前余额:" + balance); |
||||
|
} else { |
||||
|
System.out.println("存款失败:金额必须大于0!"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void withdraw(double amount) { |
||||
|
if (amount > 0 && amount <= balance) { |
||||
|
balance -= amount; |
||||
|
System.out.println("取款成功!当前余额:" + balance); |
||||
|
} else { |
||||
|
System.out.println("取款失败:金额无效或余额不足!"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public double getBalance() { |
||||
|
return balance; |
||||
|
} |
||||
|
|
||||
|
public void setOwnerName(String newName) { |
||||
|
ownerName = newName; |
||||
|
System.out.println("户主姓名已修改为:" + ownerName); |
||||
|
} |
||||
|
|
||||
|
public String getAccountNumber() { |
||||
|
return accountNumber; |
||||
|
} |
||||
|
|
||||
|
public String getOwnerName() { |
||||
|
return ownerName; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
// 创建账户
|
||||
|
BankAccount account = new BankAccount("123456789", "张三"); |
||||
|
System.out.println("=== 创建银行账户 ==="); |
||||
|
System.out.println("账号:" + account.getAccountNumber()); |
||||
|
System.out.println("户主:" + account.getOwnerName()); |
||||
|
System.out.println("初始余额:" + account.getBalance()); |
||||
|
System.out.println(); |
||||
|
|
||||
|
// 测试存款功能
|
||||
|
System.out.println("=== 存款测试 ==="); |
||||
|
account.deposit(1000); |
||||
|
account.deposit(-200); |
||||
|
account.deposit(500); |
||||
|
System.out.println("当前余额:" + account.getBalance()); |
||||
|
System.out.println(); |
||||
|
|
||||
|
// 测试取款功能
|
||||
|
System.out.println("=== 取款测试 ==="); |
||||
|
account.withdraw(300); |
||||
|
account.withdraw(1500); |
||||
|
account.withdraw(-100); |
||||
|
System.out.println("当前余额:" + account.getBalance()); |
||||
|
System.out.println(); |
||||
|
|
||||
|
// 测试修改户主姓名
|
||||
|
System.out.println("=== 修改户主信息 ==="); |
||||
|
account.setOwnerName("李四"); |
||||
|
System.out.println("当前户主:" + account.getOwnerName()); |
||||
|
System.out.println(); |
||||
|
|
||||
|
// 最终账户信息
|
||||
|
System.out.println("=== 账户最终状态 ==="); |
||||
|
System.out.println("账号:" + account.getAccountNumber()); |
||||
|
System.out.println("户主:" + account.getOwnerName()); |
||||
|
System.out.println("余额:" + account.getBalance()); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue