好的,所以我希望你们都能提供帮助,我一直在接受任务,同时通过简单的储蓄账户和使用遗产来检查账户来学习语言。
我的问题是,我想让支票账户无法超过其透支额度,而储蓄账户无法低于0,但无法解决方法如何?我的代码到目前为止如下:
SUPERCLASS(BankAccount):
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
SUBCLASS(SavingsAccount):
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");
}
}
SUBCLASS(CheckingAccount):
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");
}
}
非常感谢您的任何建议和帮助!
答案 0 :(得分:2)
将getOverdraftAmount()添加到基类:
public int getOverdraftAmount() {
return 0;
}
然后在允许透支的帐户子类中覆盖该方法。然后修改您的提款逻辑,以考虑透支限额可能不为零。
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;
}
答案 1 :(得分:0)
要执行此操作,您需要覆盖两个子类中的withdraw函数,以便单独更改每个子函数的功能。
SUBCLASS(SavingsAccount):
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 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 (WithdrawAmountIn>Balance)
System.out.println("Sorry, you don't have this much in your account.");
else
Balance = Balance - WithdrawAmountIn;
}
}
SUBCLASS(CheckingAccount):
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");
}
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, you cannot withdraw this much");
else
Balance = Balance - WithdrawAmountIn;
}
}