Skip to content

Commit

Permalink
Tried to add functionality to operator buttons, but with the exceptio…
Browse files Browse the repository at this point in the history
…n of sqrt, it doesn't work.
  • Loading branch information
loweab committed Jul 25, 2015
1 parent 949353d commit 99cb677
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 5 deletions.
127 changes: 122 additions & 5 deletions app/src/main/java/com/alexlowe/calculator/MainActivity.java
Expand Up @@ -2,18 +2,17 @@

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

String screenString = "";
TextView screen;

Double number1 = null;
Double number2 = null;
String operator;


@Override
Expand Down Expand Up @@ -76,15 +75,133 @@ public void press0(View view) {
}

public void pressPoint(View view) {
screenString += ".";
if (!screenString.contains(".")) {
screenString += ".";
displayScreen(screenString);
}
}

public void pressBack(View view) {
if (screenString.length() > 0) {
screenString = screenString.substring(0, screenString.length() - 1);
}
displayScreen(screenString);
}

public void pressAC(View view) {
number1 = null;
number2 = null;
operator = "";
screenString = "";
displayScreen(screenString);
}

public void pressSqrt(View view) {
if (!screenString.equals("")) {
number1 = Math.sqrt(Double.parseDouble(screenString));
screenString = String.valueOf(number1);
operator = "SR";
displayScreen(screenString);
}
}

public void pressPercent(View view) {
operation("%");
}

public void pressPlus(View view) {
operation("+");
/* --Old Code
if(screenString != "") {
if(number1 != null && number2 == null){
number2 = Double.parseDouble(screenString);
operator = "+";
Double result = calculate(number1, number2, operator);
screenString = String.valueOf(result);
displayScreen(screenString);
}else{
number1 = Double.parseDouble(screenString);
number2 = null;
operator = "+";
screenString = "";
displayScreen(screenString);
}
}*/
}

public void pressMinus(View view) {
operation("-");
}

public void pressMult(View view) {
operation("*");
}


public void pressDiv(View view) {
operation("/");
}

public void pressEqual(View view) {
//pick up here, trying to get plus operation to work, switch statment for operator, check for value in holder and screen
//moved operation to calculate method instead of in each operation button's method
if (!operator.equals("") && !screenString.equals("")) {
if (operator.equals("SR")) {
pressSqrt(view);
} else {
number2 = Double.parseDouble(screenString);
number1 = calculate(number1, number2, operator);
screenString = String.valueOf(number1);
}
}
}

private Double calculate(Double num1, Double num2, String operator) {
Double result = 0.0;
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
result = num1 / num2;
break;
case "%":
result = (num1 / 100) * num2;
break;
}
return result;
}

private void operation(String op) {
if (!screenString.equals("")) {
if (number1 != null && number2 == null) {

number2 = Double.parseDouble(screenString);
operator = op;

screenString = "";
displayScreen(screenString);

number1 = calculate(number1, number2, operator);
number2 = null;
} else {

number1 = Double.parseDouble(screenString);
number2 = null;
operator = op;

screenString = "";
displayScreen(screenString);
}
}
}

private void displayScreen(String screenString) {
screen.setText(screenString);
}
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Expand Up @@ -12,6 +12,7 @@
android:text="0"
android:layout_width="match_parent"
android:layout_height="150dp"
android:maxLength="12"
android:id="@+id/screen"/>

<GridLayout
Expand All @@ -38,6 +39,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="%"
android:onClick="pressPercent"
android:id="@+id/buttonPercent"
android:singleLine="true"
/>
Expand All @@ -47,6 +49,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:onClick="pressDiv"
android:id="@+id/buttonDivide"
android:singleLine="true"
/>
Expand All @@ -57,6 +60,7 @@
android:layout_height="wrap_content"
android:text="&lt;"
android:id="@+id/buttonBackspace"
android:onClick="pressBack"
android:singleLine="true"
/>

Expand Down Expand Up @@ -95,6 +99,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x"
android:onClick="pressMult"
android:id="@+id/buttonMult"
android:singleLine="true"
/>
Expand Down Expand Up @@ -133,6 +138,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:onClick="pressMinus"
android:id="@+id/buttonSub"
android:singleLine="true"
/>
Expand Down Expand Up @@ -172,6 +178,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:onClick="pressPlus"
android:id="@+id/buttonPlus"
android:singleLine="true"
/>
Expand Down Expand Up @@ -202,6 +209,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sr"
android:onClick="pressSqrt"
android:id="@+id/buttonSquareRoot"
android:singleLine="true"
/>
Expand All @@ -211,6 +219,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:onClick="pressEqual"
android:id="@+id/buttonEqual"
android:singleLine="true"
/>
Expand Down

0 comments on commit 99cb677

Please sign in to comment.