gpt4 book ai didi

Java - 一个简单的(对除了我之外的每个人)方法 - 继承

转载 作者:行者123 更新时间:2023-12-02 08:01:03 25 4
gpt4 key购买 nike

好的,所以我希望你们都能帮忙,我的任务是通过使用继承的简单储蓄帐户和支票帐户的古老测试来学习语言。

我的问题是,我想让支票账户不能超过 overdraw 限额,储蓄账户不能低于 0,但不知道如何实现?到目前为止我的代码如下:

super 类(银行帐户):

public class BankAccount
{
protected String CustomerName;
protected String AccountNumber;
protected float Balance;


//Constructor Methods

public BankAccount(String CustomerNameIn, String AccountNumberIn, float BalanceIn)
{
CustomerName = CustomerNameIn;
AccountNumber = AccountNumberIn;
Balance = BalanceIn;

}

// Get name
public String getCustomerName()
{
return (CustomerName);
}

// Get account number
public String getAccountNumber()
{
return (AccountNumber);
}

public float getBalance()
{
return (Balance);
}


public void Withdraw(float WithdrawAmountIn)
{
if(WithdrawAmountIn < 0)
System.out.println("Sorry, you can not withdraw a negative amount, if you wish to withdraw money please use the withdraw method");
else
Balance = Balance - WithdrawAmountIn;
}

public void Deposit(float DepositAmountIn)
{
if(DepositAmountIn < 0)
System.out.println("Sorry, you can not deposit a negative amount, if you wish to withdraw money please use the withdraw method");
else
Balance = Balance + DepositAmountIn;
}


} // End Class BankDetails

子类(储蓄账户):

public class SavingsAccount extends BankAccount
{

private float Interest;

public SavingsAccount(String CustomerNameIn, String AccountNumberIn, float InterestIn, float BalanceIn)
{
super (CustomerNameIn, AccountNumberIn, BalanceIn);
Interest = InterestIn;
}


public float getInterestAmount()
{
return (Interest);
}

public float newBalanceWithInterest()
{
Balance = (getBalance() + (getBalance() * Interest / 100) );

return (Balance);
}

public void SavingsOverdraft()
{
if( Balance < 0)
System.out.println("Sorry, this account is not permitted to have an overdraft facility");
}

}

子类(支票帐户):

public class CheckingAccount extends BankAccount
{
private float Overdraft;


public CheckingAccount(String CustomerNameIn, String AccountNumberIn, float BalanceIn, float OverdraftIn)
{

super (CustomerNameIn, AccountNumberIn, BalanceIn);

Overdraft = OverdraftIn;
}

public float getOverdraftAmount()
{
return(Overdraft);
}

public void setOverdraft()
{
if (Balance < Overdraft)
System.out.println("Sorry, the overdraft facility on this account cannot exceed £100");

}
}

非常感谢您的建议和帮助!

最佳答案

将 getOverdraftAmount() 添加到您的基类:

public int getOverdraftAmount() {
return 0;
}

然后在允许 overdraw 的帐户子类中重写该方法。然后修改提款逻辑,考虑到 overdraw 限额可能不为零。

public void Withdraw(float WithdrawAmountIn)
{
if(WithdrawAmountIn < 0)
System.out.println("Sorry, you can not withdraw a negative amount, if you wish to withdraw money please use the withdraw method");
else if (Balance - WithdrawAmountIn < -getOverdraftAmount()) {
System.out.println("Sorry, this withdrawal would exceed the overdraft limit");
else
Balance = Balance - WithdrawAmountIn;
}

关于Java - 一个简单的(对除了我之外的每个人)方法 - 继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8886994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com