/*This code was generated using the UMPLE modeling language!
 Date of generation: 2008/07/30 12:41:46*/
/*This class drives: ReusableFinancialInstrument*/

package BankingSystem.core;

import java.util.*;
import BankingSystem.*;
import BankingSystem.json.*;

public class FinancialInstitution {
  //Class datatypes
  private String name;

  //Class association variables
  private List<ReusableFinancialInstrument> reusableFinancialInstruments;

  //Registry of our system.
  BankingSystemRegistry registry = BankingSystemRegistry.getInstance();

  //Constructor
  public FinancialInstitution(String aName) {
    name = aName;
    reusableFinancialInstruments = new ArrayList<ReusableFinancialInstrument>();
    registry.add(reusableFinancialInstruments);

  }

  public boolean setName(String aName) {
    name = aName;
    return true;
  }

  public String getName() {
    return name;
  }

  public List<ReusableFinancialInstrument> getReusableFinancialInstruments() {
    return reusableFinancialInstruments;
  }

  public ReusableFinancialInstrument addReusableFinancialInstrument(
    String aNumber, String aPIN, FinancialInstitution aFinancialInstitution,
    Currency aCurrency) {
    ReusableFinancialInstrument newReusableFinancialInstrument;
    newReusableFinancialInstrument =
      new ReusableFinancialInstrument(aNumber, aPIN, this, aCurrency);
    if (!reusableFinancialInstruments.contains(newReusableFinancialInstrument)) {
      registry.add(newReusableFinancialInstrument);
      reusableFinancialInstruments.add(newReusableFinancialInstrument);
    }
    return newReusableFinancialInstrument;
  }

  public ReusableFinancialInstrument addReusableFinancialInstrument(
    ReusableFinancialInstrument aReusableFinancialInstrument) {
    if (!reusableFinancialInstruments.contains(aReusableFinancialInstrument))
      reusableFinancialInstruments.add(aReusableFinancialInstrument);
    return aReusableFinancialInstrument;
  }

  public void delete() {
    //Delete all many ends first.
    for (ReusableFinancialInstrument aReusableFinancialInstrument : reusableFinancialInstruments) {
      aReusableFinancialInstrument.delete();
    }
    reusableFinancialInstruments.clear();

  }

  public void deleteReusableFinancialInstrument(
    ReusableFinancialInstrument aReusableFinancialInstrument) {

    if (reusableFinancialInstruments.contains(aReusableFinancialInstrument)) {
      reusableFinancialInstruments.remove(aReusableFinancialInstrument);

      //registry.removeObj(registry.getKey(aReusableFinancialInstrument));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public boolean areManyEndsNull() {
    if (reusableFinancialInstruments.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());
    return obj;
  }

}