1
\$\begingroup\$

I am new to android developement. Created calculator application. Want to improve my code quality. Will be helpfull for any advice to make the code better and more professional (overall structure, splitting onto logical units, accordance to code conventions, methods and variables names, comments etc). Here is my code:

/*
 * Created by Leonid Ustenko
 * 2015
 * All rights reserved
 * And nothing else matters :)
 * 
 * */


package com.example.lcalculator;

/**
 * Represents enums for 
 * calculator's operational
 * and functional buttons
 *
 */

public class OperationsAndFunctions {

    static enum OperEnum {
        SUM, SUBSTRACT, MULTIPLY, DIVIDE, EQUAL
    }

    static enum FuncEnum {
        RESET, BACKSPACE, SIGN, PERCENT, POINT
    }
}


/* 
 * Created by Leonid Ustenko
 * 2015
 * All rights reserved
 * And nothing else matters :)
 * 
 * */


package com.example.lcalculator;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Main calculator class.
 * Listens for buttons pressing.
 * Makes calculations.
 * Shows the calculation result on display.
 */


public class LCalculator extends Activity {

    private TextView mDisplay;
    private boolean mDisplayIsEmpty = true;
    private boolean mFirstNumberReceived;
    private boolean mNumberEntered;
    private boolean mPointEntered;
    private double sNum1;
    private double sNum2;
    private static OperationsAndFunctions.OperEnum sOperation;
    private static OperationsAndFunctions.FuncEnum sFunction;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        mDisplay = (TextView) findViewById(R.id.tvDisplay);
        mDisplay.setText("0");
    }

    /** listens for number buttons pressing 
     * and assigns numerical value for each chosen ID */

    public void num_Clicked(View view) {

        Button button = (Button) view;
        int number = 0;

        // checking button ID
        switch (button.getId()) {

        case R.id.button1:
            number = 1;
            break;

        case R.id.button2:
            number = 2;
            break;

        case R.id.button3:
            number = 3;
            break;

        case R.id.button4:
            number = 4;
            break;

        case R.id.button5:
            number = 5;
            break;

        case R.id.button6:
            number = 6;
            break;

        case R.id.button7:
            number = 7;
            break;

        case R.id.button8:
            number = 8;
            break;

        case R.id.button9:
            number = 9;
            break;

        case R.id.button0:
            if (mDisplay.getText().toString().equals("0")) {
                return;
            }
            number = 0;
            break;
        }

        // setting a new digit onto display if it is empty
        if (mDisplayIsEmpty) {

            if (!(number == 0)) {
                mDisplayIsEmpty = false;
            }
            mDisplay.setText(Integer.toString(number));

        } else {
            // appending digit if display isn't empty
            mDisplay.append(Integer.toString(number));
        }
        mNumberEntered = true;
    }


    /** Listens for operational buttons pressing 
     * and assigns enum OperationsAndFunctions.OperEnum 
     * value for each chosen ID*/

    public void op_Clicked(View view) {
        Button button = (Button) view;

        /* calculating and printing result
         * if a number has been entered before.
         * Not entering here if another operation has been pressed before) */

        if (mNumberEntered) {
            // if received first number and saved to sNum1
            if (mFirstNumberReceived) {
                sNum2 = Double.parseDouble(mDisplay.getText().toString());

                printResult(calculateResult(sNum1, sOperation, sNum2));

                sNum1 = Double.parseDouble(mDisplay.getText().toString());

            } else {
                // if not received first number - receiving it now
                sNum1 = Double.parseDouble(mDisplay.getText().toString());
                mFirstNumberReceived = true;
            }
            mNumberEntered = false;
        }

        /* checking operational buttons id
         * and assigning appropriate operations enum value */

        switch (button.getId()) {

        case R.id.buttonPlus:
            sOperation = OperationsAndFunctions.OperEnum.SUM;
            break;

        case R.id.buttonMinus:
            sOperation = OperationsAndFunctions.OperEnum.SUBSTRACT;
            break;

        case R.id.buttonMultiply:
            sOperation = OperationsAndFunctions.OperEnum.MULTIPLY;
            break;

        case R.id.buttonDivide:
            sOperation = OperationsAndFunctions.OperEnum.DIVIDE;
            break;

        case R.id.buttonEqual:
            sOperation = OperationsAndFunctions.OperEnum.EQUAL;
            break;
        }

        // next digit entered will be a new number
        mDisplayIsEmpty = true;
        mPointEntered = false;
    }


    /** listening for functional buttons pressing *
     * and assigns enum OperationsAndFunctions.FuncEnum*/

    public void func_Clicked(View view) {
        Button button = (Button) view;

        /* checking operational buttons id
         and assigning appropriate functions enum value*/
        switch (button.getId()) {

        case R.id.buttonAc:
            sFunction = OperationsAndFunctions.FuncEnum.RESET;
            break;

        case R.id.buttonC:
            sFunction = OperationsAndFunctions.FuncEnum.BACKSPACE;
            break;

        case R.id.buttonSign:
            sFunction = OperationsAndFunctions.FuncEnum.SIGN;
            break;

        case R.id.buttonPercent:
            sFunction = OperationsAndFunctions.FuncEnum.PERCENT;
            break;

        case R.id.buttonPoint:
            sFunction = OperationsAndFunctions.FuncEnum.POINT;
            break;
        }

        // implementing chosen function
        implementFunction(sFunction);
    }


    /** Implementing function according to pressed button.
     * Current functions read current display state,
     * changes it and prints on display again*/

    private void implementFunction(OperationsAndFunctions.FuncEnum function) {
        sFunction = function;

        switch (function) {

        case RESET:
            reset();
            break;

        case BACKSPACE:
            printResult(Double.parseDouble(backSpace(mDisplay.getText())
                    .toString()));
            break;

        case SIGN:
            printResult(readDisplayState() * (-1));
            break;

        case PERCENT:
            printResult(readDisplayState() / 100);
            break;

        case POINT:
            if (!mPointEntered) {
                if (!mDisplayIsEmpty) {
                    mDisplay.append(".");
                } else {
                    mDisplay.setText("0.");
                    mDisplayIsEmpty = false;
                }
                mPointEntered = true;
            }
            break;
        }
    }

    /** reseting the calculator 
     * in case of pressing AC button*/

    private void reset() {
        mDisplay.setText("0");
        sNum1 = sNum2 = 0;
        mDisplayIsEmpty = true;
        mFirstNumberReceived = false;
        mNumberEntered = false;
        mPointEntered = false;
    }

    /** deleting the last symbol on display */

    private CharSequence backSpace(CharSequence displayState) {
        CharSequence result = displayState;

        if (displayState.length() > 1) {
            result = displayState.subSequence(0, displayState.length() - 1);

        } else if (displayState.length() == 1) {
            result = "0";
        }

        return result;
    }

    /** calculates the result of operation
     * between two numbers and returns the result in double format*/

    private double calculateResult(double num1,
            OperationsAndFunctions.OperEnum operation, double num2) {

        sNum1 = num1;
        sNum2 = num2;
        sOperation = operation;

        double result = 0;

        switch (operation) {

        case SUM:
            result = num1 + num2;
            break;

        case SUBSTRACT:
            result = num1 - num2;
            break;

        case MULTIPLY:
            result = num1 * num2;
            break;

        case EQUAL:
            mDisplayIsEmpty = true;
            mNumberEntered = false;
            mFirstNumberReceived = false;

            break;

        case DIVIDE:
            // division by zero
//          if (num2 == 0) {
//              showError();
//              break;
//          }

            try {
                result = num1 / num2;

            } catch (ArithmeticException e) {
                showError();
            }

            break;
        }

        return result;
    }

    /** reads current display number */
    private double readDisplayState() {
        return Double.parseDouble(mDisplay.getText().toString());
    }

    /** prints the result onto display */
    private void printResult(Double result) {

        /* checking if the result has decimal part
        if not then prints integer value of it without point and zero(s)
        */

        if (result % 1 == 0) {
            mDisplay.setText(Integer.toString(result.intValue()));

        } else {
            mDisplay.setText(Double.toString(result));
        }
    }

    /** Prints error message (in case of division by zero) 
     * and reseting current states */

    private void showError() {
        mDisplay.setText("Error");
        sNum1 = sNum2 = 0;
        mDisplayIsEmpty = true;
        mFirstNumberReceived = false;
        mNumberEntered = false;
        mPointEntered = false;
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.