aleajactatest.appiumcalculator.CalculatorActivity.java Source code

Java tutorial

Introduction

Here is the source code for aleajactatest.appiumcalculator.CalculatorActivity.java

Source

/*
Copyright 2016 Pierre-Yves Lapersonne (aka. "pylapp",  pylapp(dot)pylapp(at)gmail(dot)com)
    
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
//   / ?

package aleajactatest.appiumcalculator;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Activity embedding widgets for a VERY BASIC AND DUMB calculator application.
 * It possesses a displayed field with buttons.
 * The computing part is in this activity too.
 *
 * @author pylapp
 * @version 1.1.0
 * @since 18/12/2015
 */
@Deprecated
public final class CalculatorActivity extends FragmentActivity {

    /* ********** *
     * ATTRIBUTES *
     * ********** */

    /**
     * The score
     */
    private double mScore;
    /**
     * The first value
     */
    private int mLeftPart;
    /**
     * The second value
     */
    private int mRightPart;

    /**
     * The operation
     */
    private Operation mOperation;

    /* ****************** *
     * INHERTITED METHODS *
     * ****************** */

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

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        initDefaultValues();
        initButtonsClickListeners();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.dummy_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menuButtonHelp:
            showHelp("Code d'Armor rox du poney et vous souhaite la bonne anne !");
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    /* ************* *
     * OTHER METHODS *
     * ************* */

    /**
     * Cleans the GUI and resets inner values
     */
    private void initDefaultValues() {
        mLeftPart = 0;
        mRightPart = 0;
        mScore = 0;
        mOperation = Operation.NOPE;
        TextView tvScore = (TextView) findViewById(R.id.tvScore);
        tvScore.setText("");
    }

    /**
     * Computes the operation with an operator and two parts
     * Controls are done on the fields.
     * @param left -
     * @param op -
     * @param right -
     */
    private double compute(int left, Operation op, int right) {
        if (op == Operation.NOPE)
            throw new RuntimeException("Nope nope nope nope \\(_o)/");
        if (op == Operation.DIV && right == 0)
            throw new IllegalArgumentException("Tryin' to divide by 0? Herp.");
        switch (op) {
        case ADD:
            return left + right;
        case SUB:
            return left - right;
        case DIV:
            return left / right;
        case MULT:
            return left * right;
        default:
            return Double.NaN;
        }
    }

    /**
     * Computes the operation with an operator and two parts
     * There is no control on the fields.
     * @param left -
     * @param op -
     * @param right -
     */
    private double computeDummy(int left, Operation op, int right) {
        switch (op) {
        case ADD:
            return left + right;
        case SUB:
            return left - right;
        case DIV:
            return left / right;
        case MULT:
            return left * right;
        default:
            return Double.NaN;
        }
    }

    /**
     * What should it do from your point of view?
     * @param help - A message to display
     */
    private void showHelp(String help) {
        if (help == null || help.length() <= 0 || (help.length() > 0 && help.trim().length() <= 0))
            throw new IllegalArgumentException("What's that string ?");
        Toast.makeText(this, help, Toast.LENGTH_LONG).show();
    }

    /**
     * What should it do from your point of view?
     * No controls are done.
     * @param help - A message to display
     */
    private void showHelpDummy(String help) {
        Toast.makeText(this, help, Toast.LENGTH_LONG).show();
    }

    /**
     * Initializes the click listeners on the buttons
     */
    private void initButtonsClickListeners() {

        View.OnClickListener globalClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (v != null && !(v instanceof Button))
                        return;
                    Button b = (Button) v;
                    String val = b.getText().toString();
                    TextView tvScore = (TextView) findViewById(R.id.tvScore);
                    String text = tvScore.getText() + "";
                    tvScore.setText(text + val);
                    switch (val) {
                    case "0":
                    case "1":
                    case "2":
                    case "3":
                    case "4":
                    case "5":
                    case "6":
                    case "7":
                    case "8":
                    case "9":
                        if (mOperation == Operation.NOPE) {
                            mLeftPart = Integer.parseInt(val);
                        } else {
                            mRightPart = Integer.parseInt(val);
                        }
                        break;
                    case "+":
                        mOperation = Operation.ADD;
                        break;
                    case "-":
                        mOperation = Operation.SUB;
                        break;
                    case "/":
                        mOperation = Operation.DIV;
                        break;
                    case "X":
                        mOperation = Operation.MULT;
                        break;
                    case "=":
                        mScore = compute(mLeftPart, mOperation, mRightPart);
                        tvScore.setText(Double.toString(mScore));
                        break;
                    case "Clean":
                    default:
                        initDefaultValues();
                        break;
                    }
                } catch (Exception e) {
                    Log.e("CalculatorActivity", e.getMessage());
                    TextView tvLog = (TextView) findViewById(R.id.tvLog);
                    tvLog.setText(e.getMessage());
                }
            }
        };

        findViewById(R.id.button0).setOnClickListener(globalClickListener);
        findViewById(R.id.button1).setOnClickListener(globalClickListener);
        findViewById(R.id.button2).setOnClickListener(globalClickListener);
        findViewById(R.id.button3).setOnClickListener(globalClickListener);
        findViewById(R.id.button0).setOnClickListener(globalClickListener);
        findViewById(R.id.button5).setOnClickListener(globalClickListener);
        findViewById(R.id.button6).setOnClickListener(globalClickListener);
        findViewById(R.id.button7).setOnClickListener(globalClickListener);
        findViewById(R.id.button8).setOnClickListener(globalClickListener);
        findViewById(R.id.button9).setOnClickListener(globalClickListener);
        findViewById(R.id.buttonAdd).setOnClickListener(globalClickListener);
        findViewById(R.id.buttonDiv).setOnClickListener(globalClickListener);
        findViewById(R.id.buttonSub).setOnClickListener(globalClickListener);
        findViewById(R.id.buttonMul).setOnClickListener(globalClickListener);
        findViewById(R.id.buttonEqual).setOnClickListener(globalClickListener);
        findViewById(R.id.buttonClean).setOnClickListener(globalClickListener);

    }

    /* ********** *
     * INNER ENUM *
     * ********** */

    /**
     * The operation the calculator should process
     */
    private enum Operation {
        /**
         * Addition
         */
        ADD,
        /**
         * Subtraction
         */
        SUB,
        /**
         * Multiply by
         */
        MULT,
        /**
         * Division
         */
        DIV,
        /**
         * Nothing
         */
        NOPE
    }

}