添加一个功能,使BankAccount被用户选择suspend时,CurrentAccount不能进行存款和取款操作,并输出“该用户已经被挂起”反之可以进行,用Java来编写BankAccount和CurrentAccount,
时间: 2024-05-01 18:19:56 浏览: 111
Java语言编写实现银行账开户存款取款和转账等功能.pdf
好的,以下是BankAccount和CurrentAccount的Java代码,已经添加了挂起功能:
```java
public class BankAccount {
private double balance;
private boolean suspended;
public BankAccount() {
this.balance = 0;
this.suspended = false;
}
public void deposit(double amount) {
if (!suspended) {
balance += amount;
System.out.println("Deposit successful. New balance: " + balance);
} else {
System.out.println("This account has been suspended. Deposit failed.");
}
}
public void withdraw(double amount) {
if (!suspended) {
if (balance >= amount) {
balance -= amount;
System.out.println("Withdrawal successful. New balance: " + balance);
} else {
System.out.println("Insufficient funds. Withdrawal failed.");
}
} else {
System.out.println("This account has been suspended. Withdrawal failed.");
}
}
public void suspend() {
suspended = true;
System.out.println("This account has been suspended.");
}
public void unsuspend() {
suspended = false;
System.out.println("This account has been unsuspended.");
}
}
public class CurrentAccount extends BankAccount {
private double overdraftLimit;
public CurrentAccount(double overdraftLimit) {
super();
this.overdraftLimit = overdraftLimit;
}
@Override
public void withdraw(double amount) {
if (!super.suspended) {
if (super.balance + overdraftLimit >= amount) {
super.balance -= amount;
System.out.println("Withdrawal successful. New balance: " + super.balance);
} else {
System.out.println("Exceeded overdraft limit. Withdrawal failed.");
}
} else {
System.out.println("This account has been suspended. Withdrawal failed.");
}
}
}
```
在BankAccount中添加了一个boolean变量suspended来表示账户是否被挂起。在deposit()和withdraw()方法中,如果账户被挂起,就输出错误信息并返回。在suspend()方法中,将suspended变量设置为true来挂起账户。在unsuspend()方法中,将suspended变量设置为false来解除挂起。
在CurrentAccount中重写了withdraw()方法,以便处理透支的情况。如果当前余额和透支额度大于等于取款金额,就可以取款;否则,就输出错误信息并返回。在withdraw()方法中也加入了对账户是否被挂起的判断。
希望这个代码可以帮助你实现所需的功能!
阅读全文