1

我有一个小型 ATM 程序,我正在使用异常处理。我在我的程序开始时有一个 try catch,它将把它发送到异常处理 - 在程序的底部。

假设用户输入一个 5 位数字......例如:12345。然后它会检查号码,我让它检查用户号码是否 < 5 然后发送错误,但我想要它做的是检查是否长度 < 或 > 大于 5 然后发送错误。这是我现在的程序代码...

import java.util.ArrayList;
import java.util.Scanner;

    public class BankMain
    {
    private double availableBal =80;
    private double totalBal =100;
    private double availableBal2 =480;
    private double totalBal2 =500;
    private double availableBal3 =80;
    private double totalBal3 =100;
    ArrayList<Integer> cardNum = new ArrayList<Integer>();
    static Scanner input = new Scanner(System.in);


    private String error;           //String the error from the exception
    {
        error = "error";
    }


    public void cardNumbers(){


            Scanner cards = new Scanner(System.in);
            Scanner input = new Scanner(System.in);
            Scanner keyboard = new Scanner(System.in);





            try{


            System.out.println("Please select a 5 digit card number");

            cardNum.add(input.nextInt());
            checkNumber();


            }

            catch(invalidNumber err){

                System.out.println("Caught Error: " + err.getError());
            }

            System.out.println("Thank you! You're card number is " +cardNum);

            System.out.println("Type 'c' to go back to main menu.");
            String value = keyboard.next();

            if(value.equalsIgnoreCase("c")){
                menu();

    }

    else if (!keyboard.equals('c')){

        System.out.println("Invalid Entry!");

    }

}


    public void menu(){

    System.out.println("ATM Menu:");
    System.out.println();
    System.out.println("1 = Create Account");
    System.out.println("2 = Account Login");
    System.out.println("3 = Exit ATM");
    query();
                }

        public void startAtm()
        {
            menu();

        }



        public void drawMainMenu()
        {

            AccountMain main3 = new AccountMain();
            int selection;

            System.out.println("\nATM main menu:");
            System.out.println("1 - View account balance");
            System.out.println("2 - Withdraw funds");
            System.out.println("3 - Add funds");
            System.out.println("4 - Back to Account Menu");
            System.out.println("5 - Terminate transaction");
            System.out.print("Choice: ");
            selection =input.nextInt();

            switch(selection)
            {
            case 1:
                viewAccountInfo();
                break;
            case 2:
                withdraw();
                break;
            case 3:
                addFunds();
                break;
            case 4:
                AccountMain.selectAccount();
                break;
            case 5:
                System.out.println("Thank you for using this ATM!!! goodbye");
            }
        }

        public void viewAccountInfo()
        {
            System.out.println("Account Information:");
            System.out.println("\t--Total balance: $"+totalBal);
            System.out.println("\t--Available balance: $"+availableBal);
            drawMainMenu();
        }

        public void viewAccountInfo2()
        {
            System.out.println("Account Information:");
            System.out.println("\t--Total balance: $"+totalBal2);
            System.out.println("\t--Available balance: $"+availableBal2);
            drawMainMenu();
        }

        public void deposit(int depAmount)
        {
            System.out.println("\n***Please insert your money now...***");
            totalBal =totalBal +depAmount;
            availableBal =availableBal +depAmount;
        }

        public void checkNsf(int withdrawAmount)
        {
            if(totalBal -withdrawAmount < 0)
                System.out.println("\n***ERROR!!! Insufficient funds in you accout***");
            else
            {
                totalBal =totalBal -withdrawAmount;
                availableBal =availableBal -withdrawAmount;
                System.out.println("\n***Please take your money now...***");
                }
        }

        public void addFunds()
        {
            int addSelection;

            System.out.println("Deposit funds:");
            System.out.println("1 - $20");
            System.out.println("2 - $40");
            System.out.println("3 - $60");
            System.out.println("4 - $100");
            System.out.println("5 - Back to main menu");
            System.out.print("Choice: ");
            addSelection =input.nextInt();

            switch(addSelection)
            {
            case 1:
                deposit(20);
                drawMainMenu();
                break;
            case 2:
                deposit(40);
                drawMainMenu();
                break;
            case 3:
                deposit(60);
                drawMainMenu();
                break;
            case 4:
                deposit(100);
                drawMainMenu();
                break;
            case 5:
                drawMainMenu();
                break;
            }
        }

        public void withdraw()
        {
            int withdrawSelection;

            System.out.println("Withdraw money:");
            System.out.println("1 - $20");
            System.out.println("2 - $40");
            System.out.println("3 - $60");
            System.out.println("4 - $100");
            System.out.println("5 - Back to main menu");
            System.out.print("Choice: ");
            withdrawSelection =input.nextInt();

            switch(withdrawSelection)
            {
            case 1:
                checkNsf(20);
                drawMainMenu();
                break;
            case 2:
                checkNsf(40);
                drawMainMenu();
                break;
            case 3:
                checkNsf(60);
                drawMainMenu();
                break;
            case 4:
                checkNsf(100);
                drawMainMenu();
                break;
            case 5:
                drawMainMenu();
                break;
            }
        }

public void query(){

    Scanner keyboard = new Scanner(System.in);
    double input = keyboard.nextInt();  


    if (input == 2){

        BankMainPart2 main2 = new BankMainPart2();

        System.out.println("Please enter your 5 digit card number.");
         BankMainPart2.loginCard(cardNum);



    }
    else if (input == 1){

        cardNumbers();

    }

    else if (input == 3){
        System.out.println("Thank you, have a nice day!");
        System.exit(0);


    }

}

public void checkingMenu()
{

    AccountMain main3 = new AccountMain();
    int selection;

    System.out.println("\nATM main menu:");
    System.out.println("1 - View account balance");
    System.out.println("2 - Withdraw funds");
    System.out.println("3 - Add funds");
    System.out.println("4 - Back to Account Menu");
    System.out.println("5 - Terminate transaction");
    System.out.print("Choice: ");
    selection =input.nextInt();

    switch(selection)
    {
    case 1:
        viewAccountInfo2();
        break;
    case 2:
        withdraw();
        break;
    case 3:
        addFunds();
        break;
    case 4:
        AccountMain.selectAccount();
        break;
    case 5:
        System.out.println("Thank you for using this ATM!!! goodbye");
    }
}

private static void checkNumber() throws invalidNumber              //run the check activation exception
{
    if(input.nextInt() == 5)
  {
        System.out.println("Works!");
  }
    else{

     throw new invalidNumber("invalid number");
 }  
}

    public static void main(String args[])
    {
        BankMain myAtm = new BankMain();
        myAtm.startAtm();
    }
}
4

4 回答 4

1

将数字转换为字符串,然后用于length()检查位数

于 2012-09-24T05:08:47.317 回答
1
        System.out.println("Please select a 5 digit card number");

        String s=input.next();

        //Here check the nu. digits by using length()

        cardNum.add(s);

        checkNumber();
于 2012-09-24T05:20:04.567 回答
1

事实上,你的程序应该挂起(你没有在你的问题中指定这个,所以我的假设可能不正确)。您调用input.nextInt()了两次,这将使您的程序期望您的用户提供两组数字。

为了解决这个问题,我建议您首先使用该nextLine()方法将用户的输入作为字符串读取。然后,您将执行以下操作:

String userInput = input.nextLine().trim();
checkNumber(userInput);
cardNum.add(Integer.parseInt(userInput);

此外,checkNumber由于我们现在正在阅读整行,因此将失败,因此您必须进行以下更改:

  • 您将不得不遍历您的字符并使用isDigit来确保您的每个字符都是数字

  • 使用排序的正则表达式^\\d{5}$以确保字符串完全由 5 个数字组成。对于正则表达式,您可以查看教程以使您走上正轨。如果您是初学者,这可能看起来有点不知所措,但通常建议使用正则表达式对字符串进行数据验证。

于 2012-09-24T05:13:36.550 回答
0

您需要获取整个代码,然后检查它的长度是否 == 5,您需要使用以下方法将输入转换为字符串:

String.valueOf(int i)

或者

Integer.toString(int i)

或者只是使用以下命令将其读取为字符串:

string givenCode = yourScan.next();

然后检查长度:

if (givenCode.length() == 5)
于 2012-09-24T05:13:15.270 回答