Menu
Coddy logo textTech

Error Handling

Part of the Fundamentals section of Coddy's Ruby journey — lesson 85 of 88.

challenge icon

Challenge

Easy

Complete the Daily Expense Tracker by adding error handling for invalid menu options.

Your program should:

  1. Initialize an empty array called expenses
  2. Create a loop do that displays the menu and reads user input
  3. When the user enters "1", prompt for the expense name and amount, create a hash with :name and :amount keys, add it to the expenses array, and print Expense added!
  4. When the user enters "2", display all expenses or a message if none exist
  5. When the user enters "3", calculate and display the total and average of all expenses, or a message if none exist
  6. When the user enters "4", clear all expenses from the array and print All expenses cleared!
  7. When the user enters "5", print Goodbye! and exit the loop
  8. For any other input, print: Invalid option. Please try again.

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: 

For example, if the inputs are 7, abc, then 5 (two invalid options, then exit), the output should be:


--- Expense Tracker ---
1. Add Expense
2. View Expenses
3. Total & Average
4. Clear All
5. Exit
Choose an option: Invalid option. Please try again.

--- Expense Tracker ---
1. Add Expense
2. View Expenses
3. Total & Average
4. Clear All
5. Exit
Choose an option: Invalid option. Please try again.

--- 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, Coffee, 4.5, 6, 2, then 5 (add expense, invalid option, view, exit), 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: Invalid option. Please try again.

--- Expense Tracker ---
1. Add Expense
2. View Expenses
3. Total & Average
4. Clear All
5. Exit
Choose an option: 
--- Your Expenses ---
Coffee: $4.5

--- 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
    expenses << { name: name, amount: amount }
    puts "Expense added!"
  elsif choice == "2"
    if expenses.empty?
      puts "No expenses recorded yet."
    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 = expenses.sum { |expense| expense[:amount] }
      average = total / expenses.length
      puts "Total: $#{total}"
      puts "Average: $#{average}"
    end
  elsif choice == "4"
    expenses.clear
    puts "All expenses cleared!"
  elsif choice == "5"
    puts "Goodbye!"
    break
  end
  
  puts ""
end

All lessons in Fundamentals