mirror of
https://github.com/SimpleMobileTools/Simple-Calculator.git
synced 2025-02-16 19:40:48 +01:00
add a couple tests + minor refactoring
This commit is contained in:
parent
b0f64390dd
commit
95d3b31102
@ -1,13 +1,22 @@
|
|||||||
package calculator.simplemobiletools.com.simple_calculator;
|
package calculator.simplemobiletools.com.simple_calculator;
|
||||||
|
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
|
import java.text.DecimalFormatSymbols;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
public class Formatter {
|
public class Formatter {
|
||||||
public static String doubleToString(double d) {
|
public static String doubleToString(double d) {
|
||||||
if (d == (long) d) {
|
if (d == (long) d) {
|
||||||
return String.format("%d", (long) d);
|
return String.format("%d", (long) d);
|
||||||
} else {
|
} else {
|
||||||
final DecimalFormat formatter = new DecimalFormat("0.0#############");
|
final DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
|
||||||
|
symbols.setDecimalSeparator('.');
|
||||||
|
|
||||||
|
final DecimalFormat formatter = new DecimalFormat();
|
||||||
|
formatter.setMaximumIntegerDigits(12);
|
||||||
|
formatter.setMaximumFractionDigits(12);
|
||||||
|
formatter.setDecimalFormatSymbols(symbols);
|
||||||
|
formatter.setGroupingUsed(false);
|
||||||
return formatter.format(d);
|
return formatter.format(d);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,14 +19,14 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
private int lastKey;
|
private int lastKey;
|
||||||
private int lastOperation;
|
private int lastOperation;
|
||||||
|
|
||||||
private static final int DIGIT = 0;
|
public static final int DIGIT = 0;
|
||||||
private static final int EQUALS = 1;
|
public static final int EQUALS = 1;
|
||||||
private static final int PLUS = 2;
|
public static final int PLUS = 2;
|
||||||
private static final int MINUS = 3;
|
public static final int MINUS = 3;
|
||||||
private static final int MULTIPLY = 4;
|
public static final int MULTIPLY = 4;
|
||||||
private static final int DIVIDE = 5;
|
public static final int DIVIDE = 5;
|
||||||
private static final int MODULO = 6;
|
public static final int MODULO = 6;
|
||||||
private static final int POWER = 7;
|
public static final int POWER = 7;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
@ -42,13 +42,17 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
resetValue = false;
|
resetValue = false;
|
||||||
lastKey = 0;
|
lastKey = 0;
|
||||||
lastOperation = 0;
|
lastOperation = 0;
|
||||||
result.setText("0");
|
setResult("0");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addDigit(int number) {
|
public void addDigit(int number) {
|
||||||
final String currentValue = getDisplayedNumber();
|
final String currentValue = getDisplayedNumber();
|
||||||
final String newValue = removeLeadingZero(currentValue + number);
|
final String newValue = removeLeadingZero(currentValue + number);
|
||||||
result.setText(newValue);
|
setResult(newValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setResult(String value) {
|
||||||
|
result.setText(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String removeLeadingZero(String str) {
|
private String removeLeadingZero(String str) {
|
||||||
@ -57,17 +61,16 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getDisplayedNumber() {
|
private String getDisplayedNumber() {
|
||||||
resetValueIfNeeded();
|
|
||||||
return result.getText().toString();
|
return result.getText().toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private double getDisplayedNumberAsDouble() {
|
public double getDisplayedNumberAsDouble() {
|
||||||
return Double.parseDouble(result.getText().toString());
|
return Double.parseDouble(getDisplayedNumber());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void resetValueIfNeeded() {
|
private void resetValueIfNeeded() {
|
||||||
if (resetValue)
|
if (resetValue)
|
||||||
result.setText("0");
|
setResult("0");
|
||||||
|
|
||||||
resetValue = false;
|
resetValue = false;
|
||||||
}
|
}
|
||||||
@ -95,13 +98,13 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
updateResult(resultValue);
|
updateResult(resultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleOperation(int operation) {
|
public void handleOperation(int operation) {
|
||||||
if (lastKey == operation)
|
if (lastKey == operation)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (lastKey == DIGIT) {
|
if (lastKey == DIGIT)
|
||||||
getResult();
|
getResult();
|
||||||
}
|
|
||||||
resetValue = true;
|
resetValue = true;
|
||||||
lastKey = operation;
|
lastKey = operation;
|
||||||
lastOperation = operation;
|
lastOperation = operation;
|
||||||
@ -161,7 +164,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
if (newValue.equals("-0"))
|
if (newValue.equals("-0"))
|
||||||
newValue = "0";
|
newValue = "0";
|
||||||
|
|
||||||
result.setText(newValue);
|
setResult(newValue);
|
||||||
baseValue = Double.parseDouble(newValue);
|
baseValue = Double.parseDouble(newValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -188,28 +191,28 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
String value = getDisplayedNumber();
|
String value = getDisplayedNumber();
|
||||||
if (!value.contains("."))
|
if (!value.contains("."))
|
||||||
value += ".";
|
value += ".";
|
||||||
result.setText(value);
|
setResult(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void zeroClicked() {
|
public void zeroClicked() {
|
||||||
String value = getDisplayedNumber();
|
String value = getDisplayedNumber();
|
||||||
if (!value.equals("0"))
|
if (!value.equals("0"))
|
||||||
value += "0";
|
value += "0";
|
||||||
result.setText(value);
|
setResult(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateResult(double value) {
|
private void updateResult(double value) {
|
||||||
result.setText(Formatter.doubleToString(value));
|
setResult(Formatter.doubleToString(value));
|
||||||
baseValue = value;
|
baseValue = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void getResult() {
|
public void getResult() {
|
||||||
secondValue = getDisplayedNumberAsDouble();
|
secondValue = getDisplayedNumberAsDouble();
|
||||||
calculateResult();
|
calculateResult();
|
||||||
baseValue = getDisplayedNumberAsDouble();
|
baseValue = getDisplayedNumberAsDouble();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void calculateResult() {
|
public void calculateResult() {
|
||||||
switch (lastOperation) {
|
switch (lastOperation) {
|
||||||
case PLUS:
|
case PLUS:
|
||||||
updateResult(baseValue + secondValue);
|
updateResult(baseValue + secondValue);
|
||||||
@ -234,12 +237,13 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@OnClick({R.id.btn_decimal, R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7,
|
@OnClick({R.id.btn_decimal, R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7, R.id.btn_8,
|
||||||
R.id.btn_8, R.id.btn_9})
|
R.id.btn_9})
|
||||||
public void numpadClicked(View view) {
|
public void numpadClicked(View view) {
|
||||||
if (lastKey == EQUALS)
|
if (lastKey == EQUALS)
|
||||||
lastOperation = EQUALS;
|
lastOperation = EQUALS;
|
||||||
lastKey = DIGIT;
|
lastKey = DIGIT;
|
||||||
|
resetValueIfNeeded();
|
||||||
|
|
||||||
switch (view.getId()) {
|
switch (view.getId()) {
|
||||||
case R.id.btn_decimal:
|
case R.id.btn_decimal:
|
||||||
|
@ -17,7 +17,6 @@
|
|||||||
android:layout_marginBottom="@dimen/activity_margin"
|
android:layout_marginBottom="@dimen/activity_margin"
|
||||||
android:gravity="right"
|
android:gravity="right"
|
||||||
android:lines="1"
|
android:lines="1"
|
||||||
android:maxLength="15"
|
|
||||||
android:padding="@dimen/activity_margin"
|
android:padding="@dimen/activity_margin"
|
||||||
android:text="0"
|
android:text="0"
|
||||||
android:textSize="@dimen/display_text_size"/>
|
android:textSize="@dimen/display_text_size"/>
|
||||||
|
@ -1,10 +1,15 @@
|
|||||||
package calculator.simplemobiletools.com.simple_calculator;
|
package calculator.simplemobiletools.com.simple_calculator;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.robolectric.Robolectric;
|
import org.robolectric.Robolectric;
|
||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
|
import butterknife.ButterKnife;
|
||||||
|
|
||||||
|
import static junit.framework.Assert.assertEquals;
|
||||||
|
|
||||||
@RunWith(MyTestRunner.class)
|
@RunWith(MyTestRunner.class)
|
||||||
@Config(constants = BuildConfig.class, manifest = "app/src/main/AndroidManifest.xml", sdk = 21)
|
@Config(constants = BuildConfig.class, manifest = "app/src/main/AndroidManifest.xml", sdk = 21)
|
||||||
public class MainActivityTest {
|
public class MainActivityTest {
|
||||||
@ -13,5 +18,28 @@ public class MainActivityTest {
|
|||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
activity = Robolectric.setupActivity(MainActivity.class);
|
activity = Robolectric.setupActivity(MainActivity.class);
|
||||||
|
ButterKnife.bind(activity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void addSimpleDigit() {
|
||||||
|
activity.addDigit(2);
|
||||||
|
assertEquals(2.0, activity.getDisplayedNumberAsDouble());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void removeLeadingZero() {
|
||||||
|
activity.addDigit(0);
|
||||||
|
activity.addDigit(5);
|
||||||
|
assertEquals(5.0, activity.getDisplayedNumberAsDouble());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void addition_test() {
|
||||||
|
activity.setResult("-1.2");
|
||||||
|
activity.handleOperation(MainActivity.PLUS);
|
||||||
|
activity.setResult("3.4");
|
||||||
|
activity.getResult();
|
||||||
|
assertEquals(2.2, activity.getDisplayedNumberAsDouble());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user