diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..bdb0cab --- /dev/null +++ b/.gitattributes @@ -0,0 +1,17 @@ +# Auto detect text files and perform LF normalization +* text=auto + +# Custom for Visual Studio +*.cs diff=csharp + +# Standard to msysgit +*.doc diff=astextplain +*.DOC diff=astextplain +*.docx diff=astextplain +*.DOCX diff=astextplain +*.dot diff=astextplain +*.DOT diff=astextplain +*.pdf diff=astextplain +*.PDF diff=astextplain +*.rtf diff=astextplain +*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9c4de58 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build +/captures diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..59c1fd2 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +SimpleCalculator \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..97626ba --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..8d2df47 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,18 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..cca2cda --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1.8 + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..9892fe7 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/SimpleCalculator.iml b/SimpleCalculator.iml new file mode 100644 index 0000000..e50ab2a --- /dev/null +++ b/SimpleCalculator.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..73a66a7 --- /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..2633807 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,26 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 23 + buildToolsVersion "23.0.2" + + defaultConfig { + applicationId "profick.simplecalculator" + minSdkVersion 15 + 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.0' +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..061f802 --- /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 C:\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/profick/simplecalculator/ApplicationTest.java b/app/src/androidTest/java/profick/simplecalculator/ApplicationTest.java new file mode 100644 index 0000000..ed5a24a --- /dev/null +++ b/app/src/androidTest/java/profick/simplecalculator/ApplicationTest.java @@ -0,0 +1,13 @@ +package profick.simplecalculator; + +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..1163b8a --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + diff --git a/app/src/main/java/profick/simplecalculator/MainActivity.java b/app/src/main/java/profick/simplecalculator/MainActivity.java new file mode 100644 index 0000000..d718d02 --- /dev/null +++ b/app/src/main/java/profick/simplecalculator/MainActivity.java @@ -0,0 +1,224 @@ +package profick.simplecalculator; + +import android.graphics.Color; +import android.os.Bundle; +import android.support.v4.content.ContextCompat; +import android.support.v4.content.res.ResourcesCompat; +import android.support.v7.app.AppCompatActivity; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.Button; +import android.widget.LinearLayout; +import android.widget.TextView; + +import java.util.Random; + + +public class MainActivity extends AppCompatActivity implements OnClickListener { + + TextView tvResult; + private StringBuilder stringNumber; + private double number; + private boolean isFirst; + private char operation; + + + private void toScreen(String str) { + if (str.length() > 2) { + if (str.charAt(str.length() - 2) == '.' && str.charAt(str.length() - 1) == '0') { + str = str.substring(0, str.length() - 2); + } + } + + + tvResult.setText(str); + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.myscreen); + + if (savedInstanceState == null) { + stringNumber = new StringBuilder(); + clearStringNumber(); + number = 0; + isFirst = true; + operation = 0; + } + + + findViewById(R.id.buttonZero).setOnClickListener(this); + findViewById(R.id.buttonOne).setOnClickListener(this); + findViewById(R.id.buttonTwo).setOnClickListener(this); + findViewById(R.id.buttonThree).setOnClickListener(this); + findViewById(R.id.buttonFour).setOnClickListener(this); + findViewById(R.id.buttonFive).setOnClickListener(this); + findViewById(R.id.buttonSix).setOnClickListener(this); + findViewById(R.id.buttonSeven).setOnClickListener(this); + findViewById(R.id.buttonEight).setOnClickListener(this); + findViewById(R.id.buttonNine).setOnClickListener(this); + findViewById(R.id.buttonPoint).setOnClickListener(this); + findViewById(R.id.buttonAdd).setOnClickListener(this); + findViewById(R.id.buttonSub).setOnClickListener(this); + findViewById(R.id.buttonMul).setOnClickListener(this); + findViewById(R.id.buttonDiv).setOnClickListener(this); + findViewById(R.id.buttonAC).setOnClickListener(this); + findViewById(R.id.buttonRNG).setOnClickListener(this); + findViewById(R.id.buttonEqual).setOnClickListener(this); + findViewById(R.id.buttonSign).setOnClickListener(this); + + tvResult = (TextView) findViewById(R.id.textViewResult); + tvResult.setText(stringNumber); + + } + + + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + outState.putString("stringNumber", stringNumber.toString()); + outState.putChar("operation", operation); + outState.putDouble("number", number); + outState.putBoolean("isFirst", isFirst); + } + + @Override + protected void onRestoreInstanceState(Bundle savedInstanceState) { + super.onRestoreInstanceState(savedInstanceState); + stringNumber = new StringBuilder(savedInstanceState.getString("stringNumber")); + operation = savedInstanceState.getChar("operation"); + getButton(operation).setBackgroundColor(Color.GREEN); + number = savedInstanceState.getDouble("number"); + isFirst = savedInstanceState.getBoolean("isFirst"); + + } + + @Override + public void onClick(View v) { + switch (v.getId()) { + case R.id.buttonZero: pushDigit('0'); break; + case R.id.buttonOne: pushDigit('1'); break; + case R.id.buttonTwo: pushDigit('2'); break; + case R.id.buttonThree: pushDigit('3'); break; + case R.id.buttonFour: pushDigit('4'); break; + case R.id.buttonFive: pushDigit('5'); break; + case R.id.buttonSix: pushDigit('6'); break; + case R.id.buttonSeven: pushDigit('7'); break; + case R.id.buttonEight: pushDigit('8'); break; + case R.id.buttonNine: pushDigit('9'); break; + + case R.id.buttonAdd: pushOperation('+'); break; + case R.id.buttonSub: pushOperation('-'); break; + case R.id.buttonMul: pushOperation('*'); break; + case R.id.buttonDiv: pushOperation('/'); break; + + case R.id.buttonSign: changeSign(); break; + case R.id.buttonAC: pushAC(); break; + case R.id.buttonRNG: pushRNG(); break; + case R.id.buttonEqual: pushEqual(); break; + case R.id.buttonPoint: pushPoint(); break; + + + + } + } + + private void pushDigit(char digit) { + if (stringNumber.length() == 1 && stringNumber.charAt(0) == '0') { + if (digit == '0') { + return; + } + stringNumber.setLength(0); + + } + stringNumber.append(digit); + toScreen(stringNumber.toString()); + } + + private void pushOperation(char oper) { + if (isFirst) { + number = Double.parseDouble(stringNumber.toString()); + isFirst = false; + stringNumber.setLength(0); + stringNumber.append('0'); + } + + if (operation != 0) { + getButton(operation).setBackgroundColor(new ResourcesCompat().getColor(getResources(), R.color.orange, null)); + } + getButton(oper).setBackgroundColor(Color.GREEN); + + operation = oper; +// toScreen(String.valueOf(number)); + } + + private void pushPoint() { + if (!stringNumber.toString().contains(".")) { + stringNumber.append('.'); + } + toScreen(stringNumber.toString()); + } + + private void changeSign() { + if (stringNumber.charAt(0) == '-') { + stringNumber.delete(0, 1); + } else { + stringNumber.insert(0, "-"); + } + toScreen(stringNumber.toString()); + } + + private void pushAC() { + number = 0; + clearStringNumber(); + isFirst = true; + getButton(operation).setBackgroundColor(new ResourcesCompat().getColor(getResources(), R.color.orange, null)); + operation = 0; + toScreen(stringNumber.toString()); + } + + private void pushRNG() { + stringNumber = new StringBuilder(Integer.toString(new Random().nextInt(100))); + toScreen(stringNumber.toString()); + } + + private void pushEqual() { + getButton(operation).setBackgroundColor(new ResourcesCompat().getColor(getResources(), R.color.orange, null)); + switch (operation) { + case '+': + number += Double.parseDouble(stringNumber.toString()); + clearStringNumber(); + break; + case '-': + number -= Double.parseDouble(stringNumber.toString()); + clearStringNumber(); + break; + case '*': + number *= Double.parseDouble(stringNumber.toString()); + clearStringNumber(); + break; + case '/': + number /= Double.parseDouble(stringNumber.toString()); + clearStringNumber(); + break; + } + toScreen(String.valueOf(number)); + } + + private void clearStringNumber() { + stringNumber.setLength(0); + stringNumber.append('0'); + } + + private Button getButton(char ch) { + switch (ch) { + case '+': return (Button)findViewById(R.id.buttonAdd); + case '-': return (Button)findViewById(R.id.buttonSub); + case '*': return (Button)findViewById(R.id.buttonMul); + default: return (Button)findViewById(R.id.buttonDiv); + } + } + +} diff --git a/app/src/main/res/layout-land/myscreen.xml b/app/src/main/res/layout-land/myscreen.xml new file mode 100644 index 0000000..9f7459a --- /dev/null +++ b/app/src/main/res/layout-land/myscreen.xml @@ -0,0 +1,216 @@ + + + + + + + + +