Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Issue 5/ Equal button working #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions app/src/main/java/com/exuberant/calci/HomeActivity.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
package com.exuberant.calci;

import androidx.appcompat.app.AppCompatActivity;

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

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

public class HomeActivity extends AppCompatActivity implements View.OnClickListener {

private String currentNumber = "", totalCalculation = "";
private char operator ;
private double firstOperand;
private double secondOperand;
private TextView totalCalculationTextView, currentAnswerTextView;

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

initializeViews();

}

private void initializeViews() {
findViewById(R.id.btn_zero).setOnClickListener(this);
findViewById(R.id.btn_one).setOnClickListener(this);
findViewById(R.id.btn_two).setOnClickListener(this);
Expand Down Expand Up @@ -62,8 +68,11 @@ public void onClick(View view) {

//Handle calculation
case R.id.btn_equals:
firstOperand = Double.valueOf(totalCalculation.substring(0,totalCalculation.length()-1));
secondOperand = Double.valueOf(currentNumber);
operator = totalCalculation.charAt(totalCalculation.length()-1);
totalCalculation += currentNumber;
calculateAnswer();
calculateAnswer(firstOperand, secondOperand, operator);
break;

//Handle other numerical button clicks
Expand Down Expand Up @@ -104,9 +113,26 @@ private double div(double a, double b){
return a / b;
}

private void calculateAnswer(){
private void calculateAnswer(Double firstOperand, Double secondOperand, char operator){
//Use totalCalculation string to get final answer and display it
double answer = 0.0;
if(operator == '+')
{
answer = add(firstOperand, secondOperand);
}
else if(operator == '-')
{
answer = sub(firstOperand, secondOperand);
}
else if (operator == '/')
{
answer = div(firstOperand, secondOperand);
}
else
{
answer = mul(firstOperand, secondOperand);
}
currentNumber = String.valueOf(answer);
updateDisplay();
}
}