What are helper objects in java?

后端 未结 6 411
无人共我
无人共我 2021-01-30 11:04

I come across few of the times called helper objects... can anybody elaborate what are those helper objects and why do we need them?

6条回答
  •  终归单人心
    2021-01-30 11:29

    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Scanner;
    
    public class Helpers {
    public static String getDate() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
            return dateFormat.format(new Date());
        }
    
        public static boolean isTimeABeforeTimeB(String timeA, String timeB) {
            try {
                SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm aa");
                Date dA = dateFormat.parse(timeA);
                Date dB = dateFormat.parse(timeB);
                if (dA.getTime() < dB.getTime()) {
                    return true;
                } else {
                    return false;
                }
            } catch (Exception e) {
                //
            }
            return false;
        }
    
        public static String getDateAndTimeInput(String prompt) {
            Scanner input = new Scanner(System.in);
            String ans;
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm aa");
            dateFormat.setLenient(false);
            boolean dateValid;
            do {
                System.out.print(prompt);
                ans = input.nextLine();
                ans = ans.trim();
                dateValid = true;
                try {
                    Date d = dateFormat.parse(ans);
                } catch (Exception e) {
                    dateValid = false;
                }
            } while (!dateValid);
            return ans;
        }
    
    
        public static String getStringInput(String prompt) {
            Scanner input = new Scanner(System.in);
            String ans;
    
            do {
                System.out.print(prompt);
                ans = input.nextLine();
                ans = ans.trim();
            } while (ans.length() == 0);
            return ans;
        }
    
        public static double getDoubleInput(String prompt) {
            Scanner input = new Scanner(System.in);
            double ans = 0;
            boolean inputValid;
            do {
                System.out.print(prompt);
                String s = input.nextLine();
                //Convert string input to integer
                try {
                    ans = Double.parseDouble(s);
                    inputValid = true;
                } catch (Exception e) {
                     inputValid = false;
                }
            } while (!inputValid);
            return ans;
        }
    
        public static int getIntegerInput(String prompt) {
            Scanner input = new Scanner(System.in);
            int ans = 0;
            boolean inputValid;
            do {
                System.out.print(prompt);
                String s = input.nextLine();
                // Convert string input to integer
                try {
                    ans = Integer.parseInt(s);
                    inputValid = true;
                } catch (Exception e) {
                    inputValid = false;
                }
            } while (!inputValid);
            return ans;
        }
    
        public static int getIntegerInput(String prompt, int lowerBound, int upperBound) {
            Scanner input = new Scanner(System.in);
            int ans = 0;
            boolean inputValid;
            do {
                System.out.print(prompt);
                String s = input.nextLine();
                // Convert string input to integer
                try {
                    ans = Integer.parseInt(s);
                    if (ans >= lowerBound && ans <= upperBound) {
                        inputValid = true;
                    } else {
                         inputValid = false;
                    }
                } catch (Exception e) {
                    inputValid = false;
                }
           } while (!inputValid);
           return ans;
       }
    }
    

    that is an example of of a Helper Class. It contains method which of are common use of the other classes in the project.

    Example if someone wants to enter an Integer number from a class hew ill have to type in this: String num = Helpers.getIntegerInput("input your number");

    The prompt is the output that is show to the user. Other examples to input a String, double, date and time etc.

提交回复
热议问题