diff --git a/app/src/main/java/io/n0x/android/java_mot_testing/FractionCalculator.java b/app/src/main/java/io/n0x/android/java_mot_testing/FractionCalculator.java index b0a904a..5970546 100644 --- a/app/src/main/java/io/n0x/android/java_mot_testing/FractionCalculator.java +++ b/app/src/main/java/io/n0x/android/java_mot_testing/FractionCalculator.java @@ -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 ####### } diff --git a/app/src/main/java/io/n0x/android/java_mot_testing/MainActivity.java b/app/src/main/java/io/n0x/android/java_mot_testing/MainActivity.java index 865efe6..4e6e6f9 100644 --- a/app/src/main/java/io/n0x/android/java_mot_testing/MainActivity.java +++ b/app/src/main/java/io/n0x/android/java_mot_testing/MainActivity.java @@ -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; } } } \ No newline at end of file diff --git a/app/src/main/res/layout/ex9_data_binding.xml b/app/src/main/res/layout/ex9_data_binding.xml index 6f2a305..0419486 100644 --- a/app/src/main/res/layout/ex9_data_binding.xml +++ b/app/src/main/res/layout/ex9_data_binding.xml @@ -15,131 +15,148 @@