Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] exception handling within several methods

I'm trying to figure out to change the exceptions for the scanner in several different methods into one aspect, I have done several things but a few times I've tried to run it I'm getting a no main error and no aspectj runtime error here is the code:

import java.util.Scanner;

public class atm
{
        static Scanner sc = new Scanner(System.in);
        static int check = 1000;
        static int saving = 3500;
        static int i = 0;
        static int c = 0;
        static int s = 0;
        static int p = 0;
        public static void main(String[] args)
        {
                welcome();
                login();
                System.out.println("Your checking has a balance of: $" + check + "\nYour savings has a balance of: $" + saving);
                menu();
                choice();
                end();
        }

        public static void welcome()
        {
                String welcome;
                welcome = "Welcome Mr. Smith, please enter your pin:";
                System.out.println(welcome);
        }
       
        public static void login()
        {
                String wrong;
                wrong = "You have entered the wrong pin, please try again";
                int x = 1234;
                int y = 0;
                Scanner sc = new Scanner(System.in);
                try
                {
                        y = sc.nextInt ();
                }
                catch (Exception e)
                {
                        System.out.printf("only numbers are allowed\n");
                }
                if (x!= y)
                {
                        System.out.println(wrong);
                        p = p + 1;
                        if ( p == 3)
                        {
                                System.out.println("You did not enter your pin 3 times, atm is ending, please try again later");
                                end();
                        }
                        int x1 = 3 - p;
                        System.out.println("You have " + x1 + " more times to enter a correct value");
                        System.out.println("Please enter your pin again:");
                        login();
                }
                else
                {
                System.out.println("Thank you Mr. Smith");
                }
        }
       
        public static void menu()
        {
                String choice;
                choice = "What would you like to do";
                System.out.println(choice + ":" + "\n1 Withdraw" + "\n2 Transfer");
        }
       
        public static void choice()
        {
                Scanner sc = new Scanner(System.in);
                int option = 0;
                try
                {
                        option = sc.nextInt();
                }
                catch (Exception e)
                {
                        System.out.printf("only numbers are allowed\n");
                }
                if (option == 1)
                {
                        withdraw();
                }
                if (option == 2)
                {
                        transfer();
                }
                else
                {
                        System.out.println("You did not enter a valid option, please try again");
                        choice();
                }
        }
       
        public static void transfer()
        {
                System.out.println("Which account would you like to transfer from \n1 Checking \n2 Savings");
                Scanner sc = new Scanner(System.in);
                int option = 0;
                try
                {
                        option = sc.nextInt();
                }
                catch (Exception e)
                {
                        System.out.printf("only numbers are allowed\n");
                        transfer();
                }
                if (option == 1)
                {
                        checkingtransfer();
                        System.out.println("You chose to transfer money from checking to savings");
                }
                if (option == 2)
                {
                        savingstransfer();
                        System.out.println("You chose to transfer money from savings to checking");
                }
                else
                {
                        System.out.println("You did not enter a valid option, please try again");
                        transfer();
                }
        }
       
        public static void checkingtransfer()
        {
                System.out.println("How much would you like to move:");
                Scanner sc = new Scanner(System.in);
                int value = 0;
                try
                {
                        value = sc.nextInt();
                }
                catch (Exception e)
                {
                        System.out.printf("only numbers are allowed\n");
                        checkingtransfer();
                }
                if (value <= check)
                {
                        check = check - value;
                        saving = saving + value;
                        System.out.println("You have transfered $" + value + " to your savings from your checking");
                        System.out.println("Your checking has a balance of: $" + check + "\nYour savings has a balance of: $" + saving);
                        dosomethingelse();
                }
                else
                {
                        System.out.println("You do not have enough in your checking to transfer that amount");
                        c = c + 1;
                        if ( c == 3)
                        {
                                System.out.println("You failed to enter a correct value 3 times, atm is ending, please try again later");
                                end();
                        }
                        int x = 3 - c;
                        System.out.println("You have " + x + " more times to enter a correct value");
                        checkingtransfer();
                }
        }
       
        public static void savingstransfer()
        {
                System.out.println("How much would you like to move:");
                Scanner sc = new Scanner(System.in);
                int value = 0;
                try
                {
                        value = sc.nextInt();
                }
                catch (Exception e)
                {
                        System.out.printf("only numbers are allowed\n");
                        savingstransfer();
                }
                if (value <= saving)
                {
                        saving = saving - value;
                        check = check + value;
                        System.out.println("You have transfered $" + value + " to your checking from your savings");
                        System.out.println("Your checking has a balance of: $" + check + "\nYour savings has a balance of: $" + saving);
                        dosomethingelse();
                }
                else
                {
                        System.out.println("You do not have enough in your savings to transfer that amount");
                        s = s + 1;
                        if ( s == 3)
                        {
                                System.out.println("You failed to enter a correct value 3 times, atm is ending, please try again later");
                                end();
                        }
                        int x = 3 - s;
                        System.out.println("You have " + x + " more times to enter a correct value");
                        checkingtransfer();
                }
        }
       
        public static void dosomethingelse()
        {
                System.out.println("Would you like to withdraw money as well? \n1 Yes \n2 No");
                Scanner sc = new Scanner(System.in);
                int option = 0;
                try
                {
                        option = sc.nextInt();
                }
                catch (Exception e)
                {
                        System.out.printf("only numbers are allowed\n");
                        dosomethingelse();
                }
                if (option == 1)
                {
                        withdraw();
                }
                if (option == 2)
                {
                        end();
                }
                else
                {
                        System.out.println("You did not enter a valid option, please try again");
                        choice();
                }
        }
       
        public static void withdraw()
        {
                System.out.println("FYI: You may only withdraw from checking");
                System.out.println("What would you like to withdraw(it must be in denominations of 20 and no more than 200):");
                Scanner sc = new Scanner(System.in);
                int wm = 0;
                try
                {
                        wm = sc.nextInt();
                }
                catch (Exception e)
                {
                        System.out.printf("only numbers are allowed\n");
                        withdraw();
                }
                if (wm <= 200 & (wm % 20) == 0 & check >= 0 & check - wm >= 0)
                {
                        System.out.println("removing $" + wm + " from your checking account");
                        check = check - wm;
                        System.out.println("Your new balance is: $" + check);
                        System.out.println("Printing out your money, please remove your card money and reciept from the machine.");
                        end();
                }
                else
                {
                        if (check < 0 | check - wm < 0 & i < 3)
                        {
                                System.out.println("Your account does not have enough funds");
                                i = i + 1;
                                if ( i == 3)
                                {
                                        System.out.println("You did not enter a correct value 3 times that was less than the amount of funds you had, atm is ending, please try again later");
                                        end();
                                }
                                int x = 3 - i;
                                System.out.println("You have " + x + " more times to enter a correct value");
                                withdraw();
                        }
                        if (wm > 200 | (wm % 20) != 0 & i < 3);
                        {
                                System.out.println("You have entered an incorrect withdraw value");
                                i = i + 1;
                                if ( i == 3)
                                {
                                        System.out.println("You did not enter a correct value 3 times, atm is ending, please try again later");
                                        end();
                                }
                                int x = 3 - i;
                                System.out.println("You have " + x + " more times to enter a correct value");
                                withdraw();
                        }
                }
        }
       
        public static void end()
        {
                System.out.println("Thank you, Have a nice day");
                System.exit(0);
        }
}

public aspect exception
{
        declare soft : java.io.IOException : execution(void atm.login());


         void around() : call(void atm.login())
         {
           try
           {
                   proceed();
           }
           catch(Exception e)
           {
                   System.out.println("aspect caught: only numbers are allowed\n");
           }
         }
}

Back to the top