本文介绍了迷你ATM应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在创建一个迷你ATM应用程序,我遇到了一些问题。

1.我要求它撤销后循环

2.不携带整个计划中的newBalance

3.我需要除提款,存款和展示余额之外的额外功能



我尝试过的:



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;

	}
	

}

推荐答案



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=


这篇关于迷你ATM应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 09:07