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 #18 #19

Open
wants to merge 3 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
47 changes: 42 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,33 @@
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;
import com.google.android.material.snackbar.BaseTransientBottomBar;
import com.google.android.material.snackbar.Snackbar;

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 +72,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 All @@ -83,8 +96,15 @@ private void handleOperatorClick(String operator){
totalCalculation += currentNumber + operator;
currentNumber = "";
} else {
totalCalculation = totalCalculation.substring(0, totalCalculation.length() - 1);
totalCalculation += operator;
if(totalCalculation.equals("") || totalCalculation.length() == 0)
{
Toast.makeText(getApplicationContext(), "Please enter operands first", Toast.LENGTH_SHORT)
.show();
}
else {
totalCalculation = totalCalculation.substring(0, totalCalculation.length() - 1);
totalCalculation += operator;
}
}
}

Expand All @@ -104,9 +124,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();
}
}