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.

56 lines
1.5 KiB

public class BankAccount {
private final String accountNumber;
private String ownerName;
private double balance;
public BankAccount(){
this("000000","未命名");
}
public BankAccount(String accountNumber, String ownerName) {
this.accountNumber = accountNumber;
this.ownerName = ownerName;
this.balance=0.0;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
public void deposit(double amount) {
if(amount>0){
this.balance += amount;
System.out.println("存款成功,当前余额:" +this.balance);
}else{
System.out.println("存款必须大于0");
}
}
public void withdraw(double amount) {
if(amount<=0){
System.out.println("取款金额必须大于0");
}else if(amount>this.balance){
System.out.println("金额不足,当前余额:"+this.balance);
}else {
this.balance -= amount;
System.out.println("取款成功!当前余额:" + this.balance);
}
}
// 查询余额的方法
public double getBalance() {
return this.balance;
}
// 获取账户号的方法(因为账户号不可修改,只提供getter)
public String getAccountNumber() {
return this.accountNumber;
}
// 获取户主姓名的方法
public String getOwnerName() {
return this.ownerName;
}
}