迷你ATM应用程序,需要知道我可以适应2维或多维阵列的位置 [英] Mini ATM APPLICATION , need to know where I can fit a 2 or multi dimensional array

查看:50
本文介绍了迷你ATM应用程序,需要知道我可以适应2维或多维阵列的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完成了我的代码并完成了所有工作,除了我必须在我的程序或二维数组中使用多维数组的部分,如果您有任何建议,请提前将其作为解决方案发布。



我的尝试:



 / * Skandan Vecham 
* 2018-11-08
*迷你银行申请
* /
import java.util。*;
公共类ATM {

public static void main(String [] args){
Scanner input = new Scanner(System.in);
//存储帐号和密码的变量
String acctNum,pin =;
//原始余额,有助于确定帐户信息是真还是假
String originalBalance =error;
//存储和提取金额的变量
double depositAmount,withdrawAmount = 0;
//主菜单选项
int mainMenu = 0;
//用于锁定用户的计数器
int counter = 3;
//用户输入帐号和循环
循环
{
System.out.println(输入帐号:);
acctNum = input.next();
System.out.println(输入引脚:);
pin = input.next();
originalBalance = validation(acctNum,pin);
counter--;
// if语句判断用户是否尝试了多次
//如果语句将用户踢出程序
if(counter == 0)
{
System.out.println(\\\
达到的最大尝试次数,请访问分支机构或以您的方式致电银行);
System.exit(0);
}
// if语句打破循环并继续使用程序,前提是给出的信息为真
else if(!(originalBalance.equals(error)))
{
休息;
}
} while(originalBalance.equals(error));
//将字符串中的余额转换为双倍进行计算
double balance = Double.parseDouble(originalBalance);
// while循环重复mainMenu多次
do
{
mainMenu = mainMenuOption();
// if语句将用户带到所需的位置
//如果语句1需要用户提取
if(mainMenu == 1)
{
//要求用户想要提取多少钱,如果提款金额是
//大于当前余额,它将不会处理请求
System.out.println(您想要提取多少钱 );
withdrawAmount = input.nextDouble();
// while循环重复进程
while(withdrawAmount> balance)
{
System.out.println(Insufficient Funds\\\
Balance = $+ balance +\再试一次);
withdrawAmount = input.nextDouble();
}
balance = withdraw(withdrawAmount,balance);
}
//打开存款方式并存入用户选择的金额
否则if(mainMenu == 2)
{
System.out.println(多少钱你要存钱吗);
depositAmount = input.nextDouble();
余额=存款(depositAmount,余额);
}
//显示余额
的方法如果(mainMenu == 3)
{
displayBalance(balance);
}
//结束程序的方法
else if(mainMenu == 4)
{
System.out.println(感谢您访问银行of Skandanavia);
休息;
}
} while(mainMenu!= 4);

}
//显示账户余额
public static void displayBalance(double balance){
// printf $%。2f \ n舍入小数
System.out.printf(账户余额= $%。2f\ n,余额);
}
// withdraw方法,其中包含withdrawAmount和余额
public static double withdraw(double withdrawAmount,double balance)
{
// printf $%。2f \\\
舍入小数
double newBalance = 0;
newBalance = balance-withdrawAmount;
System.out.printf(Balance = $%。2f \ n,newBalance);
返回newBalance;
}
//将用户输入存入其账户的存款方式
公共静态双重存款(double depositAmount,double balance)
{
double newBalance = 0;
newBalance = balance + depositAmount;
System.out.printf(Balance = $%。2f \ n,newBalance);
返回newBalance;
}


//主菜单方法,指导用户按所需方向
public static int mainMenuOption(){
Scanner input = new Scanner( System.in);
int userInput = 0;
System.out.println(\\\
你今天想做什么?);
System.out.println(1。撤回);
System.out.println(2.存款);
System.out.println(3。银行余额);
System.out.println(4.退出);
userInput = input.nextInt();
返回userInput;
}
//用户信息验证
public static字符串验证(String acctNum,String pin){
//包含帐号,帐号密码和帐户余额的数组
String accountNums [] = {1234567,2345678,3456789,456789,76589};
String accountPin [] = {1234,2345,3456,4567,5678};
double accountBal [] = {56.8,89.6,97.3,110.9,996.8};
//结果变量帮助main方法中的原始余额来决定用户输入是否为false
String result =;
//将双重账户余额转换为字符串,以便它可以返回主方法
String accountBalS =;
int counter = 0;
// for循环检查帐户信息是否匹配数组
for(int x = 0; x< 5; x ++)
{
//实际转换为double
accountBalS = Double.toString(accountBal [x]);
counter + = 1;
if((accountNums [x] .equals(acctNum))&&(accountPin [x] .equals(pin)))
{
result = accountBalS;
返回结果;
}
//如果帐号或pin无效,则此if语句将运行
else if(!(accountNums [x] .equals(acctNum)||(accountPin [x])。 equals(pin)))&& counter == 5)
{
System.out.println(帐号或pin无效再尝试);
result =error;
返回结果;
}
}
返回结果;
}
}

解决方案

+余额+\\ n再次尝试);
withdrawAmount = input.nextDouble();
}
balance = withdraw(withdrawAmount,balance);
}
//打开存款方式并存入用户选择的金额
else if(mainMenu == 2)
{
System.out.println(您要存入多少钱);
depositAmount = input.nextDouble();
balance = deposit(depositAmount,balance);
}
//显示余额的方法
else if(mainMenu == 3)
{
displayBalance(balance);
}
//结束程序的方法
else if(mainMenu == 4)
{
System.out.println(感谢您访问银行of Skandanavia);
休息;
}
} while(mainMenu!= 4);

}
//显示账户余额
public static void displayBalance(double balance){
// printf


< blockquote>%。2f \ n舍入小数
System.out.printf(账户余额=


%。2f\ n,余额);
}
// withdraw方法,其中包含withdrawAmount和余额
public static double withdraw(double withdrawAmount,double balance)
{
// printf


I'm done my code and have everything done, except for the part where i have to use a multi dimensional array in my program or two dimensional arrays, if you have any suggestions please post it as a solution thanks in advance.

What I have tried:

/*Skandan Vecham
 *2018-11-08
 * Mini Bank Application
 */
import java.util.*;
public class ATM {

    public static void main(String[] args) {
        Scanner input=new Scanner (System.in);
        //variables that store account number and pin
        String acctNum,pin="";
        // original balance which helps decide if the account information is true or false
        String originalBalance="error";
        //variables to store deposit and withdrawal amounts
        double depositAmount, withdrawAmount=0;
        // main menu option
        int mainMenu=0;
        // counter to lock user out
        int counter=3;
        // loop for user input of account number and pin
        do
        {
            System.out.println("Enter the account number:");
            acctNum=input.next();
            System.out.println("Enter the pin:");
            pin=input.next();
            originalBalance=validation(acctNum,pin);
            counter--;
            // if statements to decide if user has tried to many times
            // if statement that kicks user out of program
            if(counter==0)
            {
                System.out.println("\nMaximum amount of attempts reached\nPlease visit a branch or call the bank at your convinence");
                System.exit(0);
            }
            // if statement that breaks the loop and gets on with the program provided that the information given is true
            else if(!(originalBalance.equals("error")))
            {
                break;
            }
        }while(originalBalance.equals("error"));
        // converts balance which is in string to double for calculations
        double balance=Double.parseDouble(originalBalance);
        // do while loop to repeat the mainMenu multiple times
        do
        {
            mainMenu=mainMenuOption();
            // if statements that take user to desired place
            // if statement 1 takes user to withdrawal
            if (mainMenu==1)
            {
                //Asks how much money the user would like to withdraw, and if withdraw amount is 
                // greater than current balance it will not process the request
                System.out.println("How much money would you like to withdraw");
                withdrawAmount=input.nextDouble();    
                // while loop repeats the process
                while(withdrawAmount>balance)
                {
                    System.out.println("Insufficient Funds\nBalance= $"+balance+"\nTry again");
                    withdrawAmount=input.nextDouble();    
                }
                balance=withdraw(withdrawAmount,balance);
            }
            // opens deposit method and deposits users choice of money
            else if (mainMenu==2)
            {
                System.out.println("How much money would you like to deposit");
                depositAmount=input.nextDouble();
                balance=deposit(depositAmount,balance);
            }
            // method that displays balance
            else if(mainMenu==3)
            {
                displayBalance(balance);
            }
            // method that ends the program
            else if(mainMenu==4)
            {
                System.out.println("Thank you for visiting the Bank of Skandanavia");
                break;
            }
        }while(mainMenu!=4);
    
}
    // shows the balance of the account
    public static void displayBalance(double balance) {
        //printf $%.2f\n rounds decimals
        System.out.printf("Account Balance= $%.2f\n",balance);
    }
// withdraw method which contains  the withdrawAmount and balance
    public static double withdraw(double withdrawAmount, double balance) 
    {
        //printf $%.2f\n rounds decimals
        double newBalance=0;
        newBalance=balance-withdrawAmount;
        System.out.printf("Balance= $%.2f\n",newBalance);
        return newBalance;
    }
// deposit method that deposits userinput into their accounts
    public static double deposit(double depositAmount, double balance) 
    {
        double newBalance=0;
        newBalance=balance+depositAmount;
        System.out.printf("Balance= $ %.2f\n",newBalance);
        return newBalance;
    }


// main menu method, directs user in the desired direction    
    public static int mainMenuOption() {
        Scanner input=new Scanner(System.in);
        int userInput=0;
        System.out.println("\nWhat would you like to do today?");
        System.out.println("1. withdrawal");
        System.out.println("2. Deposit");
        System.out.println("3. Bank Balance");
        System.out.println("4. Exit");
        userInput=input.nextInt();
        return userInput;
    }
// validation of the users information
    public static String validation(String acctNum,String pin) {
        // array that holds account number, account pin, and account balance
        String accountNums[]= {"1234567","2345678","3456789","456789","76589"};
        String accountPin[]= {"1234","2345","3456","4567","5678"};
        double accountBal[]= {56.8,89.6,97.3,110.9,996.8};
        // result variable helps the original balance in the main method to decide if user input is false
        String result="";
        // to convert double account balance to string so it can return to main method
        String accountBalS="";
        int counter=0;
        // for loop to check if account information matches arrays 
        for(int x=0;x<5;x++)
        {
            // actual conversion of double to string
            accountBalS=Double.toString(accountBal[x]);
            counter+=1;
            if((accountNums[x].equals(acctNum))&&(accountPin[x].equals(pin)))
            {
                result=accountBalS;
                return result;
            }
            // if account number or pin is invalid then this if statement will run
            else if(!(accountNums[x].equals(acctNum)||(accountPin[x].equals(pin)))&&counter==5)
            {
                System.out.println("Account number or pin is invalid try again");
                result="error";
                return result;
            }
        }
        return result;
    }
}

解决方案

"+balance+"\nTry again"); withdrawAmount=input.nextDouble(); } balance=withdraw(withdrawAmount,balance); } // opens deposit method and deposits users choice of money else if (mainMenu==2) { System.out.println("How much money would you like to deposit"); depositAmount=input.nextDouble(); balance=deposit(depositAmount,balance); } // method that displays balance else if(mainMenu==3) { displayBalance(balance); } // method that ends the program else if(mainMenu==4) { System.out.println("Thank you for visiting the Bank of Skandanavia"); break; } }while(mainMenu!=4); } // shows the balance of the account public static void displayBalance(double balance) { //printf


%.2f\n rounds decimals System.out.printf("Account Balance=


%.2f\n",balance); } // withdraw method which contains the withdrawAmount and balance public static double withdraw(double withdrawAmount, double balance) { //printf


这篇关于迷你ATM应用程序,需要知道我可以适应2维或多维阵列的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆