/*This code was generated using the UMPLE modeling language!
 Date of generation: 2008/07/30 12:30:06*/
/*This class drives: */

package BankingSystem.core.intangableResources;

import java.util.*;
import BankingSystem.*;
import BankingSystem.core.humanResources.*;
import BankingSystem.core.tangableResources.*;
import BankingSystem.json.*;

public class Account {
  //Class datatypes
  private int accountNumber;

  private Float balance;

  private Float creditLimit;

  //Class association variables
  private List<Client> clients;

  private AccountType accountType;

  private Branch branch;

  //Registry of our system.
  BankingSystemRegistry registry = BankingSystemRegistry.getInstance();

  //Constructor
  public Account(int aAccountNumber, Float aBalance, Float aCreditLimit,
    ArrayList<Client> clients, AccountType aAccountType, Branch aBranch) {
    accountNumber = aAccountNumber;
    balance = aBalance;
    creditLimit = aCreditLimit;
    this.clients = clients;

    accountType = aAccountType;

    branch = aBranch;
    branch.addAccount(this);

  }

  public boolean setAccountNumber(int aAccountNumber) {
    accountNumber = aAccountNumber;
    return true;
  }

  public boolean setBalance(Float aBalance) {
    balance = aBalance;
    return true;
  }

  public boolean setCreditLimit(Float aCreditLimit) {
    creditLimit = aCreditLimit;
    return true;
  }

  public int getAccountNumber() {
    return accountNumber;
  }

  public Float getBalance() {
    return balance;
  }

  public Float getCreditLimit() {
    return creditLimit;
  }

  public List<Client> getClients() {
    return clients;
  }

  public AccountType getAccountType() {
    return accountType;
  }

  public Branch getBranch() {
    return branch;
  }

  public Client addClient(Person aPerson, String aName, String aAddress,
    String aPhoneNumber, ArrayList<Account> accounts) throws Exception {
    if (!(clients.size() == 2)) {
      Client newClient;
      newClient = new Client(aPerson, aName, aAddress, aPhoneNumber, accounts);
      if (!clients.contains(newClient)) {
        clients.add(newClient);
        registry.add(newClient);
      }
      return newClient;
    } else
      throw new Exception("No more space in the set.");
  }

  public Client addClient(Client aClient) throws Exception {
    if (clients.size() < 2) {
      if (!clients.contains(aClient))
        clients.add(aClient);
      return aClient;
    } else
      throw new Exception("No more space in the set.");
  }

  /* This class does not drive AccountType and therefore sets the association unidirectionally.*/
  public void setAccountType(AccountType aAccountType) {
    accountType = aAccountType;
  }

  /* This class does not drive Branch and therefore sets the association unidirectionally.*/
  public void setBranch(Branch aBranch) {
    branch = aBranch;
  }

  public void delete() {
    //Delete all 1 ends.
    branch.deleteAccount(this);
  }

  public void deleteClient(Client aClient) {

    if (clients.contains(aClient)) {
      clients.remove(aClient);

      //registry.removeObj(registry.getKey(aClient));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public boolean areManyEndsNull() {
    return true;
  }

  /***********************************
   * 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("accountNumber", getAccountNumber());
    obj.put("balance", getBalance());
    obj.put("creditLimit", getCreditLimit());
    return obj;
  }

}