validation

 avatar
unknown
plain_text
2 years ago
3.9 kB
7
Indexable
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package s000011;

import java.util.Scanner;

/**
 *
 * @author MSI GTX
 */
public class Validation {

    Scanner in = new Scanner(System.in);
    private static final String VALID_PHONE = "^0[1-9]\\d{8}$";

    /**
     *
     * check user input number limit
     *
     * @return
     */
    public int checkInputIntLimit() {
        //loop until user input correct
        while (true) {
            try {
                int result = Integer.parseInt(in.nextLine().trim());
                if (result < 1 || result > 4) {
                    throw new NumberFormatException();

                }
                return result;
            } catch (NumberFormatException e) {
                System.out.println("Please input number in rage [1-4]");
                System.out.print("Enter again: ");
            }
        }
    }

    /**
     *
     * check user input string
     *
     * @return
     */
    public String checkInputString() {
        //loop until user input correct
        while (true) {
            String result = in.nextLine().trim();
            if (result.equals("")) {
                System.out.println("Please do not empty!");
                System.out.print("Enter again: ");
            } else {
                return result;
            }
        }
    }

    /**
     *
     * check user input string
     *
     * @return
     */
    public String checkInputWord() {
        //loop until user input correct
        while (true) {
            String result = in.nextLine().trim();
            if (result.isEmpty()) {
                System.out.println("Not empty");
                System.out.print("Enter again: ");
            } else {
                String[] arrz = result.split("\\s+");
                result = "";
                for (String z : arrz) {
                    result = result + (z.substring(0, 1).toUpperCase() + z.substring(1));
                    result = result + " ";

                }
                return result;
            }
        }
    }

    /**
     *
     * check user input yes/ no
     *
     * @return
     */
    public boolean checkInputYN() {
        //loop until user input correct
        while (true) {
            String result = checkInputString();
            //return true if user input y/Y
            if (result.equalsIgnoreCase("Y")) {
                return true;
            }
            //return false if user input n/N
            if (result.equalsIgnoreCase("N")) {
                return false;
            }
            System.out.println("Please input y/Y or n/N.");
            System.out.print("Enter again: ");
        }
    }

    /**
     *
     * check input integer
     */
    public int checkInputInt() {
        //loop until user input correct
        while (true) {
            try {
                int result = Integer.parseInt(in.nextLine());
                return result;
            } catch (NumberFormatException e) {
                System.out.println("Please input number");
                System.out.print("Enter again: ");
            }
        }

    }

    /**
     *
     * check input phone
     *
     * @return
     */
    public String checkInputPhone() {
        //loop until user input correct
        while (true) {
            String result = checkInputString();
            if (result.matches(VALID_PHONE)) {
                return result;
            }
            System.out.println("The phone number needs to start with 0!\nAnd must have 10 digits!\n"
                    + "Example: 0123456789");
            System.out.print("Enter again: ");
        }
    }
}
Editor is loading...