Console.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import java.util.*;
  2. import java.lang.*;
  3. /**
  4. * Created by leon on 2/9/18.
  5. */
  6. public class Console {
  7. public static void print(String output, Object... args) {
  8. System.out.printf(output, args);
  9. }
  10. public static void println(String output, Object... args) {
  11. print(output + "\n", args);
  12. }
  13. public static String getStringInput(String prompt) {
  14. Scanner scanner = new Scanner(System.in);
  15. println(prompt);
  16. String userInput = scanner.nextLine();
  17. return userInput;
  18. }
  19. public static Integer getIntInput(String prompt) {
  20. Scanner scanner = new Scanner(System.in);
  21. println(prompt);
  22. int userInput = scanner.nextInt();
  23. return userInput;
  24. }
  25. public static Double getDoubleInput(String prompt) {
  26. Scanner scanner = new Scanner(System.in);
  27. println(prompt);
  28. double userInput = scanner.nextDouble();
  29. return userInput;
  30. }
  31. public static String display(){
  32. //ALWAYS DISPLAY WHATS IN YOUR CALCULATOR ( STRINGBUILDER )
  33. return null;
  34. //return display;
  35. }
  36. public static void clear(){
  37. //CLEARS SCREEN / SET TO ZERO ??
  38. }
  39. public static double getInput(String numberString){
  40. //GETS INPUT FROM PERSON ( X )
  41. //String numberString = getStringInput("Please choose a number.");
  42. quit(numberString);
  43. try{
  44. double number1 = Double.parseDouble(numberString);
  45. //System.out.println(number1);
  46. return number1;
  47. }catch(Exception e){
  48. return getInput(numberString);
  49. }
  50. }
  51. /*
  52. public static double invert(double number){
  53. //TURNS NEGATIVE TO POSITIVE OR POSITIVE TO NEGATIVE
  54. try{
  55. double invertedNumber = -number;
  56. System.out.println(invertedNumber);
  57. return invertedNumber;
  58. } catch(Exception e){
  59. return getInput();
  60. }
  61. }
  62. */
  63. public static double getNumber(String numberString){
  64. //GETS INPUT FROM PERSON ( Y )
  65. //System.out.println(first + " " + op);
  66. //String numberString = getStringInput("Please choose a number.");
  67. quit(numberString);
  68. try{
  69. double number2 = Double.parseDouble(numberString);
  70. //System.out.println(number2);
  71. return number2;
  72. }catch(Exception e){
  73. return getNumber(numberString);
  74. }
  75. }
  76. public static String getOp(){
  77. //GET OPERATION FROM PERSON
  78. System.out.println("Current Number: " + simpleOp.currentNumber);
  79. String operation = getStringInput("Please choose your operation.");
  80. if (operation == null) {
  81. System.out.println("You have not provided valid input. Please enter \"?\" for a list of commands.");
  82. }
  83. quit(operation);
  84. opSwitch.choose(operation);
  85. //return opSwitch.choose(operation1);
  86. return operation;
  87. }
  88. public static void quit(String answer){
  89. if(answer.equals("quit") || answer.equals("exit")){
  90. System.exit(0);
  91. } else if(answer.equals("clear")) {
  92. System.out.print('\f');
  93. } else if(answer.equals("reset")){
  94. simpleOp.currentNumber = 0;
  95. System.out.println(simpleOp.currentNumber);
  96. }
  97. }
  98. }