Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
13 kB
2
Indexable
Never
package com.example.missmytrip

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import kotlinx.android.synthetic.main.activity_currency_converter.*
import kotlinx.android.synthetic.main.activity_entries_add.*
import kotlinx.android.synthetic.main.activity_entries_add.currency_spinner
import kotlinx.android.synthetic.main.activity_expense_entries.*
import kotlinx.android.synthetic.main.activity_view_statistics.*
import java.io.File

class View_Statistics_Activity : AppCompatActivity() {

    var currencyData = "HKD"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_view_statistics)

        //Initialize Spinner for Currency Selection
        val currency_data = arrayListOf("HKD", "USD", "RMB")
        val currency_adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, currency_data)
        spinner_Currency.adapter = currency_adapter
        spinner_Currency.setSelection(0)
        spinner_Currency.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>, view: View, pos: Int, id: Long) {
                currencyData = parent.getItemAtPosition(pos).toString()
            }
            override fun onNothingSelected(parent: AdapterView<*>) {}
        }

        val path = getFilesDir();
        val file = File(path, "ExpenseEntries.txt");

        var delimiter1 = ";"
        var delimiter2 = "\n"
        var temp_List: MutableList<String> = file.readText().split(delimiter1 , delimiter2).toMutableList()
        var list_Cate: MutableList<String> = mutableListOf()
        var list_Amount: MutableList<String> = mutableListOf()
        var list_Currency: MutableList<String> = mutableListOf()

        for (i in temp_List.indices) {
            if (i % 9 == 2)   //Category item
                list_Cate.add(temp_List.elementAt(i))
            if (i % 9 == 4)   //Amount item
                list_Amount.add(temp_List.elementAt(i))
            if (i % 9 == 7)   //Currency item
                list_Currency.add(temp_List.elementAt(i))
        }

        //Count the total number of entries
        NumOfExpense_Value.text = list_Amount.size.toString()

        //Find the largest expense in all records
        //Initialize values of largest amount with the first element in list_Amount to prevent error
        var largestAmount = list_Amount.elementAt(0).toDouble()
        if (list_Currency.elementAt(0) != "HKD") {
            if (list_Currency.elementAt(0) == "USD") {
                largestAmount = largestAmount * 7.80
            } else {
                largestAmount = largestAmount * 1.22
            }
        }
        for (i in list_Amount.indices) {
            var amountInHKD: Double
            if (list_Currency.elementAt(i) != "HKD") {
                if (list_Currency.elementAt(i) == "USD") {
                    amountInHKD = list_Amount.elementAt(i).toDouble() * 7.80
                } else {
                    amountInHKD = list_Amount.elementAt(i).toDouble() * 1.22
                }
            } else {
                amountInHKD = list_Amount.elementAt(i).toDouble()
            }
            if (amountInHKD > largestAmount) {
                largestAmount = amountInHKD
            }
        }
        //Display results in terms of selected currency
        if (currencyData == "HKD") {
            Largest_Expense_Value.text = "$currencyData " + String.format("%.2f", largestAmount)
        } else if (currencyData == "USD") {
            Largest_Expense_Value.text = "$currencyData " + String.format("%.2f", (largestAmount * 0.13))
        } else {
            Largest_Expense_Value.text = "$currencyData " + String.format("%.2f", (largestAmount * 0.82))
        }


        //Find the smallest expense in all records
        var smallestAmount = list_Amount.elementAt(0).toDouble()
        if (list_Currency.elementAt(0) != "HKD") {
            if (list_Currency.elementAt(0) == "USD") {
                smallestAmount = smallestAmount * 7.80
            } else {
                smallestAmount = smallestAmount * 1.22
            }
        }
        for (i in list_Amount.indices) {
            var amountInHKD: Double
            if (list_Currency.elementAt(i) != "HKD") {
                if (list_Currency.elementAt(i) == "USD") {
                    amountInHKD = list_Amount.elementAt(i).toDouble() * 7.80
                } else {
                    amountInHKD = list_Amount.elementAt(i).toDouble() * 1.22
                }
            } else {
                amountInHKD = list_Amount.elementAt(i).toDouble()
            }
            if (amountInHKD < smallestAmount) {
                smallestAmount = amountInHKD
            }
        }
        //Display results in terms of selected currency
        if (currencyData == "HKD") {
            Smallest_Expense_Value.text = "$currencyData " + String.format("%.2f", smallestAmount)
        } else if (currencyData == "USD") {
            Smallest_Expense_Value.text = "$currencyData " + String.format("%.2f", (smallestAmount * 0.13))
        } else {
            Smallest_Expense_Value.text = "$currencyData " + String.format("%.2f", (smallestAmount * 0.82))
        }

        //Calculate Sum and Average Amount of Expense
        var avgAmount = 0.0
        var sumOfAmountInHKD = 0.0
        for (i in list_Amount.indices) {
            var AmountInHKD: Double
            if (list_Currency.elementAt(i) != "HKD") {
                if (list_Currency.elementAt(i) == "USD") {
                    AmountInHKD = list_Amount.elementAt(i).toDouble() * 7.80
                } else {
                    AmountInHKD = list_Amount.elementAt(i).toDouble() * 1.22
                }
            } else {
                AmountInHKD = list_Amount.elementAt(i).toDouble()
            }
            sumOfAmountInHKD += AmountInHKD
        }
        avgAmount = sumOfAmountInHKD / list_Amount.size
        //Display Sum results in terms of selected currency
        if (currencyData == "HKD") {
            Sum_Expense_Value.text = "$currencyData " + String.format("%.2f", sumOfAmountInHKD)
        } else if (currencyData == "USD") {
            Sum_Expense_Value.text = "$currencyData " + String.format("%.2f", (sumOfAmountInHKD * 0.13))
        } else {
            Sum_Expense_Value.text = "$currencyData " + String.format("%.2f", (sumOfAmountInHKD * 0.82))
        }
        //Display Average results in terms of selected currency
        if (currencyData == "HKD") {
            Avg_Expense_Value.text = "$currencyData " + String.format("%.2f", avgAmount)
        } else if (currencyData == "USD") {
            Avg_Expense_Value.text = "$currencyData " + String.format("%.2f", (avgAmount * 0.13))
        } else {
            Avg_Expense_Value.text = "$currencyData " + String.format("%.2f", (avgAmount * 0.82))
        }


        //Find out category with largest proportion of expense
        val file_Category = File(path, "expense_category.txt");
        val category_List: MutableList<String> = file_Category.readText().split("\n").toMutableList()   //category_List stored types of categories in the record
        category_List.removeLast()
        var categoryTotalAmount_List: MutableList<Double> = mutableListOf()
        var tempAmountInHKD = 0.0
        for (i in category_List.indices) {
            var totalOfThisCategory = 0.0
            for (j in list_Amount.indices) {
                if (list_Cate.elementAt(j) == category_List.elementAt(i)) {
                    //Convert HKD first for calculation
                    if (list_Currency.elementAt(i) != "HKD") {
                        if (list_Currency.elementAt(i) == "USD") {
                            tempAmountInHKD = list_Amount.elementAt(i).toDouble() * 7.80
                        } else {
                            tempAmountInHKD = list_Amount.elementAt(i).toDouble() * 1.22
                        }
                    } else {
                        tempAmountInHKD = list_Amount.elementAt(i).toDouble()
                    }
                    totalOfThisCategory += tempAmountInHKD
                }
            }
            categoryTotalAmount_List.add(totalOfThisCategory)
        }
        var largestCate = category_List.elementAt(0)
        var smallestCate = category_List.elementAt(0)
        var largestCateAmount = categoryTotalAmount_List.elementAt(0)
        var smallestCateAmount = categoryTotalAmount_List.elementAt(0)
        for (k in categoryTotalAmount_List.indices) {
            if (categoryTotalAmount_List.elementAt(k) > largestCateAmount) {
                largestCateAmount = categoryTotalAmount_List.elementAt(k)
                largestCate = category_List.elementAt(k)
            } else if (categoryTotalAmount_List.elementAt(k) == largestCateAmount) {
                if (largestCate != category_List.elementAt(k))
                    largestCate = "$largestCate, ${category_List.elementAt(k)}"
            }
            if (categoryTotalAmount_List.elementAt(k) < smallestCateAmount) {
                smallestCateAmount = categoryTotalAmount_List.elementAt(k)
                smallestCate = category_List.elementAt(k)
            } else if (categoryTotalAmount_List.elementAt(k) == smallestCateAmount) {
                if (smallestCate != category_List.elementAt(k))
                    smallestCate = "$smallestCate, ${category_List.elementAt(k)}"
            }
        }
        val LargestCatePercent: Double = (largestCateAmount / sumOfAmountInHKD) * 100
        val SmallestCatePercent: Double = (smallestCateAmount / sumOfAmountInHKD) * 100
        Cate_Largest_Value.text = largestCate + " (${String.format("%.2f", LargestCatePercent)}%)"
        Cate_Smallest_Value.text = smallestCate + " (${String.format("%.2f", SmallestCatePercent)}%)"
    }

    fun changeViewingCurrency(view: View) {

        var currency_Selected = currencyData
        var originalCurrency = Sum_Expense_Value.text.take(3)
        var smallest_Expense = Smallest_Expense_Value.text.drop(4).toString().toDouble()
        var largest_Expense = Largest_Expense_Value.text.drop(4).toString().toDouble()
        var Sum = Sum_Expense_Value.text.drop(4).toString().toDouble()
        var Average = Avg_Expense_Value.text.drop(4).toString().toDouble()

        var ExRate_RMBUSD = 0.16
        var ExRate_USDRMB = 6.37
        var ExRate_HKDRMB = 0.82
        var ExRate_HKDUSD = 0.13

        if (currency_Selected == "HKD") {
            finish();
            startActivity(getIntent());
        } else if (currency_Selected == "USD") {
            if (originalCurrency == "HKD") {
                Smallest_Expense_Value.text = "$currency_Selected " + String.format("%.2f", smallest_Expense * ExRate_HKDUSD)
                Largest_Expense_Value.text = "$currency_Selected " + String.format("%.2f", largest_Expense * ExRate_HKDUSD)
                Sum_Expense_Value.text = "$currency_Selected " + String.format("%.2f", Sum * ExRate_HKDUSD)
                Avg_Expense_Value.text = "$currency_Selected " + String.format("%.2f", Average * ExRate_HKDUSD)
            } else if (originalCurrency == "RMB") {
                Smallest_Expense_Value.text = "$currency_Selected " + String.format("%.2f", smallest_Expense * ExRate_RMBUSD)
                Largest_Expense_Value.text = "$currency_Selected " + String.format("%.2f", largest_Expense * ExRate_RMBUSD)
                Sum_Expense_Value.text = "$currency_Selected " + String.format("%.2f", Sum * ExRate_RMBUSD)
                Avg_Expense_Value.text = "$currency_Selected " + String.format("%.2f", Average * ExRate_RMBUSD)
            }
        } else if (currency_Selected == "RMB") {
            if (originalCurrency == "USD") {
                Smallest_Expense_Value.text = "$currency_Selected " + String.format("%.2f", smallest_Expense * ExRate_USDRMB)
                Largest_Expense_Value.text = "$currency_Selected " + String.format("%.2f", largest_Expense * ExRate_USDRMB)
                Sum_Expense_Value.text = "$currency_Selected " + String.format("%.2f", Sum * ExRate_USDRMB)
                Avg_Expense_Value.text = "$currency_Selected " + String.format("%.2f", Average * ExRate_USDRMB)
            } else if (originalCurrency == "HKD") {
                Smallest_Expense_Value.text = "$currency_Selected " + String.format("%.2f", smallest_Expense * ExRate_HKDRMB)
                Largest_Expense_Value.text = "$currency_Selected " + String.format("%.2f", largest_Expense * ExRate_HKDRMB)
                Sum_Expense_Value.text = "$currency_Selected " + String.format("%.2f", Sum * ExRate_HKDRMB)
                Avg_Expense_Value.text = "$currency_Selected " + String.format("%.2f", Average * ExRate_HKDRMB)
            }
        }

    }

}