diff --git a/w3/BankAccount.java b/w3/BankAccount.java new file mode 100644 index 0000000..d583f71 --- /dev/null +++ b/w3/BankAccount.java @@ -0,0 +1,63 @@ + +public 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.0; + } + + + public String getAccountNumber() { + return accountNumber; + } + + + public String getOwnerName() { + return ownerName; + } + + public void setOwnerName(String ownerName) { + this.ownerName = ownerName; + System.out.println("户主姓名已更新为:" + ownerName); + } + + + public double getBalance() { + return balance; + } + + + public void deposit(double amount) { + if (amount > 0) { + balance += amount; + System.out.println("存款成功!存入:" + amount + ",当前余额:" + balance); + } else { + System.out.println("存款失败:金额必须大于 0!"); + } + } + + + public void withdraw(double amount) { + if (amount <= 0) { + System.out.println("取款失败:金额必须大于 0!"); + } else if (amount > balance) { + System.out.println("取款失败:余额不足!当前余额:" + balance); + } else { + balance -= amount; + System.out.println("取款成功!取出:" + amount + ",当前余额:" + balance); + } + } + + + public void displayInfo() { + System.out.println("账户号:" + accountNumber); + System.out.println("户主姓名:" + ownerName); + System.out.println("当前余额:" + balance); + } +} \ No newline at end of file