I am learning aspectj and I have the following program:
- import org.aspectj.lang.JoinPoint;
- privileged aspect newaccount
- {
- void around(account a, double x) : execution(void account.withdraw(double)) && target(a) && args(x){
- if(x < 0){
- System.out.println("trying to cheat!");
- return;
- }
- else if(x > a.balance){
- System.out.println("not enough money!");
- return;
- }
- proceed(a, x);
- }
- void around( account a, double x) : execution(void account.deposit(double)) && target(a) && args(x){
- if(x < 0){
- System.out.println("trying to deposit negtive money!");
- return;
- }
- proceed(x);
- }
- after(): execution(public static void *.main(String[])){
- account a3 = new account(3000,"he nerd");
- a3.withdraw(-100);
- a3.deposit(90);
- a3.printbalance();
- }
- }
I also want to keep track of all the transactions made on an account.
How could I implement that?
I am compiling this program with an existing java program:
- public class account
- {
- private double balance;
- private String owner;
- public account(double x, String s) { balance=x; owner=s; }
- public String owner() { return owner; }
- public void withdraw(double a) { balance -= a; }
- public void deposit(double a) { balance += a; }
- public void printbalance() { System.out.println(balance); }
- // main for testing:
- public static void main(String[] argv)
- {
- account a1 = new account(2000,"you boss");
- account a2 = new account(1000,"me nerd");
- a1.deposit(400);
- a2.withdraw(400); // not enough money!
- a2.withdraw(300); // trying to cheat!
- a1.printbalance();
- a2.printbalance();
- }//main
- } // account
I would appreciate your help. Thank you.
Replies
Know the answer? Post it — somebody with the same question will find it here.