package ejava.examples.ejb.session;

import java.io.Serializable;

/**
* This class encapsulates the state of a simple account. It plays the role of
* a ValueObject in an information exchange between the client and server.
*/
public class Account implements Serializable {
   private int id_;
   private double balance_;

   public Account(int id, double balance) {
      id_ = id;
      balance_ = balance;
   }

   public int getId()         { return id_; }
   public double getBalance() { return balance_; }

   public void deposit(double amount) throws BankException{
      if (amount < 0) {
         throw new BankException("negative amount");
      }
      balance_ += amount;
   }

   public void withdraw(double amount) throws BankException {
      if (amount < 0 ) {
         throw new BankException("negative amount");
      }
      else if (balance_ - amount < 0) {
         throw new BankException("insufficient funds");
      }
      else {
         balance_ -= amount;
      }
   }

   public String toString() {
      return "id=" + id_ + ", balance=" + balance_;
   }
}
