123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
-
- import java.util.Scanner;
- /**
- * Created by leon on 2/9/18.
- *
- * Expanded by Allison & Alfredo on 5/25/2018
- *
- * The Console displays messages and fetches calculations for the user
- * while they are using the calculator. It uses the Calculator class to
- * make calculations, and the NumericInputParser to correctly read user
- * input and format calculations
- *
- */
- public class Console {
-
- private Calculator calc;
- private NumericInputParser parse;
-
- public Console(){
- calc = new Calculator();
- parse = new NumericInputParser();
- }
-
- public void runCommandLoop()
- {
- Scanner input = new Scanner(System.in);
- println("Welcome to this calculator!");
- println("For a user guide, enter '?'");
-
- while (true) {
- print("Enter a command: ");
- String command = input.nextLine();
-
- if (command.equalsIgnoreCase("exit")) {
- print("Thanks for calculating with us!");
- break;
- }
- else if (command.equalsIgnoreCase("clear")){
- calc.clear();
- printResult();
- }
- else if (command.equalsIgnoreCase("val") ||
- command.equalsIgnoreCase("=")){
- printResult();
- }
- else if (command.equalsIgnoreCase("set")) {
- calc.setDisplayValue(getDoubleInput("Enter new value: "));
- printResult();
- }
- else if (command.equalsIgnoreCase("?")){
- printGuide();
- }
-
- //Core Features
-
- else if (command.equalsIgnoreCase("add") ||
- command.equalsIgnoreCase("+")) {
- printValue();
- Double d = getDoubleInput(" + ");
- if (d != null) {
- calc.add(d);
- printResult();
- }
- }
- else if (command.equalsIgnoreCase("sub") ||
- command.equalsIgnoreCase("-")) {
- printValue();
- Double d = getDoubleInput(" - ");
- if (d != null) {
- calc.subtract(d);
- printResult();
- }
-
- }
- else if (command.equalsIgnoreCase("mult") ||
- command.equalsIgnoreCase("*")) {
- printValue();
- Double d = getDoubleInput(" * ");
- if (d != null) {
- calc.multiply(d);
- printResult();
- }
- }
- else if (command.equalsIgnoreCase("div") ||
- command.equalsIgnoreCase("/")) {
- printValue();
- Double d = getDoubleInput(" / ");
- if (d != null) {
- calc.divide(d);
- printResult();
- }
- }
- else if (command.equalsIgnoreCase("sq")) {
- println(getValue() + "^2");
- calc.square();
- printResult();
- }
- else if (command.equalsIgnoreCase("pow") ||
- command.equalsIgnoreCase("^")) {
- printValue();
- calc.pow(getDoubleInput("^ "));
- printResult();
- }
- else if (command.equalsIgnoreCase("sqrt")) {
- println("\u221A" + getValue()); // unicode is for radical symbol
- calc.squareRoot();
- printResult();
- }
- else if (command.equalsIgnoreCase("inv")) {
- println("1\\" + getValue());
- calc.inverse();
- printResult();
- }
- else if (command.equalsIgnoreCase("chngsign")) {
- calc.changeSign();
- printResult();
- }
-
- //trig functions
- else if (command.equalsIgnoreCase("sin")) {
- Double d = getAngleInput("sin() ");
- if (d != null) {
- calc.sin(d);
- printResult();
- }
- }
- else if (command.equalsIgnoreCase("cos")) {
- Double d = getAngleInput("cos() ");
- if (d != null) {
- calc.cos(d);
- printResult();
- }
- }
- else if (command.equalsIgnoreCase("tan")) {
- Double d = getAngleInput("tan() ");
- if (d != null) {
- calc.tan(d);
- printResult();
- }
- }
- else if (command.equalsIgnoreCase("cot")) {
- Double d = getDoubleInput("cot() ");
- if (d != null) {
- calc.inverseTan(d);
- printTrigResult();
- }
- }
- else if (command.equalsIgnoreCase("csc")){
- Double d = getDoubleInput("csc() ");
- if (d != null) {
- calc.inverseSin(d);
- printTrigResult();
- }
- }
- else if (command.equalsIgnoreCase("sec")){
- Double d = getDoubleInput("sec() ");
- if (d != null) {
- calc.inverseCos(d);
- printTrigResult();
- }
- }
-
- //Bonus Functions
- else if (command.equalsIgnoreCase("log")){
- println("log " + getValue());
- calc.log();
- printResult();
- }
- else if (command.equalsIgnoreCase("ln")){
- println("ln ", getValue());
- calc.naturalLog();
- printResult();
- }
- else if (command.equalsIgnoreCase("antilog")){
- println("antilog " + getValue());
- calc.inverseLog();
- printResult();
- }
- else if (command.equalsIgnoreCase("antiln")){
- print("antiln " + getValue());
- calc.inverseLn();
- printResult();
- }
-
- //Custom functions
- else if (command.equalsIgnoreCase("pi")){
- calc.displayPi();
- printResult();
- }
- else if (command.equalsIgnoreCase("e")){
- calc.displayE();
- printResult();
- }
- else if (command.equalsIgnoreCase("fact") ||
- command.equalsIgnoreCase("!")){
- calc.factorial();
- printResult();
- }
- //display mode changes
- else if (command.equalsIgnoreCase("hex") ||
- command.equalsIgnoreCase("oct") ||
- command.equalsIgnoreCase("dec") ||
- command.equalsIgnoreCase("bin") ){
- parse.switchDisplayMode(command);
- println("Display mode changed to %s", parse.getDisplayMode());
- }
- else if (command.equalsIgnoreCase("mode?")) {
- println("Current display mode is %s", parse.getDisplayMode());
- }
- else if (command.equalsIgnoreCase("mode")) {
- parse.switchDisplayMode();
- println("Display mode changed to %s", parse.getDisplayMode());
- }
-
- //trig units mode changes
- else if (command.equalsIgnoreCase("rad") ||
- command.equalsIgnoreCase("deg")) {
- parse.switchUnitsMode(command);
- println("Trig units changed to %s", parse.getTrigUnitsMode());
- }
- else if (command.equalsIgnoreCase("trig")) {
- parse.switchUnitsMode();
- println("Trig units changed to %s", parse.getTrigUnitsMode());
- }
- else if (command.equalsIgnoreCase("trig?")) {
- println("Current trig units: %s", parse.getTrigUnitsMode());
- }
-
- //memory features
- else if (command.equalsIgnoreCase("M+")) {
- calc.memSet();
- printMemory();
- }
- else if (command.equalsIgnoreCase("MC")) {
- calc.memClear();
- printMemory();
- }
- else if (command.equalsIgnoreCase("MRC")) {
- calc.mrc();
- printResult();
- }
-
- else {
- System.out.println("Invalid input. Enter '?' for a user guide");
- System.out.println("Enter 'exit' to quit");
- }
-
- }
- }
-
- public static void print(String output, Object... args) {
- System.out.printf(output, args);
- }
-
- public static void println(String output, Object... args) {
- print(output + "\n", args);
- }
-
- public void printResult() {
- if (!errCheck()) {
- println("= %s", parse.getFormattedNumber(calc.getDisplayValue()));
- }
- else {
- errMessage();
- }
- }
-
- private boolean errCheck(){
- boolean err = false;
- if (Double.isInfinite(calc.getDisplayValue())){
- err = true;
- }
- else if (Double.isNaN(calc.getDisplayValue())){
- err = true;
- }
- return err;
- }
-
- private void errMessage(){
- while(true){
- String clear = getStringInput("ERR ");
- if (clear.equalsIgnoreCase("clear")){
- calc.clear();
- calc.memClear();
- break;
- }
- }
- }
-
- public void printTrigResult() {
- if (!errCheck()) {
- println("= %s", parse.getFormattedAngle(calc.getDisplayValue()));
- }
- else {
- errMessage();
- }
- }
-
- public void printMemory() {
- println("mem = %s", parse.getFormattedNumber(calc.getMem()));
- }
-
- public void printValue() {
- print(parse.getFormattedNumber(calc.getDisplayValue()));
- }
-
- public String getValue() {
- return parse.getFormattedNumber(calc.getDisplayValue());
- }
-
- public String getStringInput(String prompt) {
- Scanner s = new Scanner(System.in);
- print(prompt);
- return s.nextLine();
- }
-
- public Double getDoubleInput(String prompt) {
- Scanner s = new Scanner(System.in);
- print(prompt);
- Double d = null;
- try {
- d = parse.getDouble(s.nextLine());
- }
- catch (NumberFormatException e) {
- errMessage();
- }
- return d;
- }
-
- public double getAngleInput(String prompt) {
- Scanner s = new Scanner(System.in);
- print(prompt);
- Double d = null;
- try {
- d = parse.getAngle(s.nextLine());
- }
- catch (NumberFormatException e) {
- errMessage();
- }
- return d;
- }
-
- public void printGuide(){
- println("Guide Under Construction");
- println("%-10s | %s", "Command: ", "Function: ");
- println("%-10s | %s", "?", "Prints the user manual to the screen");
- println("%-10s | %s", "exit", "Closes the calculator");
- println("%-10s | %s", "set", "Prompts the user to input a number to set to the display");
- println("%-10s | %s", "val or =", "Print the current value to the display");
- println("%-10s | %s", "add or +", "Adds an input value to the current value");
- println("%-10s | %s", "sub or -", "Subtracts an input value from the current value");
- println("%-10s | %s", "mult or *", "Multiplies the current value by an input value");
- println("%-10s | %s", "div or /", "Divides the current value by an input value");
- println("%-10s | %s", "sq", "Squares the current value");
- println("%-10s | %s", "pow or ^", "Raises the current value to an input power");
- println("%-10s | %s", "inv", "Calculates the inverse of the current value");
- println("%-10s | %s", "chngsign", "Changes the sign of the current value");
- println("%-10s | %s", "sin", "Calculates the sine of an input angle (Decimal mode reccommended)");
- println("%-10s | %s", "cos", "Calculates the cosine of an input angle (Decimal mode reccommended)");
- println("%-10s | %s", "tan", "Calculates the tangent of an input angle (Decimal mode reccommended)");
- println("%-10s | %s", "cot", "Calculates of the cotangent (inverse tangent) of an input angle (Decimal mode reccommended)");
- println("%-10s | %s", "csc", "Calculates the cosecant (inverse sine) of an input angle (Decimal mode reccommended)");
- println("%-10s | %s", "sec", "Calculates the secant (inverse cosine) of an input angle (Decimal mode reccommended)");
- println("%-10s | %s", "log", "Calculates the logarithm (base 10) of the current value");
- println("%-10s | %s", "ln", "Calculates the natural logarithm of the current value");
- println("%-10s | %s", "antilog", "Calculates the inverse logarithm of the current value");
- println("%-10s | %s", "antiln", "Calculates the inverse natural logarithm of the current value");
- println("%-10s | %s", "fact or !", "Calculates the factorial of the current value. If value is not an integer, it is truncated before caluclation");
- println("%-10s | %s", "pi", "Sets the current value to pi");
- println("%-10s | %s", "e", "Sets the current values to e");
- println("%-10s | %s", "hex", "Sets the display mode to hexadecimal. Affects both input and output values.");
- println("%-10s | %s", "oct", "Sets the display mode to octal. Affects both input and output values.");
- println("%-10s | %s", "bin", "Sets the display mode to binary. Affects both input and output values.");
- println("%-10s | %s", "dec", "Sets the display mode to decimal. Affects both input and output values (Reccommended setting for trig functions)");
- println("%-10s | %s", "mode", "Cyles through display modes. Affects both input and ouput values");
- println("%-10s | %s", "mode?", "Prints the current display mode");
- println("%-10s | %s", "rad", "Sets the trig units mode to radians");
- println("%-10s | %s", "deg", "Sets the trig units mode to degrees");
- println("%-10s | %s", "trig", "Toggles the trig units mode");
- println("%-10s | %s", "trig?", "Prints the current trig units mode");
- println("%-10s | %s", "M+", "Sets the memory value to the current value");
- println("%-10s | %s", "MC", "Clears the memory value");
- println("%-10s | %s", "MRC", "Sets the current value to the memory value");
-
- }
- }
|