Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

homework1 #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
26 changes: 26 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "com.example.maksim.calculator"
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.2.1'
}
17 changes: 17 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in C:\Users\Maksim\AppData\Local\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 *;
#}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.maksim.calculator;

import android.app.Application;
import android.test.ApplicationTestCase;

/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
20 changes: 20 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.maksim.calculator">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
168 changes: 168 additions & 0 deletions app/src/main/java/com/example/maksim/calculator/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package com.example.maksim.calculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class MainActivity extends AppCompatActivity {
enum State {
FIRST, OPERATION, SECOND, RESULT
}

private TextView text;
private State state;
private char operation;
private StringBuffer result;
private Double leftOperand, rightOperand;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

text = (TextView) findViewById(R.id.screen);
if (savedInstanceState == null) {
result = new StringBuffer("0");
leftOperand = new Double(0);
rightOperand = new Double(0);
state = State.FIRST;
operation = '\0';
} else {
result = new StringBuffer(savedInstanceState.getString("result"));
leftOperand = new Double(savedInstanceState.getDouble("leftOperand"));
rightOperand = new Double(savedInstanceState.getDouble("rightOperand"));
state = (State) savedInstanceState.getSerializable("state");
operation = savedInstanceState.getChar("operation");
}
text.setText(result.toString());
}

public void onDigitClick(View view) {
if (state == State.OPERATION) {
state = State.SECOND;
leftOperand = Double.parseDouble(result.toString());
result = new StringBuffer("0");
}
if (state == State.RESULT)
this.onClearClick(view);

String digit = ((TextView) view).getText().toString();

if (state == State.FIRST || state == State.SECOND) {
result.append(digit);
if (result.charAt(0) == '0' && result.charAt(1) != '.') {
result.deleteCharAt(0);
}
}
text.setText(result.toString());
}

public void onDotClick(View view) {
if (state == State.RESULT) {
result = new StringBuffer("0.");
state = State.FIRST;
text.setText(result.toString());
return;
}
if (state == State.OPERATION) {
state = State.SECOND;
leftOperand = Double.parseDouble(result.toString());
result = new StringBuffer("0.");
text.setText(result.toString());
return;
}
if (result.toString().contains("."))
return;
result.append('.');
text.setText(result.toString());
}

public void onClearClick(View view) {
state = State.FIRST;
leftOperand = 0.0;
rightOperand = 0.0;
result = new StringBuffer("0");
text.setText(result.toString());
}

public void onChangeSignClick(View view) {
if (state == State.OPERATION)
return;
if (Double.valueOf(result.toString()) == 0.0)
return;
if (result.charAt(0) != '-')
result.insert(0, '-');
else
result.deleteCharAt(0);
text.setText(result.toString());
}

public void onDeleteClick(View view) {
if (state == State.OPERATION) {
operation = '\0';
state = State.FIRST;
}
if (result.length() > 1) {
result.deleteCharAt(result.length() - 1);
} else {
result = new StringBuffer("0");
}
text.setText(result.toString());
}

public void onResultClick(View view) {
if (state == State.FIRST)
return;
if (state == State.SECOND || state == State.OPERATION) {
rightOperand = Double.parseDouble(result.toString());
}
switch (operation) {
case '\0':
break;
case '+':
leftOperand += rightOperand;
break;
case '–':
leftOperand -= rightOperand;
break;
case '×':
leftOperand *= rightOperand;
break;
case '÷':
leftOperand /= rightOperand;
break;
default:
break;
}
leftOperand = new BigDecimal(leftOperand).setScale(5, RoundingMode.UP).doubleValue();

result = new StringBuffer(leftOperand.toString());
state = State.RESULT;
text.setText(result.toString());
}

public void onOperationClick(View view) {
if (state == State.FIRST) {
leftOperand = Double.parseDouble(result.toString());
}
if (state == State.SECOND) {
onResultClick(view);
}
state = State.OPERATION;
operation = ((TextView) view).getText().toString().charAt(0);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("result", result.toString());
outState.putChar("operation", operation);
outState.putSerializable("state", state);
outState.putDouble("leftOperand", leftOperand.doubleValue());
outState.putDouble("rightOperand", rightOperand.doubleValue());
}
}
Loading