validation
unknown
plain_text
2 years ago
3.9 kB
2
Indexable
Never
/* * 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 s000013; import java.util.ArrayList; import java.util.Scanner; /** * * @author THAYCACAC */ public class Validate { private final static Scanner in = new Scanner(System.in); //check user input number limit /** * * @return */ public int checkInputAge() { //loop until user input correct while (true) { try { int result = Integer.parseInt(in.nextLine().trim()); if (result < 18 || result > 50) { throw new NumberFormatException(); } return result; } catch (NumberFormatException e) { System.err.println("Please input number in rage [18-50]"); System.out.print("Enter again: "); } } } public int checkInputIntLimit() { //loop until user input correct while (true) { try { int result = Integer.parseInt(in.nextLine().trim()); if (result < 1 || result > 5) { throw new NumberFormatException(); } return result; } catch (NumberFormatException e) { System.err.println("Please input number in rage [1-5]"); 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.isEmpty()) { System.err.println("Not empty"); System.out.print("Enter again: "); } else { return result; } } } // check id must be existed in DB. /** * * @param lw * @param id * @return */ public boolean checkIdExist(ArrayList<Worker> lw, String id) { //check from first to last list id worker exist or not for (Worker worker : lw) { if (id.equalsIgnoreCase(worker.getId())) { return true; } } return false; } //check salary must be greater than 0 /** * * @return */ public int checkInputSalary() { //loop until user input correct while (true) { try { int result = Integer.parseInt(in.nextLine().trim()); if (result < 0) { throw new NumberFormatException(); } return result; } catch (NumberFormatException e) { System.err.println("Salary must be greater than 0"); System.out.print("Enter again: "); } } } //check worker duplicate /** * * @param lw * @param id * @param name * @param age * @param salary * @param workLocation * @return */ public boolean checkWorkerExist(ArrayList<Worker> lw, String id, String name, int age, int salary, String workLocation) { //check from first to last list worker worker exist or not for (Worker worker : lw) { if (id.equalsIgnoreCase(worker.getId()) && name.equalsIgnoreCase(worker.getName()) && age == worker.getAge() && salary == worker.getSalary()) { return false; } } return true; } }