I don't know what I'm doing wrong, I've at it with trial and error for quite some time and still can't figure this one out. Here's the question and I'll add what I have so far at the bottom. Basically I have no idea how to meet these specifications with what I have from my book. Thanks in advance.

Question :


1. Define a Bank Class to hold Account Objects
a) Has a data structure and attributes to manage the Accounts
on the Heap.
b) A Constructor which creates a Bank of any size at runtime.
c) Methods to open(add) account object, close(remove) account object,
show "Account Summary", make a deposit & make a withdrawal
wrapper methods, and other helper methods that would be appropriate.

2. Write a test program to create a Bank object at runtime, add Account
objects, verify your Methods, and remove objects,

Code :

public class Account extends Bank
{
	int num;
	
	
	
	
	public Account(int a)
	{
		super(a);
		
	}
	public static void createAccount(Bank c[], int a)
	{
		c[a] = balance;
		
	}
	public double getBalance()
	{
		return(balance);
		
	}
	public int getNum()
	{
		return num;
	}

	
	

}
public class Bank
{
	static Bank[] b1;
	public static Bank balance;
	
	public Bank(int a)
	{
		b1 = new Bank[a];
	}
	public void create(int a)
	{	
		double b = ((Object) b1[a]).createAccount(b1,5);
		
	
	}
	public String toString(int a)
	{
		return("Balance for account #" +b1[a]);
	}
}
import java.util.ArrayList;


public class ProgramTester 
{
	public static void main(String[] args)
	{
		
		
		//ArrayList<Account> accounts = new ArrayList<Account>();
		//accounts.add(new Account(5));
		Account a1 = new Account(5);
		System.out.println(a1.toString());
		
		

		
		//Account a1 = accounts.get(1);
		
		
		
		//System.out.println(a1.toString());
		

		
		

		
		
		
		

		
		
		
	
	
	}

}

well ... I've just looked fast over it, haven't got much time at this moment, but for starters: Account is not supposed to extend Bank, Bank is supposed to 'have' 0...n Accounts

a bit like this:

public class Bank{
  // Create an array of Accounts
  private Account[] accounts;

  public Bank(int size){
    this.accounts = new Accounts[size];
  }
  
  public void addAccount(Account newAccount){
    // first, check if there is still some room for new accounts
    ...
    int index = -1;
    // if so, set the index of the array to index
    if ( index != -1 )
      accounts[index] = newAccount;
    else
      System.out.println("The account could not be added");
  }

}
public class Account{

  private double amount;

  public Account(){

  }

  public Account(double cashIn){
    enterCash(cashIn);
  }
  
  public void enterCash(double cashIn){
    this.amount += cashIn;    
  }

  public double withdrawCash(double cashOut){
    // make a check, to be sure if you have enough cash on your account
    if ( enoughCash(cashOut) ){
	this.amount -= cashOut;
	return cashOut;	
    }
    else{
        System.out.println("Sorry, not enough money");
        return -1;
	// of course, you could also create an exception and throw this here, but I'm not really
	// going to put in that much work
    }

  }

  private boolean enoughCash(double cash){
    return this.amount >= cash;
  }
  
}
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.