I am working on modifying a BankAccount class for a project I'm doing. I am almost finished, but I am stuck on my second constructor. If a user has an existing account (i.e if there is an initial balance), then the BankAccount constructor should call a deposit method to allow the user to add some money if he or she wants to. However, the parameter which passes the value to the deposit method is not available for this constructor and I know that I can not simply add that parameter to the BankAccount constructor. Right now, I have decided to use a separate variable for that constructor exclusively, but I am afraid it my break my partner's code. What would be a better way to go about this?

/*
The variable I am using for the called deposit method in 
this constructor.
*/
private double amount;

   /**
      Constructs a bank account with a given balance
      @param anAccountNumber the account number for this account
      @param initialBalance the initial balance
   */
   public BankAccount(int anAccountNumber, double initialBalance)
   {  
	  
	  status= OPEN_ACCOUNT;
	  transactions= new ArrayList<Double>();
      accountNumber = anAccountNumber;
      balance = initialBalance;
      getStatus();
      if(initialBalance >= 0)
      {
    	  deposit(amount);
      }
   }
/**
      Deposits money into the bank account.
      @param amount the amount to deposit
   */
   public void deposit(double amount)
   {  
	   isOpen();
      if(status.equals(OPEN_ACCOUNT) && amount > 0)
      {
    	  double newBalance = balance + amount;
          balance = newBalance;
          addTransaction(amount);
          
      }
	  
   }

Recommended Answers

All 2 Replies

the BankAccount constructor should call a deposit method to allow the user to add some money if he or she wants to. However, the parameter which passes the value to the deposit method is not available for this constructor and I know that I can not simply add that parameter to the BankAccount constructor. Right now, I have decided to use a separate variable for that constructor exclusively, but I am afraid it my break my partner's code. What would be a better way to go about this?

That doesn't make a whole lot of sense to me. If you want a bank account to have an initial balance (that isn't 0), and that is based on a previous account, pass that amount into the constructor as a parameter. Then set the 'amount' variable to the amount passed in. You don't need to use the deposit method at all in your constructor. Also, there should be no reason your partner can break your code. . all that anyone using your code should need to know are the parameters necessary to call the method, what those parameters represent, and what the outcome of the method call should be. They don't need to know any of the details.

If that isn't helpful you will need to elaborate on what your problem is.

Calling a constructor with account number and balance is a little redundant. Are you sure you don't need a constructor with account number and deposit instead?

public BankAccount(int anAccountNumber, double depositAmount)   {  
accountNumber = anAccountNumber;
balance = //do something here to load balance from account number

//your other code in here

//make the deposit
deposit(depositAmount);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.