From 1bcec3a0d08c2737e1a9544655df619862ccad4a Mon Sep 17 00:00:00 2001 From: Zhengjie <2044415419@qq.com> Date: Sun, 22 Mar 2026 14:55:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20'w3'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w3/Test.java | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 w3/Test.java diff --git a/w3/Test.java b/w3/Test.java new file mode 100644 index 0000000..d653cf9 --- /dev/null +++ b/w3/Test.java @@ -0,0 +1,72 @@ +import java.util.Scanner; + +public class Test { + public static void main(String[] args){ + BankAccount account = new BankAccount("abc123","zj"); + + System.out.println("当前余额:" + account.getBalance()); + + account.deposit(500); + account.deposit(-100); + + account.withdraw(200); + account.withdraw(400); + account.withdraw(-50); + + account.setOwnerName("ZJ123"); + System.out.println("修改后户主:" + account.getOwnerName()); + + System.out.println("当前余额:" + account.getBalance()); + } +} + +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) { + if (ownerName == null || ownerName.isBlank()) { + throw new IllegalArgumentException("invalid ownerName"); + } + this.ownerName = ownerName.strip(); + } + + public double getBalance(){ + return balance; + } + + 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 if (amount<=0){ + System.out.println("取款金额必须大于0"); + } else { + System.out.println("取款金额不能超过当前余额"); + } + } +} \ No newline at end of file