+ Reply to Thread
Results 1 to 2 of 2

Thread: Simple Inheritances

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    41

    Simple Inheritances

    I have a super class : BankAccount , and two sub classes : SavingsAccount and CheckingAccount , I want to make the checking account incapable of going over its overdraft limit and the savings account incapable of going below 0 but cant work out how? my code so far is as follows:
    (BankAccount):
    Code:
    public class BankAccount
        {
            protected String CustomerName;
            protected String AccountNumber;
            protected float Balance;
    
     public BankAccount(String CustomerNameIn, String AccountNumberIn, float BalanceIn)
            {
                CustomerName = CustomerNameIn;
                AccountNumber = AccountNumberIn;
                Balance = BalanceIn;
    
            }
    
          public String getCustomerName()
            {
                return (CustomerName);
            }
    
          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;
           }
    
    
        }
    (SavingsAccount):
    Code:
    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");
           }
    
       }
    (CheckingAccount):
    Code:
    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");
    
           }
        }

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    6
    Add getOverdraftAmount() to your base class:
    Code:
    public int getOverdraftAmount() {
        return 0;
    }
    Then override that method in account subclasses that allow overdrafts.
    Code:
    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;
       }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts