marți, 5 iunie 2012

Account

package pack;

public abstract class Account {

    private float _balance;



    private int _accountNumber;



    public Account(int accountNumber) {

          _accountNumber = accountNumber;

    }



    public void credit(float amount) {

          setBalance(getBalance() + amount);

    }



    public void debit(float amount) throws Exception {

          float balance = getBalance();

          if (balance < amount) {

                throw new Exception("Total balance not sufficient");

          } else {

                setBalance(balance - amount);

          }

    }



    public float getBalance() {

          return _balance;

    }



    public void setBalance(float balance) {

          _balance = balance;

    }



    public static void main(String[] args) throws Exception {

          SavingsAccount account = new SavingsAccount(12456);

          account.credit(100);

          account.debit(50);

    }

}


...............................
package pack;

public aspect JoinPointTraceAspect {

   

    private int _callDepth = -1;



    pointcut tracePoints() : !within(JoinPointTraceAspect);



    before() : tracePoints() {

          _callDepth++;

          print("Before", thisJoinPoint);

    }



    after() : tracePoints() {

          print("After", thisJoinPoint);

          _callDepth--;

    }



    private void print(String prefix, Object message) {

          for (int i = 0, spaces = _callDepth * 2; i < spaces; i++) {

                System.out.print(" ");

          }

          System.out.println(prefix + ": " + message);

    }

}


..........................
package pack;

public class SavingsAccount extends Account {

   

    public SavingsAccount(int accountNumber) {

          super(accountNumber);

    }

}

Niciun comentariu:

Trimiteți un comentariu