Results 1 to 4 of 4
Thread: Problem with program
- 11-27-2012, 12:08 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 2
- Rep Power
- 0
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 } }
- 11-27-2012, 01:58 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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; }Website: Learn Java by Examples
- 11-27-2012, 04:25 AM #3
Member
- Join Date
- Nov 2012
- Posts
- 2
- Rep Power
- 0
Re: Problem with program
where i should add this ?
- 11-27-2012, 05:02 AM #4
Re: Problem with program
Please go through the Forum Rules -- particularly the third paragraph.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Small problem with problem with Java, C++ parse program.
By dragstang86 in forum New To JavaReplies: 4Last Post: 10-30-2011, 03:43 AM -
Little problem in my program
By Pojahn_M in forum New To JavaReplies: 1Last Post: 05-24-2011, 11:42 PM -
Problem in Program
By Abbinormal in forum New To JavaReplies: 9Last Post: 01-08-2010, 03:38 AM -
Gui program problem..
By mingming2009 in forum Advanced JavaReplies: 7Last Post: 04-03-2009, 05:15 AM -
Program problem
By arindamchkrbrty in forum New To JavaReplies: 8Last Post: 03-10-2009, 04:58 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks