Tuesday, February 28, 2012

Challenge 041



public class BankAccount
{
    public double balance;
   
    public BankAccount() {
       
        balance = 0;
       
    }
    public BankAccount(double start)
    {
        balance = start;
    }
    public void Deposit(double amount)
    {
        balance += amount;
        System.out.println("Current balance: $" + balance);
    }
    public void Withdrawl(double amount)
    {
        if(balance >= balance)
        {
            balance -= amount;
            System.out.println("Current balance: $" + balance);
        }
            else
            {
                System.out.println("Insufficient Funds");
            }
    }
    public double getBalance()
    {
        return balance;
    }
}





public class SavingsAccount extends BankAccount
{
    public double interestRate;
    public SavingsAccount(double rate, double start)
    {
        super(start);
        interestRate = rate;
    }
    public double compInterestRate()
    {
        return balance = (balance * interestRate);
    }
}




public class CheckingAccount extends BankAccount
{
    public double minBalance;
    public double fee;
    public CheckingAccount()
    {
    }
   
    public CheckingAccount(double start)
    {
        super(start);
        minBalance = 50.0;
        fee = 2.0;
    }
    public double makeWithdraw(double amount)
    {
        balance -= amount;
        if (balance < minBalance)
        {
            System.out.print("$2.0 fee will be charged due to overdrawing" + (balance + fee));
        }
            else
            {
                System.out.print("Current balance: $" + balance);
            }
        }      
    }      

6 comments:

  1. Hi Ryan,

    Review the following method in the CheckingAccount class:

    public double makeWithdraw(double amount)

    First, as written, it produces a compiler error as it never returns any data. Secondly, the CheckingAccount class should override the withdrawl method from the BankAccount class.

    ReplyDelete
  2. yeah i accidently copied and pasted the wrong code before i fixed that and didnt realize. this is the correct code:

    public class CheckingAccount extends BankAccount
    {
    public double minBalance;
    public double fee;


    public CheckingAccount(double start)
    {
    super(start);
    minBalance = 50.0;
    fee = 2.0;
    }
    public void makeWithdraw(double amount)
    {
    balance -= amount;
    if (balance < minBalance)
    {
    System.out.print("$2.0 fee will be charged due to minimum balance parameter" + (balance + fee));
    }
    else
    {
    System.out.print("Current balance: $" + balance);
    }
    }
    }



    it needed to be void

    ReplyDelete
  3. HI Ryan,

    Your withdraw method in the CheckingAccount class is named differently from the one in the BankAccount class, so there is no method overriding happening here.

    Also, the makeWrithdraw method, as written, shows the user the incorrect balance if they are withdrawing money below the minBalance, then it never subtracts the fee from the balance.

    Resolve when able.

    ReplyDelete
  4. public class BankAccount
    {
    public double balance;

    public BankAccount() {

    balance = 0;

    }
    public BankAccount(double start)
    {
    balance = start;
    }
    public void Deposit(double amount)
    {
    balance += amount;
    System.out.println("Current balance: $" + balance);
    }
    public void Withdrawl(double amount)
    {
    if(balance >= balance)
    {
    balance -= amount;
    System.out.println("Current balance: $" + balance);
    }
    else
    {
    System.out.println("Insufficient Funds");
    }
    }
    public double getBalance()
    {
    return balance;
    }
    }



    public class CheckingAccount extends BankAccount
    {
    private static double minBalance = 50.00;
    private static double fee = 2.00;


    public CheckingAccount(double start)
    {
    super(start);
    }

    public void Withdraw(double amount)
    {
    balance -= amount;
    if (balance < minBalance)
    {
    System.out.print("$2.0 fee will be charged due to minimum balance parameter" + (balance - fee));
    }
    else
    {
    System.out.print("Current balance: $" + balance);
    }
    }
    }


    public class SavingsAccount extends BankAccount
    {
    public double interestRate;
    public SavingsAccount(double rate, double start)
    {
    super(start);
    interestRate = rate;
    }
    public double compInterestRate()
    {
    return balance = (balance * interestRate);
    }
    }

    The fee is now subtracted from the balance when it reaches below the minimum balance. Also, the Withdrawl classes are now the same

    ReplyDelete
  5. after conversing with you and Andy, i resolved all problems in the code. Here SHOULD be the fixed and working code.


    public class BankAccount
    {
    public double balance;

    public BankAccount() {

    balance = 0;

    }
    public BankAccount(double start)
    {
    balance = start;
    }
    public void deposit(double amount)
    {
    balance += amount;
    System.out.println(" Current balance: $ " + balance);
    }
    public void withdrawl(double amount)
    {
    if(balance >= amount)
    {
    balance -= amount;
    System.out.println("Current balance: $" + balance);
    }
    else
    {
    System.out.println("Insufficient Funds");
    }
    }
    public double getBalance()
    {
    return balance;
    }
    }



    public class CheckingAccount extends BankAccount
    {
    private static double minBalance = 50.00;
    private static double fee = -2.00;


    public CheckingAccount(double start)
    {
    super(start);
    }

    public void withdrawl(double amount)
    {
    super.withdrawl(amount);

    if (balance < minBalance)
    {
    System.out.print(" $2.0 fee will be charged due to minimum balance parameter " + (balance + fee));
    }
    else
    {
    System.out.print("Current balance: $" + balance);
    }
    }
    }



    public class SavingsAccount extends BankAccount
    {
    public double interestRate;
    public SavingsAccount(double rate, double start)
    {
    super(start);
    interestRate = rate;
    }
    public double compInterestRate()
    {
    return balance = (balance * interestRate);
    }
    }

    ReplyDelete
  6. Now we're talkin'! Good work. 500 XPs awarded for this suitable completion.

    ReplyDelete