Console.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 double currentNumber = 0;
  8. public static String regexNum = "\\d+(?:\\.\\d+)?";
  9. //GIVEN TO US
  10. public static void print(String output, Object... args) {
  11. System.out.printf(output, args);
  12. }
  13. //GIVEN TO US
  14. public static void println(String output, Object... args) {
  15. print(output + "\n", args);
  16. }
  17. //GIVEN TO US
  18. public static String getStringInput(String prompt) {
  19. Scanner scanner = new Scanner(System.in);
  20. println(prompt);
  21. String userInput = scanner.nextLine();
  22. return userInput;
  23. }
  24. //GIVEN TO US
  25. public static Integer getIntInput(String prompt) {
  26. Scanner scanner = new Scanner(System.in);
  27. println(prompt);
  28. int userInput = scanner.nextInt();
  29. return userInput;
  30. }
  31. //GIVEN TO US
  32. public static Double getDoubleInput(String prompt) {
  33. Scanner scanner = new Scanner(System.in);
  34. println(prompt);
  35. double userInput = scanner.nextDouble();
  36. return userInput;
  37. }
  38. public static String display(){
  39. //ALWAYS DISPLAY WHATS IN YOUR CALCULATOR ( STRINGBUILDER )
  40. return null;
  41. //return display;
  42. }
  43. // public static double getInput(String numberString){
  44. // //GETS INPUT FROM PERSON ( X )
  45. // //String numberString = getStringInput("Please choose a number.");
  46. // quit(numberString);
  47. // try{
  48. // double number1 = Double.parseDouble(numberString);
  49. // //System.out.println(number1);
  50. // return number1;
  51. // }catch(Exception e){
  52. // return getInput(numberString);
  53. // }
  54. // }
  55. /*
  56. public static double invert(double number){
  57. //TURNS NEGATIVE TO POSITIVE OR POSITIVE TO NEGATIVE
  58. try{
  59. double invertedNumber = -number;
  60. System.out.println(invertedNumber);
  61. return invertedNumber;
  62. } catch(Exception e){
  63. return getInput();
  64. }
  65. }
  66. */
  67. public static void getInput() {
  68. System.out.println(currentNumber);
  69. String input = getStringInput("Please enter an operation or number. Enter '?' for a list of commands.");
  70. quit(input);
  71. if (input.matches(regexNum) || input.charAt(0) == '.' || input.equals("pi") || input.equals("e")) {
  72. getNumber(input);
  73. } else {
  74. getOp(input);
  75. }
  76. }
  77. public static double getNumber(String numberString){
  78. if(numberString.equals("quit") || numberString.equals("reset") || numberString.equals("clear")){
  79. quit(numberString);
  80. currentNumber = 0;
  81. }else if(numberString.equals("pi")){
  82. currentNumber = Math.PI;
  83. }else if(numberString.equals("e")){
  84. currentNumber = Math.E;
  85. }else{
  86. currentNumber = Double.parseDouble(numberString);
  87. }
  88. return currentNumber;
  89. }
  90. public static String getOp(String operation){
  91. //GET OPERATION FROM PERSON
  92. //System.out.println(simpleOp.currentNumber);
  93. //String operation = getStringInput("Please choose your operation.");
  94. if (operation == null) {
  95. System.out.println("You have not provided valid input. Please enter \"?\" for a list of commands.");
  96. } else {
  97. //quit(operation);
  98. opSwitch.choose(operation);
  99. //return opSwitch.choose(operation1);
  100. }
  101. return operation;
  102. }
  103. public static void quit(String answer){
  104. if(answer.equals("quit")){
  105. System.exit(0);
  106. } else if(answer.equals("clear")) {
  107. System.out.print('\f');
  108. } else if(answer.equals("reset")){
  109. currentNumber = 0;
  110. //getNumber(Console.getStringInput("Enter Number."));
  111. }
  112. }
  113. }