55502f40dc8b7c769880b10874abc9d0

Hi Im working on a program where you can register guests and unregister them and so on. Now Im trying to implement a module that should print out a welcome text and this menu which is in the example below. I put "Hello" just for a test and get the output in the terminal but the main_menu doesnt show. I am new at Ruby so I need help. Anyone got any suggestions?!

module Menus
  
  class Main_Menu
    puts "Hello"
   def self.main_menu
     puts "---------------------------"
     puts "      Menu"                          
     puts "  1. Checkin"
     puts "  2. Checkout"
     puts "  3. Lists"
     puts "  4. Economy"
     puts "  5. Exit"
     puts ""
     puts " What do you want to do?"
     puts "---------------------------"
   end
  end
end  


class Main_Menu
  include Menus
 



  MAIN_MENU = Main_Menu.new
	#LISTS_MENU = Lists_Menu.new
	#ECONOMY_MENU = Economy_Menu.new
end

Refactorings

No refactoring yet !

B8ba61cc84ecb63c859435be28547dfb

steved

August 26, 2010, August 26, 2010 14:15, permalink

1 rating. Login to rate!
module Menus

  class Main_Menu
    # This is a class method because of the "self"
    def self.main_menu
      puts "---------------------------"
      puts "      Menu"                          
      puts "  1. Checkin"
      puts "  2. Checkout"
      puts "  3. Lists"
      puts "  4. Economy"
      puts "  5. Exit"
      puts ""
      puts " What do you want to do?"
      puts "---------------------------"
    end
  end
end  


class Main_Menu
  include Menus

  # But this is confusing because you have Main_Menu and Menus::Main_Menu
  Main_Menu.main_menu
end

# =====================
# Do this instead

module Menus

  MAIN_MENU = <<END 
---------------------------
      Menu
1. Checkin
2. Checkout
3. Lists
4. Economy
5. Exit

What do you want to do?
---------------------------
END

end  

puts Menus::MAIN_MENU
55502f40dc8b7c769880b10874abc9d0

tragicdancer.myopenid.com

August 26, 2010, August 26, 2010 15:29, permalink

No rating. Login to rate!

Thats great thank you! But if I then would like to create 2 more menus, when you choose lists from this menu you get redirected to a "List menu" and when you choose economy you get to a economy menu. Can I just add the 2 other menus more or less the same inside the module and use puts Menus::LIST_MENU and Menus::ECONOMY_MENU for those list objects?! Sorry for the rather confusing explanation.

55502f40dc8b7c769880b10874abc9d0

tragicdancer.myopenid.com

August 29, 2010, August 29, 2010 14:08, permalink

No rating. Login to rate!

Do anyone know how to incorporate 2 more menus that should be sub menus two the main_menu?! When I try and Implement it looks something like this below and then all 3 menus are shown at the same time. That is ofcourse because I use puts but I dont know how to solve it. Any help on this?! Thanks

module Menus
  
 def self.getValidPositiveNumber
		input = gets.chomp
		
		# Har användaren matat in ett tal? ... något konstig lösning
		while (input.to_i.to_s != input && input.to_f.to_s != input) do	
			puts "Ogiltig data. Försök igen."
			input = gets.chomp
		end
		
		# Är talet större än 0?
		number = input.to_f
		if (number <= 0)
			puts "Du kan inte ange negativt värde."
			getValidPositiveNumber
		end
		return number
	end
	
	# Hämtar indata från användaren tills denne skriver in något giltigt. Med 
	# giltig indata menas något som finns i parametern valid_options, som förutsätts 
	# vara en range eller en array (eftersom dessa inkluderar include? och inspect). 
	# Returnerar indatan.
	def self.get_valid_input(valid_options)
		
		input = gets.chomp

		# loopa tills ett giltigt värde angetts
		while (!valid_options.include?(input) && !valid_options.include?(input.to_i))	
			# både Range och Array har include?
			puts "Ogiltigt värde. Skriv in ett nytt alternativ mellan " + valid_options.inspect
			input = gets.chomp
		end
		return input
	
	end
	
	
	# Klass som representerar basklass för en meny. En meny innehåller en text, 
	# samt en range för giltiga val. Varje meny måste tala om vad som händer för 
	# vart och ett av de giltiga valen (implementeras i subklassens do_menu_action).
	# Användaren skriver ett giltigt alternativ och skickas till do_menu_action
	# där olika händelser utförs beroende på val.
	class Menu
	
		attr_reader  :valid_options_range, :menu_string
		
		# Skapar en meny med giltigt intervall valid_options_range samt en
		# sträng för vad som kommer att visa menyn.
		def initialize(valid_options_range, menu_string)
			@valid_options_range = valid_options_range
			@menu_string = menu_string
		end
		
		
		# Representerar vad som ska hända vid respektive val. Förhandsvillkoret
		# är att händelsen är ett giltigt tal i valid_options_range.
		def do_menu_action(action)
			raise "Måste anropas i någon subklass!"
		end
		
		# Representationen av menyn är helt enkelt menysträngen.
		def to_s
			return @menu_string
		end
	end


  MAIN_MENU = <<END 
"---------------------------" 
  Welcome to Ruby Camping!
      Menu
1. Checkin
2. Checkout
3. Lists
4. Economy
5. Exit

What do you want to do?
"---------------------------"
END


#if choice >= menu_options.length or choice < 0
 #puts "Invalid choice"

def make_menu_choice(choice)

   case choice
        when 1:
          $camping.check_in
        when 2:
           $camping.check_out
        when 3:
          $current_menu = LISTS_MENU
        when 4:
          $current_menu = ECONOMY_MENU
        when 5:
          exit
       end
    end
 

  LISTS_MENU = <<END
"---------------------------"   
-- 1. List current guests --
-- 2. List all guests --
--                          --
-- 0. Back to Main menu      --
------------------------------"
END


def make_menu_choice(choice) 
   case choice
        when 1:
          $camping
        when 2:
          $camping.all_guests
        when 0:
          $current_menu = MAIN_MENU
       end
    end

   ECONOMY_MENU = <<END
 "---------------------------"   
 -- 1. List current guests --
 -- 2. List all guests --
 --                          --
 -- 0. Back to Main menu      --
 ------------------------------"
END

end    

puts Menus::MAIN_MENU
puts Menus::LISTS_MENU
puts Menus::ECONOMY_MENU 
__END__

Your refactoring





Format Copy from initial code

or Cancel