In this challenge, you are tasked with completing a simple Android application that calculates income tax. You are provided with a partially implemented MainActivity and must complete both the core tax calculation logic and the user interface wiring.
Your goal is to implement the missing logic to ensure the calculation works accurately for all income values and tax rates, and that the app correctly responds to user interactions.
You are given an Android project with a MainActivity.kt file containing a shell of the application logic.
The calculateTax function inside the companion object accepts the following parameters:
income:Double- The total income.taxRate:Double- The tax percentage to be applied (e.g.,10.0for 10%,12.5for 12.5%).
You need to perform the following operations:
- Tax Calculation: Implement the
calculateTaxfunction. It must return the computed tax amount and the final income (income minus tax). - UI Initialization: Uncomment and initialize the UI views (
etIncome,etTaxRate,btnCalculate,tvTaxAmount,tvFinalIncome) in theonCreatemethod. - Event Handling: Set a click listener on the
btnCalculatebutton to retrieve the parsed input values, perform the calculation, and update the text labels with the results.
The calculateTax function should return a Pair<Double, Double>:
first(Tax): ADoublerepresenting the calculated tax amount.second(Final Income): ADoublerepresenting the remaining income after the tax is deducted.
Run the unit tests:
./gradlew testThe unit tests are designed to fail initially. Your task is to modify MainActivity.kt until all tests in TaxCalculatorTest.kt pass.
UI Wiring
Uncomment the variables and carefully assign them using findViewById.
Input Parsing
Ensure you parse the text from the EditText boxes to Double correctly before doing any calculations.
Tax Calculation Logic
Income can be decimal.
The tax calculation should support 0.0 edge cases or percentages up to 100.0.
- Pay close attention to how the
taxRateis treated. AtaxRateof10.0means 10% (i.e.10.0 / 100). - Review the logic for calculating the final income. The sum of the
taxandfinal incomemust exactly equal the originalincome. - Think about edge cases:
0.0income,0.0tax rate, or decimal math. - Remember: The code is intentionally left partially unimplemented. Your structural problem solving and fundamental Android UI skills are key!