You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.7 KiB

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;
}
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) {
System.out.println("取款失败:取款金额必须大于0!");
} else if (amount > balance) {
System.out.println("取款失败:余额不足!");
} else {
balance -= amount;
System.out.println("取款成功!当前余额:" + balance);
}
}
public static void main(String[] args) {
BankAccount account = new BankAccount("622202123456789", "张三");
System.out.println("账号:" + account.getAccountNumber());
System.out.println("户主:" + account.getOwnerName());
System.out.println("初始余额:" + account.getBalance());
account.deposit(500);
account.withdraw(200);
account.withdraw(400);
}
}