HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Java console calculator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
consolecalculatorjava

Problem

```
package calc;

import com.sun.tools.corba.se.idl.constExpr.Equal; //imports .equals(variable)
import java.util.Scanner; //imports scanners

public class Calc {

public static void main(String[] args) {
boolean go = true; //sets up loop

while(go) //creates loop to top
{
System.out.println("Hello this is my calculator!");
System.out.println("To add, type a, to subtract, type s.");
System.out.println("To multiply, type m, to divide, type d.");

Scanner scan = new Scanner(System.in); //sets up scanners
Scanner scan1 = new Scanner(System.in);

String action = scan.nextLine(); //tells comp. to take user input

if("a".equals(action)) //addition
{
System.out.println("Now type in the first number you would like to add.");
int add1 = scan.nextInt();
System.out.println("Now type the second number.");
int add2 = scan.nextInt();
int add3 = add1 + add2;
System.out.println(add1 + " added to " + add2 + " equals " + add3 + "!");
}
if("s".equals(action)) //subtraction
{
System.out.println("Now type in the first number you would like to subtract.");
int sub1 = scan.nextInt();
System.out.println("Now type the second number.");
int sub2 = scan.nextInt();
int sub3 = sub1 - sub2;
System.out.println(sub1 + " subtracted bye " + sub2 + " equals " + sub3 + "!");
}
if("m".equals(action)) //multiplacation
{
System.out.println("Now type in the first number you would like to multiply.");
int mul1 = scan.nextInt();
System.out.println("Now type the second number.");
int mul2 = scan.nextInt();
int

Solution

You do "the same" for all four choices - time to abstract!

import java.util.Scanner;    //imports scanners

public class Calc {

    enum Op {

        ADD("a", "add", "added to"),
        SUB("s", "subtract", "subtracted by"),
        MUL("m", "multiply", "multiplied by"),
        DIV("d", "divide", "divided by");
        public String key;
        public String command;
        public String result;

        Op(String key, String command, String result) {
            this.key = key;
            this.command = command;
            this.result = result;
        }

        public int eval(int x, int y) {
            switch (this) {
                case ADD:
                    return x + y;
                case SUB:
                    return x - y;
                case MUL:
                    return x * y;
                case DIV:
                    return x / y;
                default:
                    throw new AssertionError();
            }
        }
    }

    public static void main(String[] args) {
        while (true) //creates loop to top
        {
            System.out.println("Hello this is my calculator!");
            System.out.println("To add, type a, to subtract, type s.");
            System.out.println("To multiply, type m, to divide, type d.");

            Scanner scan = new Scanner(System.in);    //sets up scanners
            Scanner scan1 = new Scanner(System.in);

            String action = scan.nextLine();    //tells comp. to take user input

            Op op = null;
            for (Op operation : Op.values()) {
                if (operation.key.equals(action)) {
                    op = operation;
                    break;
                }
            }

            if (op != null) {
                System.out.println("Now type in the first number you would like to " + op.command + ".");
                int x = scan.nextInt();
                System.out.println("Now type the second number.");
                int y = scan.nextInt();
                int z = op.eval(x, y);
                System.out.println(x + " " + op.result + " " + x + " equals " + z + "!");
            }

            System.out.println("Would you like to start over? (yes,no)");
            String startOver = scan1.nextLine();

            if ("no".equals(startOver)) {
                System.out.println("Bye");
                return;
            }
        }
    }
}


So all operation specific information is captured in the Op enums. If you want to add other operations like min, max, and, or, xor or mod, you don't have to introduce new if-blocks, just new enums.

Code Snippets

import java.util.Scanner;    //imports scanners

public class Calc {

    enum Op {

        ADD("a", "add", "added to"),
        SUB("s", "subtract", "subtracted by"),
        MUL("m", "multiply", "multiplied by"),
        DIV("d", "divide", "divided by");
        public String key;
        public String command;
        public String result;

        Op(String key, String command, String result) {
            this.key = key;
            this.command = command;
            this.result = result;
        }

        public int eval(int x, int y) {
            switch (this) {
                case ADD:
                    return x + y;
                case SUB:
                    return x - y;
                case MUL:
                    return x * y;
                case DIV:
                    return x / y;
                default:
                    throw new AssertionError();
            }
        }
    }

    public static void main(String[] args) {
        while (true) //creates loop to top
        {
            System.out.println("Hello this is my calculator!");
            System.out.println("To add, type a, to subtract, type s.");
            System.out.println("To multiply, type m, to divide, type d.");

            Scanner scan = new Scanner(System.in);    //sets up scanners
            Scanner scan1 = new Scanner(System.in);


            String action = scan.nextLine();    //tells comp. to take user input

            Op op = null;
            for (Op operation : Op.values()) {
                if (operation.key.equals(action)) {
                    op = operation;
                    break;
                }
            }

            if (op != null) {
                System.out.println("Now type in the first number you would like to " + op.command + ".");
                int x = scan.nextInt();
                System.out.println("Now type the second number.");
                int y = scan.nextInt();
                int z = op.eval(x, y);
                System.out.println(x + " " + op.result + " " + x + " equals " + z + "!");
            }

            System.out.println("Would you like to start over? (yes,no)");
            String startOver = scan1.nextLine();

            if ("no".equals(startOver)) {
                System.out.println("Bye");
                return;
            }
        }
    }
}

Context

StackExchange Code Review Q#6581, answer score: 4

Revisions (0)

No revisions yet.