package psJava;

import javax.swing.*;

public class KeyIn {

  public static int readInt(String prompt) {
      String num = JOptionPane.showInputDialog(prompt);
      try {
        int intNum = Integer.parseInt(num);
        return (intNum);
      } catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid integer " + num + " - try again",
                                 "Error", JOptionPane.ERROR_MESSAGE);
        return readInt(prompt);
      }
  }

  public static int readInt(String prompt, int min, int max) {
      String num = JOptionPane.showInputDialog(prompt + " (" + min + "..." + max + ")");
      try {
        int intNum = Integer.parseInt(num);
        if (intNum >= min && intNum <= max)
          return intNum;
        else if (intNum < min) {
          JOptionPane.showMessageDialog(null, intNum + " is too small - try again",
                                 "Error", JOptionPane.ERROR_MESSAGE);
          return readInt(prompt, min, max);
        } else {
          JOptionPane.showMessageDialog(null, intNum + " is too large - try again",
                                 "Error", JOptionPane.ERROR_MESSAGE);
          return readInt(prompt, min, max);
        }
      } catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid integer " + num + " - try again",
                                 "Error", JOptionPane.ERROR_MESSAGE);
        return readInt(prompt, min, max);
      }
  }

  public static double readDouble(String prompt) {
      String num = JOptionPane.showInputDialog(prompt);
      try {
        return (Double.parseDouble(num));
      } catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid real number " + num + " - try again",
                                 "Error", JOptionPane.ERROR_MESSAGE);
        return readDouble(prompt);
      }
  }

  public static double readDouble(String prompt, double min, double max) {
      String num = JOptionPane.showInputDialog(prompt + " (" + min + "..." + max + ")");
      try {
        double realNum = Double.parseDouble(num);
        if (realNum >= min && realNum <= max)
          return realNum;
        else if (realNum < min) {
          JOptionPane.showMessageDialog(null, realNum + " is too small - try again",
                                 "Error", JOptionPane.ERROR_MESSAGE);
          return readDouble(prompt, min, max);
        } else {
          JOptionPane.showMessageDialog(null, realNum + " is too large - try again",
                                 "Error", JOptionPane.ERROR_MESSAGE);
          return readDouble(prompt, min, max);
        }
      } catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid real number " + num + " - try again",
                                 "Error", JOptionPane.ERROR_MESSAGE);
        return readDouble(prompt, min, max);
      }
  }

  public static String readString(String prompt) {
    return JOptionPane.showInputDialog(prompt);
  }

  public static char readChar(String prompt) {
    String str = JOptionPane.showInputDialog(prompt);
    if (str.length() == 1)
      return str.charAt(0);
    else {
       JOptionPane.showMessageDialog(null, str + " is not a single character - try again",
                                 "Error", JOptionPane.ERROR_MESSAGE);
       return readChar(prompt);
    }
  }

  public static char readChar(String prompt, char min, char max) {
    String str = JOptionPane.showInputDialog(prompt + " (" + min + "..." + max + ")");
    if (str.length() == 1) {
      char ch = str.charAt(0);
      if (ch >= min && ch <= max)
        return ch;
      else if (ch < min) {
        JOptionPane.showMessageDialog(null, ch + " is too small - try again",
                                 "Error", JOptionPane.ERROR_MESSAGE);
        return readChar(prompt, min, max);
      } else {
        JOptionPane.showMessageDialog(null, ch + " is too large - try again",
                                 "Error", JOptionPane.ERROR_MESSAGE);
        return readChar(prompt, min, max);
      }
    } else {
       JOptionPane.showMessageDialog(null, str + " is not a single character - try again",
                                 "Error", JOptionPane.ERROR_MESSAGE);
       return readChar(prompt, min, max);
    }
  }

  public static boolean readBoolean(String query) {
    int n = JOptionPane.showConfirmDialog(null, query, "Yes/No question",
                                     JOptionPane.YES_NO_OPTION);
    return (n == 0);
  }

  public static boolean readBoolean(String query, String trueAns, String falseAns) {
    Object[] options = {trueAns, falseAns};
    int n = JOptionPane.showOptionDialog(null, query, "Select " +
               trueAns + " or " + falseAns,
               JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
               null, options, options[0]);
    return (n == 0);
  }

  public static int readChoice(String query, String[] options) {
    int n = JOptionPane.showOptionDialog(null, query, "Multiple choice question",
               JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
               null, options, options[0]);
    return n;
  }

  /*
  // main method to test class methods
  public static void main(String[] args) {
     String data = readString("Enter name ");
     System.out.println("name is " + data);
     int kids = readInt("How many kids ");
     System.out.println("number of kids is " + kids);
     kids = readInt("How many kids ", 1, 5);
     System.out.println("number of kids is " + kids);
     double sal = readDouble("Enter salary $");
     System.out.println("Salary is " + sal);
     sal = readDouble("Enter salary $", 500.0, 1000.0);
     System.out.println("Salary is " + sal);
     char ch = readChar("enter letter");
     System.out.println("letter is " + ch);
     ch = readChar("enter letter", 'a', 'z');
     System.out.println("letter is " + ch);
     boolean bool = readBoolean("Your status is single");
     System.out.println("single status is " + bool);
     bool = readBoolean("Are you single", "yes", "no");
     System.out.println("single status is " + bool);
     String[] choices = {"black", "cream only", "sugar only", "cream & sugar"};
     int coffee = readChoice("Select a coffee choice", choices);
     System.out.println("choice is " + coffee);
  }
  */
}
