2012-01-16 10 views
0

あなたがすべて助けてくれることを願っていますので、私は単純な預金口座と当座預金口座の古きよきテストで言語を学びたいと思っています。継承。Java - 単純な(私以外の誰もが)メソッド - 継承

私の問題は、当座預金口座を引き落とし限度額にすることができないようにしたいのですが、貯蓄口座は0を下回ることができません。次のように私のコードは、これまでのところです:

SUPERCLASS(銀行口座の):

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

は、Javaを学んでいる間、先端:学び、コーディングスタイル規則を使用します。たとえば、メソッド名と変数名は小文字で始まらない。したがって、あなたの** SavingsOverdraft()**メソッドは** savingsOverdraft()**、** **バランス**は**残高**などでなければなりません。 –

答えて

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; 
    } 
+0

私は残念ながら言及を忘れた1つの問題は、貯蓄は当座貸越手当を持っていないので、0未満になることはできませんが、当座預金口座は-100に行くことができます! – Phil

+0

右。したがって、普通預金口座の残高限度が0であり、当座預金口座の限度額が100である場合、両者は同じ論理を使用することができます。 0の限度は当座貸越能力なしと同じです。 –

+0

Thannkあなたは大変です – Phil

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; 
     } 
    }