Skip to content

Instantly share code, notes, and snippets.

@justuseapen
Last active December 28, 2015 11:08
Show Gist options
  • Save justuseapen/7490775 to your computer and use it in GitHub Desktop.
Save justuseapen/7490775 to your computer and use it in GitHub Desktop.
class Product
@@all = []
def initialize (item, price)
@item = item
@price = price
end
def price
@price
end
def name
@item
end
def self.create(name, price)
product = Product.new(name, price)
@@all << product
product
end
def self.all
@@all
end
end
def inventory
puts "Enter product name:"
name = gets.chomp
puts "Enter product price:"
price = gets.chomp
Product.create(name, price)
puts "Would you like to add another item?"
answer = gets.chomp.downcase
if answer == "yes"
inventory
else
total
end
end
def total
amount_due = 0
Product.all.each do |product|
puts "How many #{product.name}s would the customer like?"
quantity = gets.chomp.to_f
item_total = quantity * product.price.to_f
$amount_due = amount_due + item_total
end
end
def transaction
puts "The customer owes you $#{"%.2f" % $amount_due}."
puts "How much money did the customer give you?"
tendered = gets.chomp.to_f
if tendered >= $amount_due
change = tendered-$amount_due
change = change.to_f
puts "Please return $#{"%.2f" % change} in change to the customer."
else
"The customer did not give you enough cash."
end
end
inventory
transaction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment