Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I am creating a mini ATM application and I have a few problems with it.
1. It loops after I ask it to withdraw
2. doesnt carry newBalance throughout the program
3.I need an extra feature other than withdraw, deposit and display balance

What I have tried:

import java.util.*;
public class ATM {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input=new Scanner (System.in);
		String acctNum,password,orignalBalance="error";
		int mainMenu=0;
		int counter=3;
		double x=0;
		do
		{
			System.out.println("Enter the account number:");
			acctNum=input.next();
			System.out.println("Enter the pin:");
			password=input.next();
			orignalBalance=validation(acctNum,password);
			counter--;
			if(counter==0)
			{
				System.out.println("Maximum amount of attempts");
				break;
			}
			else if(!(orignalBalance.equals("error")))
			{
				break;
			}
		}while(orignalBalance.equals("error"));
		{
		double balance=Double.parseDouble(orignalBalance);
		mainMenu=mainMenuOption();
		while(!(mainMenu==4))
		if (mainMenu==1)
		{
			x=withdrawl(balance);
			System.out.println("Balance= $"+x);
		}
		else if (mainMenu==2)
		{
			x=deposit(balance);
			System.out.println("Balance= $"+x);
		}
	}
}

	public static double deposit(double balance) {
		// TODO Auto-generated method stub
		Scanner input=new Scanner(System.in);
		double deposit=0;
		double newBalance=0;
		System.out.println("How much money would you like to input?");
		deposit=input.nextDouble();
		newBalance=balance+deposit;
		return newBalance;
	}
	public static int mainMenuOption() {
		// TODO Auto-generated method stub
		Scanner input=new Scanner(System.in);
		int userInput=0;
		System.out.println("\nWhat would you like to do today?");
		System.out.println("1. Withdrawl");
		System.out.println("2. Deposit");
		System.out.println("3. Bank Balance");
		System.out.println("4. Recipt Bank Balance");
		System.out.println("5. Exit");
		userInput=input.nextInt();
		return userInput;
	}
	public static double withdrawl(double balance) {
		// TODO Auto-generated method stub
		Scanner input=new Scanner(System.in);
		double withdrawAmt=0;
		double newBalance=0;
		System.out.println("How much money would you like to withdraw today");
		withdrawAmt=input.nextDouble();
		if(withdrawAmt<balance)
		{
			newBalance=balance-withdrawAmt;
		}
		return newBalance;
	}
	public static String validation(String acctNum,String password) {
		// TODO Auto-generated method stub
		String a="123456 mypassword 54.6";
		String b="723456 anotherpassword 900.0";
		String c="823456 bestpassword 438.32";
		String result = "error";
		if (acctNum.equals(a.substring(0, a.indexOf(" "))) && password.equals(a.substring(a.indexOf(" ")+1,a.lastIndexOf(" "))))
            return result = a.substring(a.lastIndexOf(" ") + 1);
		if (acctNum.equals(a.substring(0, b.indexOf(" "))) && password.equals(a.substring(a.indexOf(" ")+1,a.lastIndexOf(" "))))
            return result = a.substring(a.lastIndexOf(" ") + 1);
		if (acctNum.equals(a.substring(0, c.indexOf(" "))) && password.equals(a.substring(a.indexOf(" ")+1,a.lastIndexOf(" "))))
            return result = a.substring(a.lastIndexOf(" ") + 1);
		else
		{
			System.out.println("Wrong username or password");
		}
		return result;

	}
	

}
Posted
Updated 7-Nov-18 7:55am

1. It loops after I ask it to withdraw
Doesn't look like you ever return to the main menu. It will probably do the same thing if you do a deposit.

2. doesnt carry newBalance throughout the program
Where are you transferring the method results to the balance?
Java
double x=0;
double balance=Double.parseDouble(orignalBalance);
// snipped for brevity
mainMenu=mainMenuOption();
while(!(mainMenu==4))
{  if (mainMenu==1)
   {
      x=withdrawl(balance);
      System.out.println("Balance= $"+x);
   }
   else if (mainMenu==2)
   {
      x=deposit(balance);
      System.out.println("Balance= $"+x);
   }
}

3.I need an extra feature other than withdraw, deposit and display balance
1. I see no routines for menu options 3 or 4.
2. What other functions can an ATM do? I am not going to do all of your homework.

Other notes: Have you tried "validation" routine with more than the first account? All three of your if statements are looking at String a, and none at the other accounts (b,c).

It's pretty obvious that you are writing this based on the solution I gave in your other post. A thank-you, comment, or upvote would be nice.
I have to create a mini ATM code, and I can't figure out where to start?[^]
 
Share this answer
 
v2
Comments
Member 14045850 7-Nov-18 13:19pm    
First thing thanks for your idea, but this code wasn't based of your idea, and you aren't doing my homework for me if the only thing you gave me is an outline. But thanks for your help.
Skandan Vecham 7-Nov-18 13:34pm    
Note the code is in progress
Java
mainMenu=mainMenuOption();
while(!(mainMenu==4))

You get an option from the main menu, but then you start a loop that continues while the option is not 4. So if the option was 1 or 2 then that loop will continue forever. You should also use the proper expression to compare for not equal, i.e. while(mainMenu != 4) .
I would recode this to something like:
Java
do {
    mainMenu=mainMenuOption();
    
    // switch block or if statements for all values

    } while(mainMenu != 4);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900