6
\$\begingroup\$

I was looking for a review on my coding. Since I'm planning to get a job as a Android Programmer in the long run.

This is my first calculator app, which I did by self learning Programming. I have no other Programming Langauge experience.

So I would love to hear how bad my coding is? Which areas I need improving on? What I should have used instead of repeating my self, which is bad coding as I understand.

private Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btnZero, btnDoubleZero, btnEqual, btnMultiply, btnDevide,
        btnPlus, btnMinus, btnClean, btnPoint;
private TextView textView1, totalSum;
private String lastInputNumber = "";
private double num1 = 0;
private double num2 = 0;
private double tempCount = 0;
private double tempSum = 0;

private int myClickCount = 0;

private boolean nSwitch = true;
private boolean equalSwitch = false;

String stringMinus = " - ";

String[] operation = {
    };
String[] lastNumber = {
    };

ArrayList<String> op = new ArrayList<String>();
ArrayList<String> ln = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn1 = (Button) findViewById(R.id.button3);
    btn2 = (Button) findViewById(R.id.button7);
    btn3 = (Button) findViewById(R.id.button11);
    btn4 = (Button) findViewById(R.id.button2);
    btn5 = (Button) findViewById(R.id.button6);
    btn6 = (Button) findViewById(R.id.button10);
    btn7 = (Button) findViewById(R.id.button1);
    btn8 = (Button) findViewById(R.id.button5);
    btn9 = (Button) findViewById(R.id.button9);
    btnZero = (Button) findViewById(R.id.button4);
    btnDoubleZero = (Button) findViewById(R.id.button8);
    btnEqual = (Button) findViewById(R.id.button20);
    btnMultiply = (Button) findViewById(R.id.button14);
    btnDevide = (Button) findViewById(R.id.button13);
    btnPlus = (Button) findViewById(R.id.button16);
    btnMinus = (Button) findViewById(R.id.button15);
    btnClean = (Button) findViewById(R.id.button19);
    btnPoint = (Button) findViewById(R.id.button12);

    btn1.setOnClickListener(this);
    btn2.setOnClickListener(this);
    btn3.setOnClickListener(this);
    btn4.setOnClickListener(this);
    btn5.setOnClickListener(this);
    btn6.setOnClickListener(this);
    btn7.setOnClickListener(this);
    btn8.setOnClickListener(this);
    btn9.setOnClickListener(this);
    btnZero.setOnClickListener(this);
    btnDoubleZero.setOnClickListener(this);
    btnEqual.setOnClickListener(this);
    btnMultiply.setOnClickListener(this);
    btnDevide.setOnClickListener(this);
    btnPlus.setOnClickListener(this);
    btnMinus.setOnClickListener(this);
    btnClean.setOnClickListener(this);
    btnPoint.setOnClickListener(this);

    textView1 = (TextView) findViewById(R.id.textView1);
    totalSum = (TextView) findViewById(R.id.textView3);

    Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Light.ttf");
    textView1.setTypeface(tf);
    totalSum.setTypeface(tf);

    Typeface btnTf = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Light.ttf");

    btnMultiply.setTypeface(btnTf);
    btnDevide.setTypeface(btnTf);

    btnEqual.setTypeface(btnTf);

    btnClean.setTypeface(btnTf);
    btnMinus.setTypeface(btnTf);
    btnPlus.setTypeface(btnTf);
    btnZero.setTypeface(btnTf);
    btnDoubleZero.setTypeface(btnTf);
    btn1.setTypeface(btnTf);
    btn2.setTypeface(btnTf);
    btn3.setTypeface(btnTf);
    btn4.setTypeface(btnTf);
    btn5.setTypeface(btnTf);
    btn6.setTypeface(btnTf);
    btn7.setTypeface(btnTf);
    btn8.setTypeface(btnTf);
    btn9.setTypeface(btnTf);

    // makes the textviews scrollable
    textView1.setMovementMethod(new ScrollingMovementMethod());
    totalSum.setMovementMethod(new ScrollingMovementMethod());
}


@Override
public void onClick(View v) {

    switch (v.getId()) {
    case R.id.button3:
        // log that you clicked one
        Log.d("OSSI", "button 1 was clicked");
        // First if switch is to check of equal was used
        if (equalSwitch == true) {

        } else {
            // add one to the existing textView1 string
            textView1.setText(textView1.getText().toString() + "1");

            // my click counter plus one
            myClickCount++;
            // Bollean switch so operators cant be called twice
            nSwitch = false;
        }

      break;
    case R.id.button7:
        // log that you clicked two
        Log.d("OSSI", "button 2 was clicked");
        // First if switch is to check of equal was used
        if (equalSwitch == true) {

        } else {
            // add two to the existing textView1 string
            textView1.setText(textView1.getText().toString() + "2");

            // my click counter plus one
            myClickCount++;
            // Bollean switch so operators cant be called twice
            nSwitch = false;
        }

      break;
    case R.id.button11:
        // log that you clicked three
        Log.d("OSSI", "button 3 was clicked");
        // First if switch is to check of equal was used
        if (equalSwitch == true) {

        } else {
            // add three to the existing textView1 string
            textView1.setText(textView1.getText().toString() + "3");

            // my click counter plus one
            myClickCount++;
            // Bollean switch so operators cant be called twice
            nSwitch = false;
        }

      break;
    case R.id.button2:
        // log that you clicked four
        Log.d("OSSI", "button 4 was clicked");
        // First if switch is to check of equal was used
        if (equalSwitch == true) {

        } else {
            // add four to the existing textView1 string
            textView1.setText(textView1.getText().toString() + "4");

            // my click counter plus one
            myClickCount++;
            // Bollean switch so operators cant be called twice
            nSwitch = false;
        }

      break;
    case R.id.button6:
        // log that you clicked five
        Log.d("OSSI", "button 5 was clicked");
        // First if switch is to check of equal was used
        if (equalSwitch == true) {

        } else {
            // add five to the existing textView1 string
            textView1.setText(textView1.getText().toString() + "5");

            // my click counter plus one
            myClickCount++;
            // Bollean switch so operators cant be called twice
            nSwitch = false;
        }

      break;
    case R.id.button10:
        // log that you clicked six
        Log.d("OSSI", "button 6 was clicked");
        // First if switch is to check of equal was used
        if (equalSwitch == true) {

        } else {
            // add six to the existing textView1 string
            textView1.setText(textView1.getText().toString() + "6");

            // my click counter plus one
            myClickCount++;
            // Bollean switch so operators cant be called twice
            nSwitch = false;
        }

      break;
    case R.id.button1:
        // log that you clicked seven
        Log.d("OSSI", "button 7 was clicked");
        // First if switch is to check of equal was used
        if (equalSwitch == true) {

        } else {
            // add seven to the existing textView1 string
            textView1.setText(textView1.getText().toString() + "7");

            // my click counter plus one
            myClickCount++;
            // Bollean switch so operators cant be called twice
            nSwitch = false;
        }

      break;
    case R.id.button5:
        // log that you clicked eight
        Log.d("OSSI", "button 8 was clicked");
        // First if switch is to check of equal was used
        if (equalSwitch == true) {

        } else {
            // add eight to the existing textView1 string
            textView1.setText(textView1.getText().toString() + "8");

            // my click counter plus one
            myClickCount++;
            // Bollean switch so operators cant be called twice
            nSwitch = false;
        }

      break;
    case R.id.button9:
        // log that you clicked nine
        Log.d("OSSI", "button 9 was clicked");
        // First if switch is to check of equal was used
        if (equalSwitch == true) {

        } else {
            // add nine to the existing textView1 string
            textView1.setText(textView1.getText().toString() + "9");

            // my click counter plus one
            myClickCount++;
            // Bollean switch so operators cant be called twice
            nSwitch = false;
        }

      break;
    case R.id.button4:
        // log that you clicked zero
        Log.d("OSSI", "button 0 was clicked");

        // First if switch is to check of equal was used
        if (equalSwitch == true) {

        } else {
            // add zero to the existing textView1 string
            textView1.setText(textView1.getText().toString() + "0");

            // my click counter plus one
            myClickCount++;
            // Bollean switch so operators cant be called twice
            nSwitch = false;
        }

      break;
    case R.id.button8:
        // log that you clicked doublezero
        Log.d("OSSI", "button 00 was clicked");
        if (myClickCount == 0) {

        }
        else if (equalSwitch == true) {

        }
        else {
            // add doublezero to the existing textView1
            textView1.setText(textView1.getText().toString() + "00");

            // my click counter plus one
            myClickCount += 2;
            // Bollean switch so operators cant be called twice
            nSwitch = false;
        }


      break;
    case R.id.button13:
        Log.d("OSSI", "button / was clicked");
        // checks if ln arraylist has something stored before executing the math code
        if (textView1.length() < 1) {

        }   
        else if (ln.isEmpty()) {
            // Log ln array is Empty
            Log.d("OSSI", "ln is empty");
            // Getting textView1 and parse into an int
            num1 = Double.parseDouble(textView1.getText().toString());
            // Saving the int num1 in sum1 as a String
            String sum1 = Double.toString(num1);
            // Setting text from sum1 to totalSum
            totalSum.setText(sum1);
            // Adding sum1 inside ln string arraylist
            ln.add(sum1);
            // Adding devide symbol to the exsiting textView1 text
            textView1.setText(textView1.getText().toString() + " / ");
            // Adding devide to the op array list
            op.add("/");
        }
        else if (nSwitch == true) {
            // Save textView1 into lastOperator String
            String lastOperator = textView1.getText().toString();
            // Save String without the last Operator
            String deleteLastOperator = lastOperator.substring(lastOperator.length() - (lastOperator.length()),
                                                                                    lastOperator.length() - 3);
            // Saving the new String without the last Operator
            textView1.setText(deleteLastOperator);
            // Adding new operator to textView1 String
            textView1.setText(textView1.getText().toString() + " / ");
            // Adding to the op array list
            op.add("/");
            // Removes the first operator from op arraylist
            op.remove(0);
        }
        else {

            // Saving textView1 as lastNumb
            String lastNumb = textView1.getText().toString();

            // A substring of lastNumb String will be saved as lastIput
            // (lastnumb minus the click counter, till the last lastnumb)
            String lastInput = lastNumb.substring(lastNumb.length() - myClickCount, lastNumb.length());

            // Parsing lastInput to into num2 integer
            num2 = Double.parseDouble(lastInput);
            // Parsing String of totalSum into tempSum integer
            tempSum = Double.parseDouble(totalSum.getText().toString());

                if (op.get(0) == "+") {
                    Log.d("OSSI", "if plus operator worked");
                    // Calculating tempSum + sum2
                    tempSum = tempSum + num2;
                } 
                else if  (op.get(0) == "-")  {
                    Log.d("OSSI", "if minus operator worked");
                    // Calculating tempSum - sum2
                    tempSum = tempSum - num2;
                }
                else if (op.get(0) == "/") {
                    Log.d("OSSI", "if devide operator worked");
                    // Calculating tempSum / sum2
                    tempSum = tempSum / num2;
                }
                else if (op.get(0) == "*") {
                    Log.d("OSSI", "if multiply operator worked");
                    // Calculating tempSum * sum2
                    tempSum = tempSum * num2;
                }

                // Math to round 2 decimal number before tempSum will be set into totalSum  
                double mathRound = tempSum;
                double tempSum = Math.round(mathRound*100.0)/100.0;



            // Changing tempSum to a String
            String sumDevide = Double.toString(tempSum);
            // String sumDevide will be the new totalSum text
            totalSum.setText(sumDevide);



            // Adding devide symbol to the exsiting textView1 text
            textView1.setText(textView1.getText().toString() + " / ");
            // Adding to the op array list
            op.add("/");
            // Removes the first operator from op arraylist
            op.remove(0);
        }
        // my click counter reset to zero
        myClickCount = 0;
        // Equal Switch to false so numbers can be used again
        equalSwitch = false;

        nSwitch = true;
      break;
    case R.id.button14:
        Log.d("OSSI", "button * was clicked");

        if (textView1.length() < 1) {

        }
        // checks if ln arraylist has something stored before executing the math code
        else if (ln.isEmpty()) {
            // Log ln array is Empty
            Log.d("OSSI", "ln is empty");
            // Getting textView1 and parse into an int
            num1 = Double.parseDouble(textView1.getText().toString());
            // Saving the int num1 in sum1 as a String
            String sum1 = Double.toString(num1);
            // Setting text from sum1 to totalSum
            totalSum.setText(sum1);
            // Adding sum1 inside ln string arraylist
            ln.add(sum1);
            // Adding multiply symbol to the exsiting textView1 text
            textView1.setText(textView1.getText().toString() + " * ");
            // Adding multiply to the op array list
            op.add("*");
        }
        else if (nSwitch == true) {
            // Save textView1 into lastOperator String
            String lastOperator = textView1.getText().toString();
            // Save String without the last Operator
            String deleteLastOperator = lastOperator.substring(lastOperator.length() - (lastOperator.length()),
                                                                                    lastOperator.length() - 3);
            // Saving the new String without the last Operator
            textView1.setText(deleteLastOperator);
            // Adding new operator to textView1 String
            textView1.setText(textView1.getText().toString() + " * ");
            // Adding to the op array list
            op.add("*");
            // Removes the first operator from op arraylist
            op.remove(0);
        }
        else {

            // Saving textView1 as lastNumb
            String lastNumb = textView1.getText().toString();

            // A substring of lastNumb String will be saved as lastIput
            // (lastnumb minus the click counter, till the last lastnumb)
            String lastInput = lastNumb.substring(lastNumb.length() - myClickCount, lastNumb.length());

            // Parsing lastInput to into num2 integer
            num2 = Double.parseDouble(lastInput);
            // Parsing String of totalSum into tempSum integer
            tempSum = Double.parseDouble(totalSum.getText().toString());

                if (op.get(0) == "+") {
                    Log.d("OSSI", "if plus operator worked");
                    // Calculating tempSum + sum2
                    tempSum = tempSum + num2;
                } 
                else if  (op.get(0) == "-")  {
                    Log.d("OSSI", "if minus operator worked");
                    // Calculating tempSum - sum2
                    tempSum = tempSum - num2;
                }
                else if (op.get(0) == "/") {
                    Log.d("OSSI", "if devide operator worked");
                    // Calculating tempSum / sum2
                    tempSum = tempSum / num2;
                }
                else if (op.get(0) == "*") {
                    Log.d("OSSI", "if multiply operator worked");
                    // Calculating tempSum * sum2
                    tempSum = tempSum * num2;
                }

                // Math to round 2 decimal number before tempSum will be set into totalSum  
                double mathRound = tempSum;
                double tempSum = Math.round(mathRound*100.0)/100.0;

            // Changing tempSum to a String
            String sumMulitply = Double.toString(tempSum);
            // String sumMultiply will be the new totalSum text
            totalSum.setText(sumMulitply);



            // Adding multiply symbol to the exsiting textView1 text
            textView1.setText(textView1.getText().toString() + " * ");
            // Adding to the op array list
            op.add("*");
            // Removes the first operator from op arraylist
            op.remove(0);
        }
        // my click counter reset to zero
        myClickCount = 0;

        nSwitch = true;
        // Equal Switch to false so numbers can be used again
        equalSwitch = false;
      break;
    case R.id.button15:
        Log.d("OSSI", "button - was clicked");

        if (textView1.length() < 1) {

        }
        // checks if ln arraylist has something stored before executing the math code
        else if (ln.isEmpty()) {
            // Log ln array is Empty
            Log.d("OSSI", "ln is empty");
            // Getting textView1 and parse into an int
            num1 = Double.parseDouble(textView1.getText().toString());
            // Saving the int num1 in sum1 as a String
            String sum1 = Double.toString(num1);
            // Setting text from sum1 to totalSum
            totalSum.setText(sum1);
            // Adding sum1 inside ln string arraylist
            ln.add(sum1);
            // Adding minus symbol to the exsiting textView1 text
            textView1.setText(textView1.getText().toString() + stringMinus);
            // Adding minus to the op array list
            op.add("-");
        }
        else if (nSwitch == true) {
            // Save textView1 into lastOperator String
            String lastOperator = textView1.getText().toString();
            // Save String without the last Operator
            String deleteLastOperator = lastOperator.substring(lastOperator.length() - (lastOperator.length()),
                                                                                    lastOperator.length() - 3);
            // Saving the new String without the last Operator
            textView1.setText(deleteLastOperator);
            // Adding new operator to textView1 String
            textView1.setText(textView1.getText().toString() + stringMinus);
            // Adding to the op array list
            op.add("-");
            // Removes the first operator from op arraylist
            op.remove(0);
        }
        else {

                // Saving textView1 as lastNumb
                String lastNumb = textView1.getText().toString();

                // A substring of lastNumb String will be saved as lastIput
                // (lastnumb minus the click counter, till the last lastnumb)
                String lastInput = lastNumb.substring(lastNumb.length() - myClickCount, lastNumb.length());

                // Parsing lastInput to into num2 integer
                num2 = Double.parseDouble(lastInput);
                // Parsing String of totalSum into tempSum integer
                tempSum = Double.parseDouble(totalSum.getText().toString());

                if (op.get(0) == "+") {
                    Log.d("OSSI", "if plus operator worked");
                    // Calculating tempSum + sum2
                    tempSum = tempSum + num2;
                } 
                else if  (op.get(0) == "-")  {
                    Log.d("OSSI", "if minus operator worked");
                    // Calculating tempSum - sum2
                    tempSum = tempSum - num2;
                }
                else if (op.get(0) == "/") {
                    Log.d("OSSI", "if devide operator worked");
                    // Calculating tempSum / sum2
                    tempSum = tempSum / num2;
                }
                else if (op.get(0) == "*") {
                    Log.d("OSSI", "if multiply operator worked");
                    // Calculating tempSum * sum2
                    tempSum = tempSum * num2;
                }

                // Math to round 2 decimal number before tempSum will be set into totalSum  
                double mathRound = tempSum;
                double tempSum = Math.round(mathRound*100.0)/100.0;


            // Changing tempSum to a String
            String sumMinus = Double.toString(tempSum);
            // String sumMinus will be the new totalSum text
            totalSum.setText(sumMinus);



            // Adding multiply symbol to the exsiting textView1 text
            textView1.setText(textView1.getText().toString() + stringMinus);
            // Adding to the op array list
            op.add("-");
            // Removes the first operator from op arraylist
            op.remove(0);
        }
        // my click counter reset to zero
        myClickCount = 0;

        nSwitch = true;
        // Equal Switch to false so numbers can be used again
        equalSwitch = false;
      break;
    case R.id.button16:
        Log.d("OSSI", "button + was clicked");

        if (textView1.length() < 1) {

        }
        // checks if ln arraylist has something stored before executing the math code
        else if (ln.isEmpty()) {
            // Log ln array is Empty
            Log.d("OSSI", "ln is empty");
            // Getting textView1 and parse into an int
            num1 = Double.parseDouble(textView1.getText().toString());
            // Saving the int num1 in sum1 as a String
            String sum1 = Double.toString(num1);
            // Setting text from sum1 to totalSum
            totalSum.setText(sum1);
            // Adding sum1 inside ln string arraylist
            ln.add(sum1);
            // Adding plus symbol to the exsiting textView1 text
            textView1.setText(textView1.getText().toString() + " + ");
            // Adding plus to the op array list
            op.add("+");
        }
        else if (nSwitch == true) {
            // Save textView1 into lastOperator String
            String lastOperator = textView1.getText().toString();
            // Save String without the last Operator
            String deleteLastOperator = lastOperator.substring(lastOperator.length() - (lastOperator.length()),
                                                                                    lastOperator.length() - 3);
            // Saving the new String without the last Operator
            textView1.setText(deleteLastOperator);
            // Adding new operator to textView1 String
            textView1.setText(textView1.getText().toString() + " + ");
            // Adding to the op array list
            op.add("+");
            // Removes the first operator from op arraylist
            op.remove(0);
        }
        else {

            // Saving textView1 as lastNumb
            String lastNumb = textView1.getText().toString();

            // A substring of lastNumb String will be saved as lastIput
            // (lastnumb minus the click counter, till the last lastnumb)
            String lastInput = lastNumb.substring(lastNumb.length() - myClickCount, lastNumb.length());

            // Parsing lastInput to into num2 integer
            num2 = Double.parseDouble(lastInput);
            // Parsing String of totalSum into tempSum integer
            tempSum = Double.parseDouble(totalSum.getText().toString());

                if (op.get(0) == "+") {
                    Log.d("OSSI", "if plus operator worked");
                    // Calculating tempSum + sum2
                    tempSum = tempSum + num2;
                    new DecimalFormat("#.##").format(tempSum);
                } 
                else if  (op.get(0) == "-")  {
                    Log.d("OSSI", "if minus operator worked");
                    // Calculating tempSum - sum2
                    tempSum = tempSum - num2;
                }
                else if (op.get(0) == "/") {
                    Log.d("OSSI", "if devide operator worked");
                    // Calculating tempSum / sum2
                    tempSum = tempSum / num2;
                }
                else if (op.get(0) == "*") {
                    Log.d("OSSI", "if multiply operator worked");
                    // Calculating tempSum * sum2
                    tempSum = tempSum * num2;
                }

                // Math to round 2 decimal number before tempSum will be set into totalSum  
                double mathRound = tempSum;
                double tempSum = Math.round(mathRound*100.0)/100.0;

            // Changing tempSum to a String
            String sumAdition = Double.toString(tempSum);
            // String sumAdition will be the new totalSum text
            totalSum.setText(sumAdition);


            // Adding multiply symbol to the exsiting textView1 text
            textView1.setText(textView1.getText().toString() + " + ");
            // Adding to the op array list
            op.add("+");
            // Removes the first operator from op arraylist
            op.remove(0);
        }
        // my click counter reset to zero
        myClickCount = 0;

        // Equal Switch to false so numbers can be used again
        equalSwitch = false;

        nSwitch = true;
      break;

    case R.id.button20:
        Log.d("OSSI", "button = was clicked");

        if (myClickCount == 0) {

        }

        else if (ln.isEmpty()) {

        }

        else if (nSwitch == true) {

        }

        else {
            // Saving textView1 as lastNumb
            String lastNumb = textView1.getText().toString();

            // A substring of lastNumb String will be saved as lastIput
            // (lastnumb minus the click counter, till the last lastnumb)
            String lastInput = lastNumb.substring(lastNumb.length() - myClickCount, lastNumb.length());

            // Parsing lastInput to into num2 integer
            num2 = Double.parseDouble(lastInput);
            // Parsing String of totalSum into tempSum integer
            tempSum = Double.parseDouble(totalSum.getText().toString());

                if (op.get(0) == "+") {
                    Log.d("OSSI", "if plus operator worked");
                    // Calculating tempSum + sum2
                    tempSum = tempSum + num2;
                } 
                else if  (op.get(0) == "-")  {
                    Log.d("OSSI", "if minus operator worked");
                    // Calculating tempSum - sum2
                    tempSum = tempSum - num2;
                }
                else if (op.get(0) == "/") {
                    Log.d("OSSI", "if devide operator worked");
                    // Calculating tempSum / sum2
                    tempSum = tempSum / num2;
                }
                else if (op.get(0) == "*") {
                    Log.d("OSSI", "if multiply operator worked");
                    // Calculating tempSum * sum2
                    tempSum = tempSum * num2;
                }

            // Math to round 2 decimal number before tempSum will be set into totalSum  
            double mathRound = tempSum;
            double tempSum = Math.round(mathRound*100.0)/100.0;


            // Changing tempSum to a String
            String sumEqual = Double.toString(tempSum);
            // String sumMultiply will be the new totalSum text
            totalSum.setText(sumEqual);

            // Adding to the op array list
            op.add("tF");

            // Removes the first operator from op arraylist
            op.remove(0);

            // checks if equal was pressed so no number can be pressed
            equalSwitch = true;
        }

      break;

    case R.id.button12:
        Log.d("OSSI", "button Point was clicked");
        // sets the textView1 to a string
        String string = textView1.getText().toString();
        String pointChecker = string.substring(string.length() - myClickCount, string.length());

        if (equalSwitch == true) {

        }
        else if (string.length() == 0){
            // If string length is 0 no point will added
        }
        else if (myClickCount == 0) {

        }
        else if (pointChecker.contains(".")) {
            // If string contains a point, no new point will added
        }
        else {
            // Adds one point in the TextView after any given number
            textView1.setText(textView1.getText().toString() + ".");
            myClickCount++;
        }
      break;

    case R.id.button19:
        Log.d("OSSI", "button C was clicked");
        textView1.setText("");
        totalSum.setText("");

        this.num1 = 0;
        this.num2 = 0;
        this.tempCount = 0;
        this.tempSum = 0;

        this.myClickCount = 0;

        this.nSwitch = true;
        this.equalSwitch = false;

        op.removeAll(op);
        ln.removeAll(ln);

      break;



    default:
      break;
    }


}
\$\endgroup\$
1
  • \$\begingroup\$ Once you've fixed the things @rolfl suggested below, please post a follow-up question as I think there will be more stuff that can be fixed then! \$\endgroup\$ – Simon Forsberg Feb 4 '14 at 18:43
5
\$\begingroup\$

You have a large amount of code duplication. The highly technical description of your problem is 'DRY' or Don't Repeat Yourself.

I would say that fixing three issues in your code would make the rest of the code a lot simpler to manage. Once those three are done, you should bring your code back for review.

1. Resources

You have completely munged the names of the resources in your Layout.xml file. Your button resources should have ID's that match the button. Why is R.id.button3 actually Button btn1? You should re-identify all your resources and make the resource names match the use of the resource. This is a simple fix, but would have been better to get right the first time around.

2. Button Arrays

You should specify your buttons as an array of Button and you should populate it accordingly. Even better would be an enum of Button.

public enum CalculatorButton {
    ZERO(R.id.CalcZero),
    ONE(R.id.CalcOne),
    ....
    POINT(R.id.CalcPoint),
    ...
    ;


    public static final CalculatorButton getOwner(int buttonid) {
        for (CalculatorButton cb : values()) {
            if (cb.getButtonID() == button) {
                return cb;
            }
        }
        return null;
    }

    private final int calcbutton;
    private CalculatorButton(int calcbutton) {
        this.calcbutton = caclbutton;
    }

    public int getButtonID() {
        return calcbutton;
    }

}

Then, when you set up your Activity, you can loop over the enum members:

for (CalculatorButton cb : CalculatorButton.values()) {
    cb.getButton().setOnClickListener(this);
    cb.getButton().setTypeface(btnTf)
}

Similarly, there are a lot of other places where you can reduce the code footprint using the loop.

Switch on Enum

With your buttons represented as enum members, your big switch statement can become:

public void onClick(View v) {

    switch (CalculatorButton.getOwner(v.getId())) {
    case ONE:
          ....
    case POINT:
          ....

}

You should have better handling for what you do with the buttons as well, but these three changes will go a long way to making your code more readable.

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.