-2
  1. I am having a problem in getting the o/p

Depositing amount.....10000.0

Withdrawing amount.....4999.0

Savings Account Balance is:5001.0Rs

Depositing amount.....9000.0

Loan Account Balance is:11000.0Rs

Withdrawing amount.....5000.0

Loan Account Balance is:16000.0Rs

please help me ,i am a beginner:

package interfaces;
interface Account 
{   
    double accBal;
void deposit(double amt);   
void withdraw(double amt);
void printBalance();
}

 class SavingsAccount implements Account
{

public void deposit(double amt)
{
    System.out.println("Depositing amount....."+amt);
    accBal = accBal + amt;
}
public void withdraw(double amt) 
{
    System.out.println("Withdrawing amount....."+amt);
    accBal=accBal - amt;
}
public void printBalance() 
{
    System.out.println("Savings Account  Balance is:" +accBal+ "Rs");       
}       
 }

 class LoanAccount implements Account
{

 public void deposit(double amt)
{
    System.out.println("Depositing amount....."+amt);
    accBal=accBal - amt;
}
 public void withdraw(double amt) 
{
    System.out.println("Withdrawing amount....."+amt);
    accBal=accBal + amt;
}
public void printBalance()
{
    System.out.println("Loan Account  Balance is:" +accBal+ "Rs");      
}   

 }

public class TestAccount {

public static void main(String[] args)
{
    Account acc1;
    acc1 = new SavingsAccount();
    acc1.deposit(10000);
    acc1.withdraw(4999);
    acc1.printBalance();
    acc1 = new LoanAccount();
    acc1.deposit(9000);
    acc1.printBalance();
    acc1.withdraw(5000);
    acc1.printBalance();

}

}

need to initialise loan account balance =(accBal=20000.00;) i have tried accbal=20000.00 in sub class

  • 1
    Your account is initialized at `0.0`. Why don't just make an initial withdraw of that quantity? Optionally, create a constructor that accepts the initial amount. BTW, use of `float` or `double` numbers to represent currency may lead to rounding issues. – SJuan76 Jun 03 '14 at 10:53
  • Do you have to use an interface? otherwise use abstract classes as described here: http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html – mhafellner Jun 03 '14 at 10:55
  • yes i need to use interface only i tried using abstract class its working fine – user3702697 Jun 03 '14 at 10:56
  • [are you aware of this?](http://stackoverflow.com/q/1858380/1113392) – A4L Jun 03 '14 at 11:00

1 Answers1

0

If you are following good practice and TDD then if you cannot do something because it currently does not support it, Write a test, Write the code, Refactor repeat.

Do not be afraid to throw code away if it is not fit for purpose, the one thing i would do is split all the classes into separate files. Makes it easier to follow what is going on.

as to setting your initial balance, you could set it in the constructor for each class

public class LoanAccount implements Account {

  public LoanAccount(double initialBalance)
  {
      this.accbalance = initialBalance;
  }

}

Theresa Forster
  • 1,914
  • 3
  • 19
  • 35