2 Replies - 626 Views - Last Post: 25 November 2013 - 06:15 PM Rate Topic: -----

#1 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Burger ordering program - need help

Posted 24 November 2013 - 07:22 PM

I am trying write a program that allows a user to order burgers, collect the ingredients requested and tally up a total price. I'm sure my code is very inefficient in all sorts of ways but I would still like to understand how to fix the bugs in it? I would appreciate a little guidance. I have put comments throughout the program that indicate where I know/believe problems are occurring. Thank you for your time and consideration. The error message I receive is as follows:

Welcome to Franks's Burger Joint!
How many burgers Would you like?
> 2
Would you like any condiments on burger 1?
> yes
How many condiments would you like on burger 1.
> 3
Sweet list them out
> ketchup
Burgers.rb:53:in `block in <main>': undefined method `collect_item' for main:Object (NoMethodError)
from Burgers.rb:51:in `times'
from Burgers.rb:51:in `<main>'
Gregorys-MacBook-Pro:programs gregoryheaphyheaphy$



# This program is supposed to ask a user how many burger he/she wants.
# record the ingredients on each burger 
# and calculate the final price


def prompt
	print "> "
end


puts "Welcome to Franks's Burger Joint!"
puts "How many burgers Would you like?"

prompt; burger_order = gets.chomp.to_i

if burger_order == 1
	puts "Just one!"
	puts "Would you like any condiments on your one'n only burger."

	prompt; answere = gets.chomp

	if answere == "yes"
		puts "We have ketchup, mustard, and mayonnaise to choose from and they don't cost nothin!" 
		puts "How many condiments would you like on your burger?"

		prompt; condiment_number = gets.chomp.to_i
        puts "Sweet list them out."

		condiment_number.times {        #problem?
		prompt; item = gets.chomp       #problem?
	 	collect_item(item)              #problem?
	 	}
	else
		puts "Boring dude. Learn to live."
	end

elsif burger_order > 1
	burgers = 1
	while burgers < burger_order # apparently I should only use while loops for 
		                         # infinite loops and for loops for all other loops?
		puts "Would you like any condiments on burger #{burgers}?"  

		prompt; answere = gets.chomp

		if answere == "yes"
			puts "How many condiments would you like on burger #{burgers}."

			prompt; condiment_number = gets.chomp.to_i
        	puts "Sweet list them out"

			condiment_number.times {     #problem?
			prompt; item = gets.chomp    #problem?
	 		collect_item(item)           #problem?
	 		}
	 	else
	 		puts "Boring"
		end
		burgers += 1
	end
end

collected_items = []
def collect_item(item)             
	collected_items = []           
	collected_items.push(item) # I'm hoping that this array can be accessed in the method down below as defined.
end

# Problems below for sure!!!!!!!!!!

def calculate_price # this would really be better if i could just access the values of a hash and 
	                # add them up without having to collect them in a seperate pricing array.
	food_prices = {"patty/bun" => 100, "pickles" => 25, "tomato" => 25, "onion" => 25, "cheese" => 25,
	                "green peppers" => 30, "yellow peppers" => 30, "jalapenos" => 30}
    for x in 0..(collected_items.length)
    	if food_prices.include?(collected_items[x]) #push all these into a new array
    		pricing_array = []
    		pricing_array.push(food_prices[collected_items[x]])
    		pricing_array.each { |item| sum += item }
    		puts (sum/100.00)    # I hope this will output a float!
    	else
    		puts "the customer got a plain burger...lame"
    	end
    end
end




Is This A Good Question/Topic? 0
  • +

Replies To: Burger ordering program - need help

#2 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1372
  • View blog
  • Posts: 3,470
  • Joined: 28-November 09

Re: Burger ordering program - need help

Posted 24 November 2013 - 11:06 PM

Given these classes, see what you come up with:

class Burger
  attr_accessor :condiment_list

  def initialize(condiment_list)
    # How do we make a condiment list using one passed in here?
  end
end

class Condiment
  attr_accessor :name, :price

  def initialize(name, price)
    @name = name
    @price = price
  end
end

class CondimentList
  attr_accessor :condiments
  attr_reader :price_list

  def initialize(price_list)
    # Make an array of condiments
    # Pass it a price list
  end

  def add(ingredient)
    # check if the price list includes the ingredient, if not
    # you should probably reject it. 
  end

  def total_price
    # use reduce(0) to total the condiments array, since the condiments are assured to be keys
    # of that hash, you can get the prices from there.
  end
end

class PriceList
  attr_accessor :items

  def initialize(item_hash = {})
    @items = parse item_hash
  end

  def parse(item_hash)
    # Use reduce([]) to put all the items of the hash into new condiment objects
  end
end



Now, to explain how some of this works. A class is a container that makes a new type of data (reference http://ruby.learncod.../book/ex40.html )

Reduce comes from the Enumerable module of Ruby (reference http://ruby-doc.org/...method-i-reduce )

<< is essentially the same as push. include will check if a hash has a key (reference http://ruby.learncod.../book/ex39.html )

Now consider using an until loop, and prompting a simple yes or no if they wish to add more ingredients.

I purposely left parts out of that code above, as this'll teach you far more if you actually research it. Oh, and do without trying to be cheeky or clever in prompts, it does nothing but get a teacher riled up and annoyed with you.

This way will be harder, but you'll learn more.
Was This Post Helpful? 0
  • +
  • -

#3 heaphyg  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 85
  • Joined: 30-August 13

Re: Burger ordering program - need help

Posted 25 November 2013 - 06:15 PM

Thank you for the assistance. I will study your content closely. No worries about a teacher, however. I am teaching my self and just trying to have a little fun. Thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1