This commit is contained in:
_N0x 2021-06-29 21:01:53 +02:00
parent eb6c30f3cd
commit c62b6123d5
3 changed files with 189 additions and 76 deletions

View File

@ -8,79 +8,159 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
public class FractionCalculator extends BaseObservable{
import java.security.InvalidParameterException;
public class FractionCalculator extends BaseObservable{
// Debugging stuff
private static final String TAG = FractionCalculator.class.getName();
private String denomLeft;
private String enumLeft;
private String denomRight;
private String enumRight;
private String denomResult;
private String enumResult;
public enum ARTIH {ADD, SUBTRACT, MULTIPLICATE, DIVIDE};
private ARTIH arithMethod;
private String centerSign;
private int denomLeft;
private int enumLeft;
private int denomRight;
private int enumRight;
private int denomResult;
private int enumResult;
public FractionCalculator(){
Log.d(TAG, "new FractionCalculator created");
}
/// #### GETTER AND SETTER ####
// Calculate the result
// https://de.wikibooks.org/wiki/C%2B%2B-Programmierung/_Br%C3%BCche
public void calculateResult(){
if(denomLeft == 0 || denomRight == 0){
Log.e(TAG, "Denominator cannot be 0");
} else {
switch (arithMethod) {
case ADD:
denomResult = getLCM(denomLeft, denomRight);
enumResult = enumLeft * (denomResult / denomLeft) + enumRight * (denomResult / denomRight);
break;
case SUBTRACT:
denomResult = getLCM(denomLeft, denomRight);
enumResult = enumLeft * (denomResult / denomLeft) - enumRight * (denomResult / denomRight);
break;
case MULTIPLICATE:
denomResult = denomLeft * denomRight;
enumResult = enumLeft * enumRight;
break;
case DIVIDE:
enumResult = enumLeft * denomRight;
denomResult = denomLeft * enumRight;
break;
}
// update the result variables
notifyPropertyChanged(BR.denomResult);
notifyPropertyChanged(BR.enumResult);
}
}
// ####### HELPER METHODS #######
// calculate greatest common divisor (Euclidean algorithm)
private int getGCD(int a, int b){
if(b==0){
return a;
} else {
return getGCD(b,a % b);
}
}
// calculate least common multiple
private int getLCM(int a, int b){
return (a*b)/getGCD(a,b);
}
// ####### END HELPER METHODS #######
/// ####### GETTER AND SETTER #######
public void setArithMethod(ARTIH arithMethod) {
this.arithMethod = arithMethod;
switch(arithMethod){
case ADD:
centerSign = "+";
break;
case SUBTRACT:
centerSign = "-";
break;
case MULTIPLICATE:
centerSign = "*";
break;
case DIVIDE:
centerSign = "/";
break;
}
// Update the sign in the middle of the screen
notifyPropertyChanged(BR.centerSign);
}
@Bindable
public String getDenomLeft() {
public int getDenomLeft() {
return denomLeft;
}
public void setDenomLeft(String denomLeft) {
public void setDenomLeft(int denomLeft) {
this.denomLeft = denomLeft;
notifyPropertyChanged(BR.denomLeft);
}
@Bindable
public String getEnumLeft() {
public int getEnumLeft() {
return enumLeft;
}
public void setEnumLeft(String enumLeft) {
public void setEnumLeft(int enumLeft) {
this.enumLeft = enumLeft;
notifyPropertyChanged(BR.enumLeft);
}
@Bindable
public String getDenomRight() {
public int getDenomRight() {
return denomRight;
}
public void setDenomRight(String denomRight) {
public void setDenomRight(int denomRight) {
this.denomRight = denomRight;
notifyPropertyChanged(BR.denomRight);
}
@Bindable
public String getEnumRight() {
public int getEnumRight() {
return enumRight;
}
public void setEnumRight(String enumRight) {
public void setEnumRight(int enumRight) {
this.enumRight = enumRight;
notifyPropertyChanged(BR.enumRight);
}
@Bindable
public String getDenomResult() {
public int getDenomResult() {
return denomResult;
}
public void setDenomResult(String denomResult) {
public void setDenomResult(int denomResult) {
this.denomResult = denomResult;
notifyPropertyChanged(BR.denomResult);
}
@Bindable
public String getEnumResult() {
public int getEnumResult() {
return enumResult;
}
public void setEnumResult(String enumResult) {
public void setEnumResult(int enumResult) {
this.enumResult = enumResult;
notifyPropertyChanged(BR.enumResult);
}
@Bindable
public String getCenterSign() {
return centerSign;
}
/// ####### END GETTER AND SETTER #######
}

View File

@ -1,5 +1,6 @@
package io.n0x.android.java_mot_testing;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
@ -16,6 +17,8 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
// Local calculator instance
private FractionCalculator calc;
private int test;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -23,8 +26,8 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
calc = new FractionCalculator();
// set a value for testing
calc.setEnumResult("0");
calc.setDenomResult("0");
calc.setEnumResult(0);
calc.setDenomResult(0);
// Bind instance of FractionCalculator to view
Ex9DataBindingBinding binding = DataBindingUtil.setContentView(this, R.layout.ex9_data_binding);
@ -32,21 +35,34 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
// Button Listeners
findViewById(R.id.button_add).setOnClickListener(this);
findViewById(R.id.button_sub).setOnClickListener(this);
findViewById(R.id.button_mult).setOnClickListener(this);
findViewById(R.id.button_div).setOnClickListener(this);
findViewById(R.id.button_calc).setOnClickListener(this);
// Log create action
Log.d(TAG, "MainActivity started");
}
@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View v) {
if (v.getId() == R.id.button_add) {
// works (without using bindings)
TextView textView = findViewById(R.id.txt_enum_result);
textView.setText("1234");
// work with DataBinding
calc.setDenomLeft("123123123");
switch(v.getId()){
case R.id.button_add:
calc.setArithMethod(FractionCalculator.ARTIH.ADD);
break;
case R.id.button_sub:
calc.setArithMethod(FractionCalculator.ARTIH.SUBTRACT);
break;
case R.id.button_mult:
calc.setArithMethod(FractionCalculator.ARTIH.MULTIPLICATE);
break;
case R.id.button_div:
calc.setArithMethod(FractionCalculator.ARTIH.DIVIDE);
break;
case R.id.button_calc:
calc.calculateResult();
break;
}
}
}

View File

@ -15,131 +15,148 @@
<Button
android:id="@+id/button_add"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="152dp"
android:layout_marginEnd="8dp"
android:text="+"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button_sub"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button_sub"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="152dp"
android:layout_marginEnd="8dp"
android:text="-"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_add" />
app:layout_constraintEnd_toStartOf="@+id/button_mult"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button_mult"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="152dp"
android:layout_marginEnd="8dp"
android:text="*"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_sub" />
app:layout_constraintEnd_toStartOf="@+id/button_div"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button_div"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="152dp"
android:layout_marginEnd="8dp"
android:text="/"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_mult" />
<TextView
android:id="@+id/txt_equals"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:text="="
android:textSize="34sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.181"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_div" />
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txt_enum_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:text="@{calc.enumResult}"
android:layout_marginTop="16dp"
android:text="@{String.valueOf(calc.enumResult)}"
android:textAlignment="center"
android:textSize="34sp"
app:layout_constraintBottom_toTopOf="@+id/txt_denom_result"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_div" />
app:layout_constraintTop_toBottomOf="@+id/button_mult" />
<TextView
android:id="@+id/txt_denom_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:layout_marginBottom="412dp"
android:text="@{calc.denomResult}"
android:text="@{String.valueOf(calc.denomResult)}"
android:textAlignment="center"
android:textSize="34sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt_enum_result" />
<EditText
android:text="@{calc.enumRight}"
android:id="@+id/enumRight"
android:layout_width="140dp"
android:layout_height="50dp"
android:layout_marginTop="32dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:ems="10"
android:gravity="center_horizontal"
android:hint="Zähler"
android:inputType="numberSigned"
android:text="@={`` + calc.enumRight}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/button_add"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:text="@{calc.enumRight}"
android:id="@+id/enumLeft"
android:layout_width="140dp"
android:layout_height="50dp"
android:layout_marginTop="32dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:gravity="center_horizontal"
android:hint="Zähler"
android:inputType="numberSigned"
app:layout_constraintEnd_toStartOf="@+id/button_add"
android:text="@={`` + calc.enumLeft}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:text="@{calc.denomLeft}"
android:id="@+id/denomLeft"
android:layout_width="140dp"
android:layout_height="50dp"
android:layout_marginTop="32dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:gravity="center_horizontal"
android:hint="Nenner"
android:inputType="number"
app:layout_constraintEnd_toStartOf="@+id/button_mult"
android:text="@={`` + calc.denomLeft}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/enumLeft" />
<EditText
android:text="@{calc.denomRight}"
android:id="@+id/denomRight"
android:layout_width="140dp"
android:layout_height="50dp"
android:layout_marginTop="32dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:ems="10"
android:gravity="center_horizontal"
android:hint="Nenner"
android:inputType="number"
android:text="@={`` + calc.denomRight}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/button_mult"
app:layout_constraintTop_toBottomOf="@+id/enumRight" />
<Button
android:id="@+id/button_calc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="236dp"
android:text="="
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/centerSign"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@{calc.centerSign}"
android:textAlignment="center"
android:textSize="60sp"
app:layout_constraintBottom_toTopOf="@+id/button_mult"
app:layout_constraintEnd_toStartOf="@+id/enumRight"
app:layout_constraintStart_toEndOf="@+id/enumLeft"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>