diff --git a/Calculator.iml b/Calculator.iml new file mode 100644 index 0000000..6aea003 --- /dev/null +++ b/Calculator.iml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/app.iml b/app/app.iml new file mode 100644 index 0000000..6421484 --- /dev/null +++ b/app/app.iml @@ -0,0 +1,97 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..6813f00 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,26 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 23 + buildToolsVersion "23.0.2" + + defaultConfig { + applicationId "com.mrcrambo.calculator" + minSdkVersion 14 + targetSdkVersion 23 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + testCompile 'junit:junit:4.12' + compile 'com.android.support:appcompat-v7:23.1.1' +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..2faa041 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /Users/ilnarkadyrov/Library/Android/sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/app/src/androidTest/java/com/mrcrambo/calculator/ApplicationTest.java b/app/src/androidTest/java/com/mrcrambo/calculator/ApplicationTest.java new file mode 100644 index 0000000..4aaac58 --- /dev/null +++ b/app/src/androidTest/java/com/mrcrambo/calculator/ApplicationTest.java @@ -0,0 +1,13 @@ +package com.mrcrambo.calculator; + +import android.app.Application; +import android.test.ApplicationTestCase; + +/** + * Testing Fundamentals + */ +public class ApplicationTest extends ApplicationTestCase { + public ApplicationTest() { + super(Application.class); + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..ae037cc --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + diff --git a/app/src/main/java/com/mrcrambo/calculator/Calculator.java b/app/src/main/java/com/mrcrambo/calculator/Calculator.java new file mode 100644 index 0000000..55e8c0c --- /dev/null +++ b/app/src/main/java/com/mrcrambo/calculator/Calculator.java @@ -0,0 +1,106 @@ +package com.mrcrambo.calculator; + +import java.math.BigDecimal; + +/** + * Created by ilnarkadyrov on 12/9/15. + */ +public class Calculator{ + private Double mOperand = Double.valueOf(0); + private Double mWaitingOperand = Double.valueOf(0); + private Double mCalcMemory = Double.valueOf(0); + private String mWaitOperator = ""; + + //operator types + public static final String ADD = "+"; + public static final String SUB = "-"; + public static final String DIV = "/"; + public static final String MUL = "*"; + + public static final String CLEAR = "C"; + public static final String CLEARMEM = "MC"; + public static final String ADDTOMEM = "M+"; + public static final String DELFROMMEM = "M-"; + public static final String CALLMEM = "MR"; + public static final String CHANGESIGN = "+/-"; + + public void setOperand(double operand){ + this.mOperand = operand; + } + + public Double getOperand(){ + return mOperand; + } + + public Double getWaitingOperand() { + return mWaitingOperand; + } + + public String getWaitOperator() { + return mWaitOperator; + } + + public void setWaitingOperand(double mWaitingOperand) { + this.mWaitingOperand = mWaitingOperand; + } + + public void setWaitOperator(String mWaitOperator) { + this.mWaitOperator = mWaitOperator; + } + + public void setCalcMemory(double memory){ + mCalcMemory = memory; + } + + public Double getCalcMemory() { + return mCalcMemory; + } + + @Override + public String toString() { + return String.valueOf(mOperand); + } + + private String toString(BigDecimal mOperand) { + return mOperand.toString(); + } + + protected Double Operation(String operator){ + if (operator.equals(CLEAR)){ + mOperand = Double.valueOf(0); + mWaitOperator = ""; + mWaitingOperand = Double.valueOf(0); + } else if(operator.equals(CLEARMEM)){ + mCalcMemory = Double.valueOf(0); + } else if (operator.equals(ADDTOMEM)){ + //add current number to memory + mCalcMemory += mOperand; + } else if (operator.equals(DELFROMMEM)){ + //sub current number from memory + mCalcMemory -= mOperand; + } else if (operator.equals(CALLMEM)){ + mOperand = mCalcMemory; + } else if (operator.equals(CHANGESIGN)){ + mOperand = -mOperand; + } else { + calcOperation(); + mWaitingOperand = mOperand; + mWaitOperator = operator; + } + return mOperand; + } + + protected void calcOperation(){ + if (mWaitOperator.equals(ADD)){ + mOperand += mWaitingOperand; + } else if (mWaitOperator.equals(SUB)){ + mOperand = mWaitingOperand - mOperand; + } else if (mWaitOperator.equals(MUL)){ + mOperand = mWaitingOperand * mOperand; + } else if (mWaitOperator.equals(DIV)){ + if (!mOperand.equals(Double.valueOf(0))) { + mOperand = mWaitingOperand / mOperand; + } + } + } +} diff --git a/app/src/main/java/com/mrcrambo/calculator/MainActivity.java b/app/src/main/java/com/mrcrambo/calculator/MainActivity.java new file mode 100644 index 0000000..37040ca --- /dev/null +++ b/app/src/main/java/com/mrcrambo/calculator/MainActivity.java @@ -0,0 +1,107 @@ +package com.mrcrambo.calculator; + +import android.app.Activity; +import android.os.Bundle; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.Button; +import android.widget.TextView; + +public class MainActivity extends Activity implements OnClickListener { + + public static final String TOKEN = "TOKEN"; + public static final String OPERAND = "OPERAND"; + public static final String MEMORY = "MEMORY"; + public static final String OPERATOR = "OPERATOR"; + public static final String WAITING_OPERATOR = "WAITINGOPERATOR"; + public static TextView calculatorDisplay; + private static final String DIGITS = "0123456789."; + private Boolean userIsInTheMiddleOfTypingANumber = false; + Calculator calculator; + + @Override + protected void onCreate(Bundle savedInstanceState) { + + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + calculator = new Calculator(); + calculatorDisplay = (TextView) findViewById(R.id.textView); + + findViewById(R.id.button0).setOnClickListener(this); + findViewById(R.id.button1).setOnClickListener(this); + findViewById(R.id.button2).setOnClickListener(this); + findViewById(R.id.button3).setOnClickListener(this); + findViewById(R.id.button4).setOnClickListener(this); + findViewById(R.id.button5).setOnClickListener(this); + findViewById(R.id.button6).setOnClickListener(this); + findViewById(R.id.button7).setOnClickListener(this); + findViewById(R.id.button8).setOnClickListener(this); + findViewById(R.id.button9).setOnClickListener(this); + + findViewById(R.id.buttonAdd).setOnClickListener(this); + findViewById(R.id.buttonSubtract).setOnClickListener(this); + findViewById(R.id.buttonMultiply).setOnClickListener(this); + findViewById(R.id.buttonDivide).setOnClickListener(this); + findViewById(R.id.buttonChangeSign).setOnClickListener(this); + findViewById(R.id.buttonDecimalPoint).setOnClickListener(this); + findViewById(R.id.buttonEquals).setOnClickListener(this); + findViewById(R.id.buttonClear).setOnClickListener(this); + findViewById(R.id.buttonClearMemory).setOnClickListener(this); + findViewById(R.id.buttonAddToMemory).setOnClickListener(this); + findViewById(R.id.buttonSubtractFromMemory).setOnClickListener(this); + findViewById(R.id.buttonRecallMemory).setOnClickListener(this); + } + + @Override + public void onClick(View view) { + + String buttonPressed = ((Button) view).getText().toString(); + + if (DIGITS.contains(buttonPressed)) { + // digit was pressed + if (userIsInTheMiddleOfTypingANumber) { + calculatorDisplay.append(buttonPressed); + } else { + calculatorDisplay.setText(buttonPressed); + userIsInTheMiddleOfTypingANumber = true; + } + } else { + // operation was pressed + if (userIsInTheMiddleOfTypingANumber) { + calculator.setOperand(Double.parseDouble(calculatorDisplay.getText().toString())); + userIsInTheMiddleOfTypingANumber = false; + } + + calculator.Operation(buttonPressed); + if (calculator.getOperand().longValue() == calculator.getOperand()){ + calculatorDisplay.setText(String.valueOf(calculator.getOperand().longValue())); + }else { + calculatorDisplay.setText(String.valueOf(calculator.getOperand())); + } + + } + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + // Save variables on screen orientation change + outState.putString(TOKEN, calculatorDisplay.getText().toString()); + outState.putDouble(OPERAND, Double.valueOf(calculatorDisplay.getText().toString())); + outState.putDouble(MEMORY, calculator.getCalcMemory()); + outState.putString(OPERATOR, calculator.getWaitOperator()); + outState.putDouble(WAITING_OPERATOR, calculator.getWaitingOperand()); + } + + @Override + protected void onRestoreInstanceState(Bundle savedInstanceState) { + super.onRestoreInstanceState(savedInstanceState); + // Restore variables on screen orientation change + calculator.setOperand(savedInstanceState.getDouble(OPERAND)); + calculator.setCalcMemory(savedInstanceState.getDouble(MEMORY)); + calculatorDisplay.setText(savedInstanceState.getString(TOKEN)); + calculator.setWaitOperator(savedInstanceState.getString(OPERATOR)); + calculator.setWaitingOperand(savedInstanceState.getDouble(WAITING_OPERATOR)); + } +} diff --git a/app/src/main/res/drawable/button_selector.xml b/app/src/main/res/drawable/button_selector.xml new file mode 100644 index 0000000..ce6b1ce --- /dev/null +++ b/app/src/main/res/drawable/button_selector.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..86bd800 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,275 @@ + + + + + + + + + +