Results 1 to 4 of 4
  1. #1
    karolek0901 is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Problem with program

    hello i have problem with my code. When i run the program it asking user to enter First Name Last name and account number but then when i will move to menu Check balance or different menu then when i comeback to main menu it ask me to enter information again and i dont have any idea how to fix it to ask me only
    at the beginning to insert this information not everytime when i comeback to main manu

    Java Code:
    /**
     *
     * @author Karol Regulski
     */
    import java.util.Scanner;
    public class Main
    {
    	private double currentBal =1000;
    	Scanner input = new Scanner(System.in);
    
    	public String userfName()
    	{
    		System.out.print("Enter your First Name: ");
    		String fname = input.next();
    		return fname;
    	}
    
    	public String userlName()
    	{
    		System.out.print("Enter your Last Name: ");
    		String lname = input.next();
    		return lname;
    	}
    	public int userAccount()
    	{
    		System.out.print("Enter your account number: ");
    		int account;
    		account = input.nextInt();
    		return account;
    	}
    
    
    	public void mainMenu()
    	{			
    		String lName, fName;
    		int uA;
    		fName = userfName(); 
    		lName = userlName(); 
    		uA = userAccount();
    	
    	
    		int selection;
    		System.out.print("Welcome to the ATM Machine!\n");
    		System.out.println("Select from the following menu options below:\n");
    		System.out.println("========================");
    		System.out.println("| [1]  Check Balance   |");
    		System.out.println("| [2]  Withdrawal      |");
    		System.out.println("| [3]  Deposit         |");
    		System.out.println("| [4]  Exit            |");
            System.out.println("========================");
            System.out.print("Please select your option now: ");
            selection =input.nextInt();
    		
    		if (selection == 1)
    		{
    			viewBalance();
    		}
    		else if (selection == 2)
    		{
    			withdrawFunds();
    		}
    		else if (selection == 3)
    		{
    			depositFunds();
    		}
    		else if (selection == 4)
    		{
    			System.out.println("Name:" + fName + " " + lName);
    			System.out.println("Account Number:" + uA);
    			System.out.println("Current Balance: " + currentBal);
    			System.out.println("Thank you for using ATM! \n Goodbye! \n");
    		}
    	}
    	public void viewBalance()
    	{
    		int selection1;
    		System.out.println("You have selected Balance.\n");
    		System.out.println("\t-- Your Current Balance is:$ " + currentBal);
    		System.out.println("Return to main menu? \n [1] for YES \n");
    		selection1 =input.nextInt();
    		
    		if (selection1 == 1)
    		{
    			mainMenu();
    		}
    	}
    	public void withdrawFunds() 
    	{
    		int withdrawSelection;
    		System.out.println("Amount to withdraw: ");
    		System.out.println("[1] - $20");
    		System.out.println("[2] - $40");
    		System.out.println("[3] - $50");
    		System.out.println("[4] - $100");
    		System.out.println("[5] - MAIN MENU");
    		System.out.print("Please select your option now: ");
    		withdrawSelection =input.nextInt();
    		
    		if (withdrawSelection == 1)
    		{
    			accountWithdraw(20);
    			mainMenu();
    		}	
    		else if (withdrawSelection == 2)
    		{
    			accountWithdraw(40);
    			mainMenu();
    		}	
    		else if (withdrawSelection == 3)
    		{
    			accountWithdraw(50);
    			mainMenu();
    		}
    		else if (withdrawSelection == 4)
    		{
    			accountWithdraw(100);
    			mainMenu();
    		}
    		else if (withdrawSelection == 5)
    		{
    			mainMenu();
    		}
    	}
    	public void accountWithdraw(int withdrawFunds)
    	{
    		currentBal = currentBal -withdrawFunds - 1.50;
    		System.out.println("Please take your funds.");
    	}
    	public void depositFunds()
    	{
    		int addSelection;
    		System.out.println("Amount to deposit: ");
    		System.out.println("[1] - $20");
    		System.out.println("[2] - $40");
    		System.out.println("[3] - $50");
    		System.out.println("[4] - $100");
    		System.out.println("[5] - MAIN MENU");
    		System.out.print("Please select your option now: ");
    		addSelection =input.nextInt();
    		
    		if (addSelection  == 1)
    		{
    			accountAdd(20);
    			mainMenu();
    		}
    		else if (addSelection  == 2)
    		{
    			accountAdd(40);
    			mainMenu();
    		}
    		else if (addSelection  == 3)
    		{
    			accountAdd(50);
    			mainMenu();
    		}
    		else if (addSelection  == 4)
    		{
    			accountAdd(100);
    			mainMenu();
    		}
    		else if (addSelection  == 5)
    		{
    			mainMenu();
    		}	
    	}
    	public void accountAdd (int depositFunds)
    	{
    		currentBal = currentBal +depositFunds - 1.50;
    		System.out.println("Thank you.");
    	}
    	public static void main(String[] args)
    	{
    			
        		new Main().mainMenu();  //creating the instance and calling the method
    	}
    }

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Problem with program

    Hi,

    My suggestion is that you can make the local variables such as fname, lname and account become instance variable of the class. After that change the method that take these three information. In these method you check whether the information has been filled or not. For example the method userfName() become:

    Java Code:
    public String userfName()
    {
        if (fname == null || fname.equals("")) {
            System.out.print("Enter your First Name: ");
            fname = input.next();
        }
        return fname;
    }

  3. #3
    karolek0901 is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Re: Problem with program

    where i should add this ?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Problem with program

    Please go through the Forum Rules -- particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Small problem with problem with Java, C++ parse program.
    By dragstang86 in forum New To Java
    Replies: 4
    Last Post: 10-30-2011, 03:43 AM
  2. Little problem in my program
    By Pojahn_M in forum New To Java
    Replies: 1
    Last Post: 05-24-2011, 11:42 PM
  3. Problem in Program
    By Abbinormal in forum New To Java
    Replies: 9
    Last Post: 01-08-2010, 03:38 AM
  4. Gui program problem..
    By mingming2009 in forum Advanced Java
    Replies: 7
    Last Post: 04-03-2009, 05:15 AM
  5. Program problem
    By arindamchkrbrty in forum New To Java
    Replies: 8
    Last Post: 03-10-2009, 04:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •