Clear All
Part of the Fundamentals section of Coddy's Ruby journey — lesson 84 of 88.
Challenge
EasyBuild upon your expense tracker by adding the "Clear All" functionality.
Your program should:
- Initialize an empty array called
expenses - Create a
loop dothat displays the menu and reads user input - When the user enters
"1", prompt for the expense name and amount, create a hash with:nameand:amountkeys, add it to theexpensesarray, and printExpense added! - When the user enters
"2", display all expenses or a message if none exist - When the user enters
"3", calculate and display the total and average of all expenses, or a message if none exist - When the user enters
"4", clear all expenses from the array and print a confirmation message - When the user enters
"5", printGoodbye!and exit the loop
The menu should display exactly as before:
--- Expense Tracker ---
1. Add Expense
2. View Expenses
3. Total & Average
4. Clear All
5. Exit
Choose an option: When option "4" is selected, use the clear method on the expenses array and print: All expenses cleared!
For example, if the inputs are 1, Coffee, 5.0, 4, 2, then 5 (add an expense, clear all, then view), the output should be:
--- Expense Tracker ---
1. Add Expense
2. View Expenses
3. Total & Average
4. Clear All
5. Exit
Choose an option: Expense name: Amount: Expense added!
--- Expense Tracker ---
1. Add Expense
2. View Expenses
3. Total & Average
4. Clear All
5. Exit
Choose an option: All expenses cleared!
--- Expense Tracker ---
1. Add Expense
2. View Expenses
3. Total & Average
4. Clear All
5. Exit
Choose an option: No expenses recorded yet.
--- Expense Tracker ---
1. Add Expense
2. View Expenses
3. Total & Average
4. Clear All
5. Exit
Choose an option: Goodbye!If the inputs are 1, Lunch, 12.0, 1, Dinner, 25.0, 3, 4, 3, then 5 (add two expenses, check total, clear, check total again), the output should be:
--- Expense Tracker ---
1. Add Expense
2. View Expenses
3. Total & Average
4. Clear All
5. Exit
Choose an option: Expense name: Amount: Expense added!
--- Expense Tracker ---
1. Add Expense
2. View Expenses
3. Total & Average
4. Clear All
5. Exit
Choose an option: Expense name: Amount: Expense added!
--- Expense Tracker ---
1. Add Expense
2. View Expenses
3. Total & Average
4. Clear All
5. Exit
Choose an option: Total: $37.0
Average: $18.5
--- Expense Tracker ---
1. Add Expense
2. View Expenses
3. Total & Average
4. Clear All
5. Exit
Choose an option: All expenses cleared!
--- Expense Tracker ---
1. Add Expense
2. View Expenses
3. Total & Average
4. Clear All
5. Exit
Choose an option: No expenses to calculate.
--- Expense Tracker ---
1. Add Expense
2. View Expenses
3. Total & Average
4. Clear All
5. Exit
Choose an option: Goodbye!Try it yourself
expenses = []
loop do
puts "--- Expense Tracker ---"
puts "1. Add Expense"
puts "2. View Expenses"
puts "3. Total & Average"
puts "4. Clear All"
puts "5. Exit"
print "Choose an option: "
choice = gets.chomp
if choice == "1"
print "Expense name: "
name = gets.chomp
print "Amount: "
amount = gets.chomp.to_f
expense = { name: name, amount: amount }
expenses << expense
puts "Expense added!"
elsif choice == "2"
if expenses.empty?
puts "No expenses recorded."
else
expenses.each do |expense|
puts "#{expense[:name]}: $#{expense[:amount]}"
end
end
elsif choice == "3"
if expenses.empty?
puts "No expenses to calculate."
else
total = 0
expenses.each do |expense|
total += expense[:amount]
end
average = total / expenses.length
puts "Total: $#{total}"
puts "Average: $#{average}"
end
elsif choice == "5"
puts "Goodbye!"
break
end
puts ""
endAll lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 42Variables and Data Types
Numbers and VariablesString Data TypeBoolean Data TypeSymbol Data TypeChecking Data TypesNaming ConventionsRecap - Variable Creation8Loops
For Loop with RangesWhile LoopBreakNextRecap - FactorialTimes LoopUntil LoopNested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators6Basic IO
Output with putsOutput with print and pOutput With VariablesInput with getsChomp MethodType ConversionRecap - Age CalculatorRecap - True or False9Methods
Defining a MethodMethod ParametersReturn ValuesRecap - Sigma MethodRecap - Validation MethodDefault Parameter Values15Daily Expense Tracker
Project OverviewExit The Program