/*This code was generated using the UMPLE modeling language!
 Date of generation: 2008/07/30 12:41:46*/
/*This class drives: BankAccount*/

package BankingSystem.core;

import java.util.*;
import BankingSystem.*;
import BankingSystem.json.*;

public class Branch {
  //Class datatypes
  private String name;

  private String address;

  //Class association variables
  private Bank bank;

  private List<BankAccount> bankAccounts;

  //Registry of our system.
  BankingSystemRegistry registry = BankingSystemRegistry.getInstance();

  //Constructor
  public Branch(String aName, String aAddress, Bank aBank) {
    name = aName;
    address = aAddress;
    bank = aBank;
    bank.addBranch(this);

    bankAccounts = new ArrayList<BankAccount>();
    registry.add(bankAccounts);

  }

  public boolean setName(String aName) {
    name = aName;
    return true;
  }

  public boolean setAddress(String aAddress) {
    address = aAddress;
    return true;
  }

  public String getName() {
    return name;
  }

  public String getAddress() {
    return address;
  }

  public Bank getBank() {
    return bank;
  }

  public List<BankAccount> getBankAccounts() {
    return bankAccounts;
  }

  public BankAccount addBankAccount(String aAccountNumber, String aBalance,
    Float aOverdraftOrCreditLimit, Branch aBranch) {
    BankAccount newBankAccount;
    newBankAccount =
      new BankAccount(aAccountNumber, aBalance, aOverdraftOrCreditLimit, this);
    if (!bankAccounts.contains(newBankAccount)) {
      registry.add(newBankAccount);
      bankAccounts.add(newBankAccount);
    }
    return newBankAccount;
  }

  public BankAccount addBankAccount(BankAccount aBankAccount) {
    if (!bankAccounts.contains(aBankAccount))
      bankAccounts.add(aBankAccount);
    return aBankAccount;
  }

  /* This class does not drive Bank and therefore sets the association unidirectionally.*/
  public void setBank(Bank aBank) {
    bank = aBank;
  }

  public void delete() {
    //Delete all many ends first.
    for (BankAccount aBankAccount : bankAccounts) {
      aBankAccount.delete();
    }
    bankAccounts.clear();

    //Delete all 1 ends.
    bank.deleteBranch(this);
  }

  public void deleteBankAccount(BankAccount aBankAccount) {

    if (bankAccounts.contains(aBankAccount)) {
      bankAccounts.remove(aBankAccount);

      //registry.removeObj(registry.getKey(aBankAccount));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public boolean areManyEndsNull() {
    if (bankAccounts.size() == 0) {
      return true;
    } else
      return false;
  }

  /***********************************
   * Returns the attribute list along with the  
   * class ID in JSON format.
   ***********************************/
  public JSONObject getAttributes() throws JSONException {
    JSONObject obj = new JSONObject();
    obj.put("CLASS_ID", registry.getKey(this));
    obj.put("name", getName());
    obj.put("address", getAddress());
    return obj;
  }

}