Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Please help - Bank account application

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please help - Bank account application

    hello, I am trying to make a ATM machine like program, everything is pretty basic, but i am getting confused in my if statements, there are 3 different accounts and i dont know how to call a method that gives the menu under each of the accounts. here are my codes

    The main class:
    package stuchkus;
     
    import java.util.Scanner;
     
    public class BankApp
    {
     
        public static void main(String[] args)
        {
            boolean flag = false;
            double depositAmount=50;
            double withdrawAmount;
            int accountNumber;
            int password;
     
            Scanner userInput = new Scanner(System.in);
     
            Account bob = new Account("Bob Jones", 1111, 100.00);
            Account mary = new Account("Mary Mack", 2222, 200.00);
            Account sally = new Account("Sally Star", 1111, 300.00);
     
            do{
     
                showWelcome();
     
                System.out.println("Please enter your account number: ");
     
                accountNumber = userInput.nextInt();
     
                if(accountNumber == -1)
                {
                    showExit();
                    flag = true;
                }
                else
                {
                    System.out.println("Please enter your password: ");
                    password = userInput.nextInt();
     
                    if(accountNumber == 101 && password == 1111)
                    {
     
                    }
                    else if(accountNumber == 102 && password == 2222)
                    {
     
                    }
                    else if(accountNumber == 103 && password == 1111)
                    {
     
                    }
     
                }
     
     
            }while(flag == false);
     
        }
     
     
        public static void accountActivity()
        {
     
        }
     
     
        public static void showMenu()
        {
            System.out.println("******************************");
            System.out.println("*                            *");
            System.out.println("* Please choose an option:   *");
            System.out.println("*                            *");
            System.out.println("*  1. Depoait                *");
            System.out.println("*                            *");
            System.out.println("*  2. Withdraw               *");
            System.out.println("*                            *");
            System.out.println("*  3. Check Balance          *");
            System.out.println("*                            *");
            System.out.println("*  4. Log Out                *");
            System.out.println("*                            *");
            System.out.println("******************************");
        }
        public static void showWelcome()
        {
            System.out.println("******************************");
            System.out.println("*                            *");
            System.out.println("*  Welcome to Brandon's Bank *");
            System.out.println("*                            *");
            System.out.println("******************************");
        }
        public static void showExit()
        {
            System.out.println("***************************************");
            System.out.println("*                                     *");
            System.out.println("*  Thank you for using Brandon's Bank *");
            System.out.println("*                                     *");
            System.out.println("***************************************");
     
     
     
        }
     
    }

    the account class:
    package stuchkus;
     
     
    public class Account
    {
        private String accountName;
        private int password;
        private double balanceAmount;
        private static int accountNumberCounter = 100;
        private int accountNumber;
     
        public Account(String name, int pword, double balance)
        {
            accountName = name;
            password = pword;
            balanceAmount = balance;
            accountNumberCounter++;
            accountNumber = accountNumberCounter;
     
        }
     
        public int acctNum()
        {
            return accountNumber;
        }
     
        public double getBalance()
        {
            return balanceAmount;
     
        }
        public void setBalance(double balance)
        {
            balanceAmount = balance;
     
        }
     
        public void withdraw(double withdrawAmount)
        {
            if(withdrawAmount > balanceAmount)
            {
                System.out.println("You do not have enough money in your account, try again");
            }
            else
            {
                balanceAmount = balanceAmount - withdrawAmount;
            }
     
        }
     
        public void deposit(double depositAmount)
        {
            balanceAmount = balanceAmount + depositAmount;
        }
     
    }

    Please help me
    Last edited by JavaPF; May 2nd, 2010 at 01:42 PM. Reason: Please use code tags


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Please help

    Hello brandonssss.

    Welcome to the Java Programming Forums

    You were almost there. I have made some changes to the BankApp class and it now works as expected:

    import java.util.Scanner;
     
    public class BankApp {
     
    	public static void main(String[] args) {
    		boolean flag = false;
    		double depositAmount = 50;
    		double withdrawAmount;
    		int accountNumber;
    		int password = 0;;
     
    		Scanner userInput = new Scanner(System.in);
     
    		Account bob = new Account("Bob Jones", 1111, 100.00);
    		Account mary = new Account("Mary Mack", 2222, 200.00);
    		Account sally = new Account("Sally Star", 1111, 300.00);
     
    			showWelcome();
     
    			System.out.println("Please enter your account number: ");
     
    			while(userInput.hasNext()){
     
    			accountNumber = userInput.nextInt();
     
    			// if account number is less than 101 or greater than 103 
    			// display error and exit
    			if (accountNumber < 101 | accountNumber > 103) {
     
    				System.out.println("You entered an invalid account number");
    				showExit();
     
    			} else {
     
    				System.out.println("Please enter your password: ");
    				password = userInput.nextInt();
     
    				if(accountNumber == 101 && password == 1111){
    					System.out.println("Welcome account 101");
    					showMenu();
    				}
    				else if (accountNumber == 102 && password == 2222){
    					System.out.println("Welcome account 102");
    					showMenu();
    				}
    				else if (accountNumber == 103 && password == 1111){
    					System.out.println("Welcome account 103");
    					showMenu();
    				}
    				else
    				{
    					System.out.println("You entered an invalid password for account " + accountNumber);
    					showExit();
    				}
     
    			}// end else		
     
    			}// end while
     
    	}
     
    	public static void accountActivity() {
     
    	}
     
    	public static void showMenu() {
    		System.out.println("******************************");
    		System.out.println("*                            *");
    		System.out.println("* Please choose an option:   *");
    		System.out.println("*                            *");
    		System.out.println("*  1. Depoait                *");
    		System.out.println("*                            *");
    		System.out.println("*  2. Withdraw               *");
    		System.out.println("*                            *");
    		System.out.println("*  3. Check Balance          *");
    		System.out.println("*                            *");
    		System.out.println("*  4. Log Out                *");
    		System.out.println("*                            *");
    		System.out.println("******************************");
     
    	}
     
    	public static void showWelcome() {
    		System.out.println("******************************");
    		System.out.println("*                            *");
    		System.out.println("*  Welcome to Brandon's Bank *");
    		System.out.println("*                            *");
    		System.out.println("******************************");
    	}
     
    	public static void showExit() {
    		System.out.println("***************************************");
    		System.out.println("*                                     *");
    		System.out.println("*  Thank you for using Brandon's Bank *");
    		System.out.println("*                                     *");
    		System.out.println("***************************************");
     
    	}
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    May 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please help

    thank you, this helps. but once they do get welcomed and they have logged in, what do i do for each account? say bob logs in, how do i make it so that bobs information is available? say he chooses deposit. they user will enter an amount and id call it from the account class, bob.deposit(amount).. but where would i do all of this? thanks

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Please help - Bank account application

    This isn't exactly the best way to code this application but you could do something like this:

    BankApp class

    import java.util.Scanner;
     
    public class BankApp {
     
    	public static int option;
    	public static int accountNumber, account;
    	public static Scanner userInput = new Scanner(System.in);
     
    	public static void main(String[] args) {
    		boolean flag = false;
    		double depositAmount = 50;
    		double withdrawAmount;
    		int accountNumber;
    		int password = 0;
     
     
    		showWelcome();
     
    		System.out.println("Please enter your account number: ");
     
    		while (userInput.hasNext()) {
     
    			accountNumber = userInput.nextInt();
    			account = accountNumber;
     
    			// if account number is less than 101 or greater than 103
    			// display error and exit
    			if (accountNumber < 101 | accountNumber > 103) {
     
    				System.out.println("You entered an invalid account number");
    				showExit();
     
    			} else {
     
    				System.out.println("Please enter your password: ");
    				password = userInput.nextInt();
     
    				if (accountNumber == 101 && password == 1111) {
    					System.out.println("Welcome account 101");
    					showMenu();
    				} else if (accountNumber == 102 && password == 2222) {
    					System.out.println("Welcome account 102");
    					showMenu();
    				} else if (accountNumber == 103 && password == 1111) {
    					System.out.println("Welcome account 103");
    					showMenu();
    				} else {
    					System.out
    							.println("You entered an invalid password for account "
    									+ accountNumber);
    					showExit();
    				}
     
    			}// end else
     
    		}// end while
     
    	}
     
    	public static void accountActivity() {
     
    	}
     
    	public static void showMenu() {
    		System.out.println("******************************");
    		System.out.println("*                            *");
    		System.out.println("* Please choose an option:   *");
    		System.out.println("*                            *");
    		System.out.println("*  1. Depoait                *");
    		System.out.println("*                            *");
    		System.out.println("*  2. Withdraw               *");
    		System.out.println("*                            *");
    		System.out.println("*  3. Check Balance          *");
    		System.out.println("*                            *");
    		System.out.println("*  4. Log Out                *");
    		System.out.println("*                            *");
    		System.out.println("******************************");
     
    		option = userInput.nextInt();
    		System.out.println("You picked option: " + option);
     
    		// Check accountNumber and pass information over to Account class
    		if (account == 101) {
     
    			Account bob = new Account("Bob Jones", 1111, 100.00);
     
    		} else if (account == 102) {
     
    			Account mary = new Account("Mary Mack", 2222, 200.00);
     
    		} else if (account == 103) {
     
    			Account sally = new Account("Sally Star", 1111, 300.00);
     
    		}
     
    	}
     
    	public static void showWelcome() {
    		System.out.println("******************************");
    		System.out.println("*                            *");
    		System.out.println("*  Welcome to Brandon's Bank *");
    		System.out.println("*                            *");
    		System.out.println("******************************");
    	}
     
    	public static void showExit() {
    		System.out.println("***************************************");
    		System.out.println("*                                     *");
    		System.out.println("*  Thank you for using Brandon's Bank *");
    		System.out.println("*                                     *");
    		System.out.println("***************************************");
     
    	}
     
    }

    Account class

    public class Account {
    	private String accountName;
    	private int password;
    	private double balanceAmount;
    	private static int accountNumberCounter = 100;
    	private int accountNumber;
     
    	public Account(String name, int pword, double balance) {
    		accountName = name;
    		password = pword;
    		balanceAmount = balance;
    		accountNumberCounter++;
    		accountNumber = accountNumberCounter;
     
    		// This shows we have brought the information over from the BankApp class
    		System.out.println(accountName + " " + password + " " + balanceAmount);
    		System.out.println("Option " + BankApp.option + " was selected.");
     
    	}
     
    	public int acctNum() {
    		return accountNumber;
    	}
     
    	public double getBalance() {
    		return balanceAmount;
     
    	}
     
    	public void setBalance(double balance) {
    		balanceAmount = balance;
     
    	}
     
    	public void withdraw(double withdrawAmount) {
    		if (withdrawAmount > balanceAmount) {
    			System.out
    					.println("You do not have enough money in your account, try again");
    		} else {
    			balanceAmount = balanceAmount - withdrawAmount;
    		}
     
    	}
     
    	public void deposit(double depositAmount) {
    		balanceAmount = balanceAmount + depositAmount;
    	}
     
    }

    As you can see, once they select an option from the menu, their information is passed over to the Account class. You can do what you wish with that information then.

    Like I say, this isnt the best way to code this application so i'm sure you are going to come across problems!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.