123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- import java.util.*;
- import java.lang.*;
- /**
- * Created by leon on 2/9/18.
- */
- public class Console {
- public static double currentNumber = 0;
- public static String regexNum = "\\d+";
-
- //GIVEN TO US
- public static void print(String output, Object... args) {
- System.out.printf(output, args);
- }
- //GIVEN TO US
- public static void println(String output, Object... args) {
- print(output + "\n", args);
- }
- //GIVEN TO US
- public static String getStringInput(String prompt) {
- Scanner scanner = new Scanner(System.in);
- println(prompt);
- String userInput = scanner.nextLine();
- return userInput;
- }
- //GIVEN TO US
- public static Integer getIntInput(String prompt) {
- Scanner scanner = new Scanner(System.in);
- println(prompt);
- int userInput = scanner.nextInt();
- return userInput;
- }
- //GIVEN TO US
- public static Double getDoubleInput(String prompt) {
- Scanner scanner = new Scanner(System.in);
- println(prompt);
- double userInput = scanner.nextDouble();
- return userInput;
- }
-
- public static String display(){
- //ALWAYS DISPLAY WHATS IN YOUR CALCULATOR ( STRINGBUILDER )
- return null;
- //return display;
- }
-
- // public static double getInput(String numberString){
- // //GETS INPUT FROM PERSON ( X )
- // //String numberString = getStringInput("Please choose a number.");
- // quit(numberString);
- // try{
- // double number1 = Double.parseDouble(numberString);
- // //System.out.println(number1);
- // return number1;
- // }catch(Exception e){
- // return getInput(numberString);
- // }
- // }
- /*
- public static double invert(double number){
- //TURNS NEGATIVE TO POSITIVE OR POSITIVE TO NEGATIVE
- try{
- double invertedNumber = -number;
- System.out.println(invertedNumber);
- return invertedNumber;
- } catch(Exception e){
- return getInput();
- }
- }
- */
- public static void getInput() {
- System.out.println(currentNumber);
- String input = getStringInput("Please enter an operation or number. Enter '?' for a list of commands.");
- quit(input);
- if (input.matches(regexNum) || input.equals("pi") || input.equals("e")) {
- getNumber(input);
- } else {
- getOp(input);
- }
- }
-
- public static double getNumber(String numberString){
- if(numberString.equals("quit") || numberString.equals("reset") || numberString.equals("clear")){
- quit(numberString);
- currentNumber = 0;
- }else if(numberString.equals("pi")){
- currentNumber = Math.PI;
- }else if(numberString.equals("e")){
- currentNumber = Math.E;
- }else{
- currentNumber = Double.parseDouble(numberString);
- }
- return currentNumber;
- }
-
- public static String getOp(String operation){
- //GET OPERATION FROM PERSON
- //System.out.println(simpleOp.currentNumber);
- //String operation = getStringInput("Please choose your operation.");
- if (operation == null) {
- System.out.println("You have not provided valid input. Please enter \"?\" for a list of commands.");
- } else {
- //quit(operation);
- opSwitch.choose(operation);
- //return opSwitch.choose(operation1);
- }
- return operation;
- }
-
- public static void quit(String answer){
- if(answer.equals("quit")){
- System.exit(0);
- } else if(answer.equals("clear")) {
- System.out.print('\f');
- } else if(answer.equals("reset")){
- currentNumber = 0;
- //getNumber(Console.getStringInput("Enter Number."));
- }
- }
-
- }
|