Add Expense
Part of the Fundamentals section of Coddy's Kotlin journey. Lesson 86 of 93.
Challenge
MediumIn the previous lesson, you created a menu loop that exits when the user selects option 5. Now, implement the "Add Expense" functionality for option 1.
Modify your program to:
- Create a
MutableList<Double>calledexpensesbefore the loop to store expense amounts - When the user selects option
1:- Print
Enter expense amount: - Read the next input, convert it to
Double, and add it to theexpenseslist - Print
Expense added!
- Print
- Option
5should still printGoodbye!and exit - All other options (2, 3, 4, or invalid) should still print
Invalid option
Input format:
Menu choices and expense amounts as strings. When option 1 is selected, the next input will be the expense amount.
Output format:
The menu, followed by the appropriate response for each action.
Example:
If the inputs are 1, 25.50, 1, 10.00, 3, 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:
Invalid option
--- Expense Tracker ---
1. Add Expense
2. View All Expenses
3. Total and Average
4. Clear All
5. Exit
Choose an option:
Goodbye!Try it yourself
fun main() {
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 == "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