2 changed files with 64 additions and 0 deletions
@ -0,0 +1,38 @@ |
|||||
|
|
||||
|
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 void deposit(double amount) { |
||||
|
if (amount > 0) { |
||||
|
this.balance += amount; |
||||
|
System.out.println("[业务] 存入 $" + amount + " 成功。"); |
||||
|
} else { |
||||
|
System.out.println("[业务] 存款失败:金额必须大于0。"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public void withdraw(double amount) { |
||||
|
if (amount > 0 && amount <= this.balance) { |
||||
|
this.balance -= amount; |
||||
|
System.out.println("[业务] 取出 $" + amount + " 成功。"); |
||||
|
} else { |
||||
|
System.out.println("[业务] 取款失败:余额不足或金额无效。"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public double getBalance() { |
||||
|
return this.balance; |
||||
|
} |
||||
|
|
||||
|
public String getOwnerName() { |
||||
|
return this.ownerName; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,26 @@ |
|||||
|
public class BankAccountDemo { |
||||
|
public static void main(String[] args) { |
||||
|
System.out.println("====== 开始运行测试程序 ======\n"); |
||||
|
|
||||
|
BankAccount myAccount = new BankAccount("CN-888", "王五"); |
||||
|
myAccount.deposit(5000.0); |
||||
|
myAccount.deposit(2000.0); |
||||
|
|
||||
|
System.out.println("当前户主: " + myAccount.getOwnerName()); |
||||
|
System.out.println("当前余额: $" + myAccount.getBalance()); |
||||
|
|
||||
|
myAccount.withdraw(1000.0); |
||||
|
myAccount.withdraw(100000.0); |
||||
|
myAccount.withdraw(-500.0); |
||||
|
|
||||
|
System.out.println("最终余额: $" + myAccount.getBalance()); |
||||
|
|
||||
|
BankAccount friendAccount = new BankAccount("CN-999", "赵六"); |
||||
|
friendAccount.deposit(100.0); |
||||
|
|
||||
|
System.out.println("王五的余额: $" + myAccount.getBalance()); |
||||
|
System.out.println("赵六的余额: $" + friendAccount.getBalance()); |
||||
|
|
||||
|
System.out.println("\n====== 测试程序结束 ======"); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue