我需要Java中的验证方法

时间:2019-02-20 15:33:47

标签: java arrays validation methods

我需要为学校项目制造自动取款机。 我完成了所有工作,并且一切正常,并且我对销进行了验证,因为它是字符串。因此,我的问题是如何对所有其他方法进行验证,以检查是否输入了除数字以外的其他内容,以说出错误的用户,并在该方法开始时将其返回。所有变量都以整数形式存储在数组中。

这是我的代码,请帮助我尝试了很多事情,但我无法使其工作..

public class Banka {

    static Scanner skener = new Scanner(System.in);
    static String pin[] = {"1234","2345","3456","4567","5678"};
    static String username[] = {"Mate","Nathan","John","Michelle","Angelina"};
    static int balance[] = {200,100,250,150,300};
    static boolean overdraft[] = {true,true,false,true,false};
    static int index;

    public static void main(String[] args) {
        login();
    }
    public static void login() {
        Date datum = new Date();
        System.out.println("" +datum);
        System.out.println("------------------------------");
        System.out.println("Welcome to the Illuminati Bank \n Please log in with your PIN");
        String login = skener.next();
        checkpin(login);
        for (int i = 0; i< pin.length; i++) {
            if (login.matches(pin[i])) {
                index = i;
                System.out.println("\nWelcome " + username[index]);
                Menu();
            }
        }
        System.out.println("Wrong PIN entered, please login again \n");
        login();  
    }
    public static void Menu() {
        System.out.println("Please select an option");
        System.out.println("\n 1.View Bank Statement \n 2.Deposit \n 3.Withdraw \n 4.Change Pin \n 5.Exit \n");
        int choice = skener.nextInt();

        switch (choice) {
            case 1: statement();
              break;
            case 2: deposit();
              break;
            case 3: withdraw();
              break;
            case 4: change();
              break;
            case 5: exit();
              break;
            default: System.out.println("Incorrect Choice ");
        Menu();
        } 
    }
    public static void statement() {
        switch(index) {
            case 0: case 1: case 2: case 3: case 4:
                System.out.println("" +username[index]+ ", your balance is: " +balance[index]+ "€");
                if (overdraft[index] == true) {
                    System.out.println("You are entitled to overdraft");
                }
                else {
                    System.out.println("You are NOT entitled to overdraft");
                }
                Menu();
        }
    }
    public static void deposit() {
        System.out.println("" +username[index]+ ", how much you wish to deposit?");
        int deposit = skener.nextInt();
        balance[index] = balance[index] + deposit;
        System.out.println("Thank you, you deposited " +deposit+ "€, now you have " +balance[index]+ "€ total");
        depositm();
    }
    public static void depositm(){
        System.out.println("\n 1.Deposit more \n 2.Exit to menu");
        int more = skener.nextInt();
        switch (more) {
            case 1: deposit();
            break;
            case 2: Menu();
            default: System.out.println("Wrong choice, please choose again");
            depositm();
        }
    }
    public static void withdraw() {
        System.out.println("" +username[index]+ ", how much you wish to withdraw?");
        int withdraw = skener.nextInt();
        if (overdraft[index] == true) {
            balance[index] = balance[index] - withdraw;
            System.out.println("Thank you, you withdrawed the money, now you have " +balance[index]+ "€");
            Menu();
        }
        if(overdraft[index] == false && balance[index] >= withdraw) 
        {balance[index] = balance[index] - withdraw;
        System.out.println("Thank you, you withdrawed the money, now you have " +balance[index]+ "€");
        Menu();
        }
        else {
        System.out.println("You have insufficient funds \nPlease try again");
        withdraw();
        } 
    }
    public static void change() {
        System.out.println("" +username[index]+ ", do you want to change your pin?");
        System.out.println("Press 1 to change or 2 to exit to menu");
        int change = skener.nextInt();
        switch (change) {
            case 1: System.out.println("Please enter new PIN");
            pin[index] = skener.next();
            System.out.println("You successfully changed your PIN");
            Menu();
            case 2: System.out.println("Your PIN remains unchanged");
            Menu();
            default: System.out.println("Wrong choice, please choose again");
            change();
        }
    }
    public static void exit(){
        System.out.println("Goodbye " +username[index]+ ", Illuminati Bank wish you all the best \n");
        login();
    }
    public static int checkpin(String x){
        while(!x.matches("\\d{4}")){
            System.err.println("\n Error.\n Please enter 4 digit pin.");
            login();
       }
        return 0;    
    }
}

因此,如果有人可以帮助我如何使用用户输入验证所有其他方法,那么最好使用INT。

1 个答案:

答案 0 :(得分:1)

尝试一下

String input = "xxxx";
int pin;
try{
    pin = Integer.parseInt(input);
}catch(NumberFormatException e){
    // input contains letters or symbols.
}

或者这是另一个使用Character类的人。

String input = "xxxx";
boolean allDigits = true;
for(char ch: input.toCharArray()){
    if(!Character.isDigit(ch)) {
        allDigits = false;
        break;
    }
}

if(allDigits){
    // input contains only digits.
}

编辑:回答this条评论。

您可以像这样修改您的方法,

public static void checkpin(String x) {
    if (x.length() == 4) {
        try {
            Integer.parseInt(x);
            login();
        } catch (NumberFormatException e) {
            System.err.println("\n Error.\n Invalid pin.");
        }
    } else {
        System.err.println("\n Error.\n Please enter 4 digit pin.");
    }
}

这样,仅当引脚具有4位数字且所有数字均为数字时,才调用方法login()