Trying to execute a method in another script when a selection in the menu is made

Viewed 24

I'm new to programming and I'm trying to create a basic finance tracking terminal application. I am trying to get the method to run that is in another script when I make a menu selection. It brings me to the options "Add Income" and "Deduct Expenses" when I select "Make changes to your balance" but when I select "Add Income" or "Deduct Expenses" it does not execute the either method. It worked before but not entirely as intended, I've made minor changes and now it has stopped working and it's got me lost.

What is it that I am doing wrong and how can I do this better?

index.rb

require "tty-prompt"
require_relative ("./income_and_expenses.rb")
$prompt = TTY::Prompt.new

selection = ""

puts "Welcome to FinView, a terminal application that helps you track your finances.
With the help of FinView you are able to document your income and expenses."

def main_menu
    return $prompt.select("What would you like to do?",
    ["Make changes to your balance", 
    "View current balance", 
    "View breakdown of income and expenses", 
    "Create or delete a category", 
    "Exit"])
end

income_expense_selection = ""
def income_and_expenses_menu
    return $prompt.select("What would you like to do?",
    ["Add Income",
    "Deduct Expenses",
    "Return to Main Menu"])
end

while selection != "Exit"
    selection = main_menu
    system "clear"
    case selection
    when "Make changes to your balance"
        income_and_expenses_menu
    when "View current balance"
        puts "balance"
    when "View breakdown of income and expenses"
        puts "breakdown"
    when "Create or delete a category"
        puts "category"
    when "Exit"
        exit
    end
end

while income_expense_selection != "Return to Main Menu"
    income_expense_selection = income_and_expenses_menu
    system "clear"
    case income_expense_selection
    when "Add Income"
       income 
    when "Deduct Expenses"
        expenses
    when "Return to Main Menu"
        main_menu
    end
end

The 2nd script I am trying to access is

income_and_expenses.rb

def expenses
    categories_hash = {
        :Bills => 0,
        :Utilities => 0,
        :Groceries => 0
    }

    puts "Type in the category you would like add expenses to"
    puts categories_hash.keys
    chosen_category = gets.chomp.capitalize

    if chosen_category == "Bills"
        puts "What amount?"
        expense_amount = gets.chomp.to_i
        categories_hash = expense_amount
    end
    puts "Your expenses for #{:Bills} has included the amount of #{expense_amount}"
end

def income
    puts "Please enter your income"
    balance = gets.chomp.to_i
    puts "Your added income is #{balance}"
end
1 Answers

The problem is not with the require, that's fine. The problem is that you never start the second while loop.

This only renders the new menu, which on it's turn returns the selection but than nothing is done with that value. So the first loop basically continues, instead of starting the second one.

when "Make changes to your balance"
    income_and_expenses_menu # Shows menu, but does nothing else

What you can do is to move the second loop to a method and call that method instead. Note that you do have to change income_expense_selection to an instance variable @income_expense_selection. Here's a working example.

require "tty-prompt"
require ("./income_and_expenses.rb")
$prompt = TTY::Prompt.new

selection = ""

puts "Welcome to FinView, a terminal application that helps you track your finances.
With the help of FinView you are able to document your income and expenses."

def main_menu
    return $prompt.select("What would you like to do?",
    ["Make changes to your balance",
    "View current balance",
    "View breakdown of income and expenses",
    "Create or delete a category",
    "Exit"])
end

@income_expense_selection = ""
def income_and_expenses_menu
    return $prompt.select("What would you like to do?",
    ["Add Income",
    "Deduct Expenses",
    "Return to Main Menu"])
end

def income_and_expenses_state
    while @income_expense_selection != "Return to Main Menu"
        @income_expense_selection = income_and_expenses_menu
        system "clear"
        case @income_expense_selection
        when "Add Income"
            income
        when "Deduct Expenses"
            expenses
        when "Return to Main Menu"
            main_menu
        end
    end
end

while selection != "Exit"
    selection = main_menu
    system "clear"
    case selection
    when "Make changes to your balance"
        income_and_expenses_state
    when "View current balance"
        puts "balance"
    when "View breakdown of income and expenses"
        puts "breakdown"
    when "Create or delete a category"
        puts "category"
    when "Exit"
        exit
    end
end

Related