Skip to content
Open
Show file tree
Hide file tree
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
116 changes: 0 additions & 116 deletions .idea/codeStyles/Project.xml

This file was deleted.

9 changes: 0 additions & 9 deletions .idea/misc.xml

This file was deleted.

12 changes: 0 additions & 12 deletions .idea/runConfigurations.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

44 changes: 35 additions & 9 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 @@ -93,20 +102,37 @@ private double add(double a, double b){
}

private double sub(double a, double b){
return a + b;
return a - b;
}

private double mul(double a, double b){
return a + b;
return a * b;
}

private double div(double a, double b){
return a + 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();
}
}
}
4 changes: 2 additions & 2 deletions app/src/main/res/layout/content_answer_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
android:layout_marginTop="16dp"
android:gravity="end"
android:textSize="24sp"
android:text="Total Calculation"
android:text="@string/total_calculation"
android:textColor="#757575"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Expand All @@ -24,7 +24,7 @@
android:layout_marginEnd="16dp"
android:gravity="end"
android:textSize="32sp"
android:text="Current Calculation"
android:text="@string/current_calculation"
android:textColor="#757575"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Expand Down
Loading