From 80808fdf9ffbf8ee7004c8877701ec63a10627ad Mon Sep 17 00:00:00 2001 From: Allison Silva Date: Wed, 14 Dec 2022 15:58:45 -0300 Subject: [PATCH] Fix #1 all the operations are working! but we still can't concatenate operations properly --- .../stalkerfish/thecalculator/MainActivity.kt | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/app/src/main/java/com/stalkerfish/thecalculator/MainActivity.kt b/app/src/main/java/com/stalkerfish/thecalculator/MainActivity.kt index d7b0fca..ce4ceba 100644 --- a/app/src/main/java/com/stalkerfish/thecalculator/MainActivity.kt +++ b/app/src/main/java/com/stalkerfish/thecalculator/MainActivity.kt @@ -1,5 +1,6 @@ package com.stalkerfish.thecalculator +import android.annotation.SuppressLint import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.View @@ -52,6 +53,7 @@ class MainActivity : AppCompatActivity() { } } + @SuppressLint("SetTextI18n") fun onEqual(view: View){ if (lastWasNumeric){ // checks if the last digit is a number, so it can perform the operation @@ -79,6 +81,45 @@ class MainActivity : AppCompatActivity() { tvInput?.text = (one.toDouble() - two.toDouble()).toString() // convert the strings to Double, do the operation, // convert back to strings, and set to tvInput, all at the same time + } else if (tvValue.contains("+")){ + + val splitValue = tvValue.split("+") // Split the string using the operator sign as a delimiter + + var one = splitValue[0] // the split string comes in arrays + var two = splitValue[1] + + if(prefix.isNotEmpty()){ + one = prefix + one + } + + tvInput?.text = (one.toDouble() + two.toDouble()).toString() // convert the strings to Double, do the operation, + // convert back to strings, and set to tvInput, all at the same time + } else if (tvValue.contains("/")){ + + val splitValue = tvValue.split("/") // Split the string using the operator sign as a delimiter + + var one = splitValue[0] // the split string comes in arrays + var two = splitValue[1] + + if(prefix.isNotEmpty()){ + one = prefix + one + } + + tvInput?.text = (one.toDouble() / two.toDouble()).toString() // convert the strings to Double, do the operation, + // convert back to strings, and set to tvInput, all at the same time + } else if (tvValue.contains("*")){ + + val splitValue = tvValue.split("*") // Split the string using the operator sign as a delimiter + + var one = splitValue[0] // the split string comes in arrays + var two = splitValue[1] + + if(prefix.isNotEmpty()){ + one = prefix + one + } + + tvInput?.text = (one.toDouble() * two.toDouble()).toString() // convert the strings to Double, do the operation, + // convert back to strings, and set to tvInput, all at the same time } }catch (e: ArithmeticException){