Ybadoo - Soluções em Software Livre
Tutoriais
Programação Orientada a Objetos

A listagem a seguir apresenta a classe Bank. Transforme a classe Bank em um Singleton.

Arquivo Bank.java

/**
 * Copyright (C) 2009/2018 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

public class Bank
{
  private java.util.Hashtable accounts = new java.util.Hashtable();

  public void addAccount(String name, BankAccount account)
  {
    accounts.put(name, account);
  }

  public double totalholdings()
  {
    double total = 0.0;
    
    java.util.Enumeration enum = accounts.elements();
    
    while(enum.hasMoreElements())
    {
      BankAccount account = (BankAccount)enum.nextElement();
      
      total += account.getBalance();
    }
    
    return total;
  }

  public int totalAccounts()
  {
    return accounts.size();
  }
	
  public void deposit(String name, double ammount)
  {
    BankAccount account = retrieveAccount(name);
    
    if(account != null)
    {
      account.depositFunds(ammount);
    }
  }

  private BankAccount retrieveAccount(String name)
  {
    return (BankAccount) accounts.get(name);
  }
	
  public double balance(String name)
   {
    BankAccount account = retrieveAccount(name);
    
    if(account != null)
    {
      return account.getBalance();
    }
    
    return 0.0;
  }
}

 

Arquivo Bank.java

/**
 * Copyright (C) 2009/2018 - Cristiano Lehrer (cristiano@ybadoo.com.br)
 *                  Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
 *
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.3
 * or any later version published by the Free Software Foundation; with
 * no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU
 * Free Documentation License".
 */

package com.ybadoo.tutoriais.poo;

public class Bank
{
  /*
   * Variavel estatica que armazenara a instancia da classe
   */
  private static Bank instance = new Bank();
  
  /*
   * Construtor privado
   */
  private Bank()
  {
  
  }
  
  /*
   * Metodo publico para retornar a instancia da classe
   *
   * @return instancia da classe
   */
  public static Bank getInstance()
  {
    return instance;
  }

  private java.util.Hashtable accounts = new java.util.Hashtable();

  public void addAccount(String name, BankAccount account)
  {
    accounts.put(name, account);
  }

  public double totalholdings()
  {
    double total = 0.0;
    
    java.util.Enumeration enum = accounts.elements();
    
    while(enum.hasMoreElements())
    {
      BankAccount account = (BankAccount)enum.nextElement();
      
      total += account.getBalance();
    }
    
    return total;
  }

  public int totalAccounts()
  {
    return accounts.size();
  }
	
  public void deposit(String name, double ammount)
  {
    BankAccount account = retrieveAccount(name);
    
    if(account != null)
    {
      account.depositFunds(ammount);
    }
  }

  private BankAccount retrieveAccount(String name)
  {
    return (BankAccount) accounts.get(name);
  }
	
  public double balance(String name)
   {
    BankAccount account = retrieveAccount(name);
    
    if(account != null)
    {
      return account.getBalance();
    }
    
    return 0.0;
  }
}