Member 14045850 Ответов: 2

Мини-приложение ATM , нужно знать, где я могу разместить 2 или многомерный массив


Я закончил свой код и сделал все, за исключением той части, где я должен использовать многомерный массив в своей программе или двумерные массивы, если у вас есть какие-либо предложения, пожалуйста, опубликуйте их в качестве решения заранее спасибо.

Что я уже пробовал:

/*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;
    }
}

CPallini

Почему многомерные массивы? Массив объектов "account" прекрасно подошел бы.

Member 14045850

Это необходимо для моего задания

CPallini

Ваше назначение-фикция.
В любом случае, я понимаю, вы должны подчиниться.

2 Ответов

Рейтинг:
2
Рейтинг:
1

MadMyche

Похоже, вам придется провести некоторый рефакторинг для ваших методов учета...

String AccountInfo[][] = {
  {"1234567","1234", "56.8" },
  {"2345678", ""2345", "89.6"},
  {"3456789", "3456", "97.3"},
  {"456789", "4567", "110.9"},
  {"76589", "5678", "996.8"}
}


Member 14045850

Спасибо, (предполагается, что это будет 5-й старт)

CPallini

Похоже, у тебя слишком много измерений.

MadMyche

да я так и делаю

Member 14045850

Эй, ребята, большое спасибо за вашу помощь, я только что сдал задание, и я думаю, что сделаю прти хорошо.