Clear All
Part of the Fundamentals section of Coddy's Kotlin journey. Lesson 89 of 93.
Challenge
MediumIn the previous lesson, you implemented the "Total and Average" functionality. Now, implement the "Clear All" functionality for option 4.
Modify your program to handle option 4:
- Clear all expenses from the
expenseslist - Print
All expenses cleared!
All other options should continue working as before:
- Option
1: Add expense - Option
2: View all expenses - Option
3: Total and average - Option
5: PrintGoodbye!and exit - Invalid options: Print
Invalid option
Input format:
Menu choices and expense amounts as strings.
Output format:
The menu, followed by the appropriate response for each action.
Example:
If the inputs are 1, 50.0, 1, 25.0, 2, 4, 2, and 5, the output should be:
--- Expense Tracker ---
1. Add Expense
2. View All Expenses
3. Total and Average
4. Clear All
5. Exit
Choose an option:
Enter expense amount:
Expense added!
--- Expense Tracker ---
1. Add Expense
2. View All Expenses
3. Total and Average
4. Clear All
5. Exit
Choose an option:
Enter expense amount:
Expense added!
--- Expense Tracker ---
1. Add Expense
2. View All Expenses
3. Total and Average
4. Clear All
5. Exit
Choose an option:
Your expenses:
- $50.0
- $25.0
--- Expense Tracker ---
1. Add Expense
2. View All Expenses
3. Total and Average
4. Clear All
5. Exit
Choose an option:
All expenses cleared!
--- Expense Tracker ---
1. Add Expense
2. View All Expenses
3. Total and Average
4. Clear All
5. Exit
Choose an option:
No expenses recorded.
--- Expense Tracker ---
1. Add Expense
2. View All Expenses
3. Total and Average
4. Clear All
5. Exit
Choose an option:
Goodbye!Hint: Use the clear() method on your mutable list to remove all elements.
Try it yourself
fun main() {
val expenses = mutableListOf<Double>()
while (true) {
println("--- Expense Tracker ---")
println("1. Add Expense")
println("2. View All Expenses")
println("3. Total and Average")
println("4. Clear All")
println("5. Exit")
println("Choose an option:")
val choice = readLine()
if (choice == "1") {
println("Enter expense amount:")
val amount = readLine()!!.toDouble()
expenses.add(amount)
println("Expense added!")
} else if (choice == "2") {
if (expenses.isEmpty()) {
println("No expenses recorded.")
} else {
println("Your expenses:")
for (expense in expenses) {
println("- \$$expense")
}
}
} else if (choice == "3") {
if (expenses.isEmpty()) {
println("No expenses recorded.")
} else {
val total = expenses.sum()
val average = total / expenses.size
println("Total: \$$total")
println("Average: \$$average")
}
} else if (choice == "5") {
println("Goodbye!")
break
} else {
println("Invalid option")
}
}
}All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorAugmented AssignmentComparison OperatorsRecap - Simple Math7Basic IO
Println FunctionString TemplatesReadLine InputType ConversionRecap - Years Until RetirementRecap - True or False10Functions
Declare A FunctionParameters And ArgumentsReturn ValuesNamed ArgumentsDefault ValuesSingle Expression FunctionsRecap - Sigma FunctionRecap - Validation Function2Variables
Val vs VarType InferenceNumbersStringBooleanNaming ConventionsRecap - Initialize Variables5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Logical Operators Part 3Ternary With If ExpressionRecap - Simple Logic8Bill Split Calculator
Welcome MessageGetting Input3Nullability
What Is Null SafetyNullable TypesSafe Call OperatorElvis OperatorFunction Challenge BasicsNot Null AssertionRecap - Safe Access6Decision Making
If StatementIf - ElseIf As An ExpressionWhen ExpressionWhen With RangesRecap - Simple Calculator9Loops
For LoopWhile LoopDo-While LoopBreakContinueRanges In LoopsNested LoopRecap - FactorialRecap - Dynamic Input12Lists Basics
List vs MutableListAccessing ElementsModifying ListsList MethodsPair And TripleRecap - Product ListRecap - Reversed List15Daily Expense Tracker
Project OverviewExit The ProgramPractice on your own: Kotlin playground