-
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
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
}
}
-
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:
Code:
public String userfName()
{
if (fname == null || fname.equals("")) {
System.out.print("Enter your First Name: ");
fname = input.next();
}
return fname;
}
-
Re: Problem with program
where i should add this ?
-
Re: Problem with program
Please go through the Forum Rules -- particularly the third paragraph.
db