Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
8.2 kB
2
Indexable
Never
package com.vrushabhhirap.it7_homeworkcalculator;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.button.MaterialButton;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    //All the declarations

    MaterialButton btn_one, btn_two, btn_three, btn_four, btn_five, btn_six, btn_seven, btn_eight, btn_nine, btn_zero;
    MaterialButton btn_plus, btn_minus, btn_multiply, btn_divide, btn_equalsTo;
    MaterialButton btn_c, btn_ac, btn_dot, btn_open_bracket, btn_close_bracket;

    TextView solutionTV, resultTV;

    StringBuilder currentNumber = new StringBuilder();


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

        //implementing the xml button to use in java code

        btn_one = findViewById(R.id.button_one);
        btn_two = findViewById(R.id.button_two);
        btn_three = findViewById(R.id.button_three);
        btn_four = findViewById(R.id.button_four);
        btn_five = findViewById(R.id.button_five);
        btn_six = findViewById(R.id.button_six);
        btn_seven = findViewById(R.id.button_seven);
        btn_eight = findViewById(R.id.button_eight);
        btn_nine = findViewById(R.id.button_nine);
        btn_zero = findViewById(R.id.button_zero);
        btn_c = findViewById(R.id.button_c);
        btn_ac = findViewById(R.id.button_AC);
        btn_open_bracket = findViewById(R.id.button_open_bracket);
        btn_close_bracket = findViewById(R.id.button_close_bracket);
        btn_dot = findViewById(R.id.button_dot);
        btn_plus = findViewById(R.id.button_plus);
        btn_minus = findViewById(R.id.button_minus);
        btn_multiply = findViewById(R.id.button_multiply);
        btn_divide = findViewById(R.id.button_divide);
        btn_equalsTo = findViewById(R.id.button_equal_to);

        solutionTV = findViewById(R.id.displaytextview);
        resultTV = findViewById(R.id.displayanswerview);

        //setting the on click listerner for all the material button

        btn_one.setOnClickListener(this);
        btn_two.setOnClickListener(this);
        btn_three.setOnClickListener(this);
        btn_four.setOnClickListener(this);
        btn_five.setOnClickListener(this);
        btn_six.setOnClickListener(this);
        btn_seven.setOnClickListener(this);
        btn_eight.setOnClickListener(this);
        btn_nine.setOnClickListener(this);
        btn_zero.setOnClickListener(this);
        btn_ac.setOnClickListener(this);
        btn_c.setOnClickListener(this);
        btn_open_bracket.setOnClickListener(this);
        btn_close_bracket.setOnClickListener(this);
        btn_divide.setOnClickListener(this);
        btn_multiply.setOnClickListener(this);
        btn_plus.setOnClickListener(this);
        btn_minus.setOnClickListener(this);
        btn_dot.setOnClickListener(this);
        btn_equalsTo.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        //taking users input to the String buttonText
        MaterialButton button = (MaterialButton) view;
        String butttonText = button.getText().toString();
        String dataToCalculate = solutionTV.getText().toString();

        switch (butttonText) {
            case "+":
            case "-":
            case "x":
            case "/":
                //if the datatocalculate is not empty and it is nor the +,-,*,/ then we will add the input button to the datatocalculate
                if (!dataToCalculate.isEmpty() && !isOperator(dataToCalculate.charAt(dataToCalculate.length() - 1))) {
                    dataToCalculate += butttonText;
                    solutionTV.setText(dataToCalculate);

                } else {
                    //and else we will genrate the toast as an error
                    Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();

                }
                break;
            case "AC":
                //when user clicks on ac then solution and result will be empty
                solutionTV.setText("");
                resultTV.setText("0");
                break;
            case "=":
                //calculate here
                calculation(dataToCalculate, butttonText, resultTV);
                break;
            case "C":
                //when user clickes the c then we want to cut the size of the datatocalculate to -1
                dataToCalculate = solutionTV.getText().toString();
                if (!dataToCalculate.isEmpty()) {
                    StringBuilder sb = new StringBuilder(dataToCalculate);
                    sb.setLength(dataToCalculate.length() - 1);
                    solutionTV.setText(sb.toString());
                }
                break;
            case ".":
                //when user clicks on dot it should automatically add 0 before the dot when it is clicked after the any operator or the first time
                if (dataToCalculate.isEmpty() || isOperator(dataToCalculate.charAt(dataToCalculate.length() - 1))) {
                    dataToCalculate += 0;
                    dataToCalculate += butttonText;
                    solutionTV.setText(dataToCalculate);
                } else {
                    dataToCalculate += butttonText;
                    solutionTV.setText(dataToCalculate);
                }
                break;
            default:
                //other than the above when user enters any other no we will add it to the datatocalculate and update the solutionTV

                dataToCalculate = dataToCalculate + butttonText;
                solutionTV.setText(dataToCalculate);
                break;
        }
    }

    private boolean isOperator(char c) {
        return c == '+' || c == '-' || c == 'x' || c == '/';
    }

    public void calculation(String dataToCalculate, String buttonText, TextView ResultTV) {
        List<Double> numbers = new ArrayList<>();
        List<Character> operators = new ArrayList<>();
        StringBuilder currentNumber = new StringBuilder();

        // Parse the input string into numbers and operators
        for (int i = 0; i < dataToCalculate.length(); i++) {
            char ch = dataToCalculate.charAt(i);
            if (Character.isDigit(ch) || ch == '.') {
                currentNumber.append(ch);
            } else if (ch == '+' || ch == '-' || ch == 'x' || ch == '/') {
                if (currentNumber.length() > 0) {
                    numbers.add(Double.parseDouble(currentNumber.toString()));
                    currentNumber.setLength(0);
                }
                operators.add(ch);
            }
        }
        if (currentNumber.length() > 0) {
            numbers.add(Double.parseDouble(currentNumber.toString()));
        }

        // Handle multiplication and division first
        for (int i = 0; i < operators.size(); i++) {
            if (operators.get(i) == 'x' || operators.get(i) == '/') {
                double num1 = numbers.get(i);
                double num2 = numbers.get(i + 1);
                double result = 0;
                if (operators.get(i) == 'x') {
                    result = num1 * num2;
                } else if (operators.get(i) == '/') {
                    result = num1 / num2;
                }
                numbers.set(i, result);
                numbers.remove(i + 1);
                operators.remove(i);
                i--; // Re-evaluate this position since we removed an operator
            }
        }

        // Handle addition and subtraction
        double finalResult = numbers.get(0);
        for (int i = 0; i < operators.size(); i++) {
            char operator = operators.get(i);
            double num = numbers.get(i + 1);
            if (operator == '+') {
                finalResult += num;
            } else if (operator == '-') {
                finalResult -= num;
            }
        }

        ResultTV.setText(String.valueOf(finalResult));
    }
}
Leave a Comment