0

I need to create an object (a bank) which has an array of clients and bankID. My problem is that I don't know how to create a bank in main function.

Bank class:

public class Bank {
   Client[] client=new Client[20];
   String idBank;

   public Bank(Client[] client,String idBank) {
      this.client=client;
      this.idBank=idBank;
   }

   public Bank(Bank b) {
      b.client=client;
      b.idBank=idBank;
   }

Client class:

public class Client {
   String name;
   String address;
   BancAccount[] banc=new BancAccount[50];

   public Client(String name,String address,BancAccount[] banc) {
      this.name=name;
      this.address=address;
      this.banc=banc;
   }

   public Client(Client c) {
      c.address=address;
      c.name=name;
      c.banc=banc;
   }
}

Main class:

public class Lab5 {
   public static void main(String[] args) {
      Bank[] b1=new Bank(client[3],"14332");
   }
}

These is the problem :

You have to create a program to simulate a bank activity. The system includes the following modules: Bank — clients (array of Client) — idBank (String) 5 BancAccount — accountNumber (String) — amount (float) Client — name (String) — address (String) — accounts (array of BankAccount; a client can have at least an account, but not more than five accounts)

2
  • 2
    What is the 3 being sent to the bank constructor? I thought you said bank takes an array of clients? Commented Apr 12, 2014 at 17:05
  • I wrote that just to have an ideea of what should be there(the number of clients which is stored in an array), I don't know how to do that.... Commented Apr 12, 2014 at 17:10

1 Answer 1

1

Let's look at what you were doing (sans the erroneous []):

Bank b1 =new Bank(3,"14332"); //works, but BAD

Now, that's not very pretty, is it? Let's talk about your Bank class. Specifically, this part:

Client[] client=new Client[20];
public Bank(Client[] client,String idBank)
{
   this.client=client;
   this.idBank=idBank;
}

It doesn't make very much sense to pass 3 in as opposed to an array of Clients. It also seems strange that you declare the Client array like this:

Client[] client=new Client[20];

But you then force the array to be reassigned to something else when a Bank constructor is called. Let's add another constructor:

public Bank(int numClients,String idBank)
{
   client = new Client[numClients];
   this.idBank=idBank;
}

and perhaps an addClient(Client c) method that will enforce the max client size, and a variable to keep track of our current index in the array (essentially making this act like an ArrayList):

private int index = 0;
public void addClient(Client c) {
   if(index == client.length)
      return; //or throw an Exception, your choice here.
   client[index] = c;
   index++;
}

Now, if you want to keep all your classes the same, then you can also just do something like this in main():

Client[] clients = new Client[20]; //or whatever number of Clients is appropriate
//Code to instantiate Clients, whatever that may be
Bank = new Bank(new Client[]{}, "14332");
Sign up to request clarification or add additional context in comments.

2 Comments

Or you can simply use Collections - an ArrayList as an example - to get rid of tedious job of working with arrays. List<Client> clients; as a field and you can add to it by clients.add(client);
@NaderHadjiGhanbari I would have done that to begin with, but OP is obviously just starting to learn the language, and this is probably a homework assignment. I am trying to avoid going into anything too complicated (such as Generics, Collections, etc...).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.