Skip to content

Instantly share code, notes, and snippets.

@samstephen
Last active August 29, 2015 14:22
Show Gist options
  • Save samstephen/b92080585bda406a91d4 to your computer and use it in GitHub Desktop.
Save samstephen/b92080585bda406a91d4 to your computer and use it in GitHub Desktop.
Warehouse app
# Product Class
class Product
#method that will get/change value of name and shelf_id
attr_accessor :name, :category, :quantity
def initialize(name, quantity)
@name = name
@quantity = quantity
end
#returns `true/false` if the product belongs to a certain category.
def of_category?(title_of_a_category)
if @category = Category.new(@title)
true
else
false
end
end
end
class Category
#attr returns a title (gaming systems, food, etc)
attr_reader :title
#initialize Category.new(title)
def initialize(title)
@title = title
end
end
# This is an Array to store ALL of our products.
all_products = []
#create some categories
consoles = Category.new("Consoles")
mp3_players = Category.new("MP3 Players")
games = Category.new("Games")
#create some products
gameboy = Product.new("Game Boy", 8)
playstation = Product.new("PlayStation", 4)
xbox = Product.new("Xbox", 17)
ipod = Product.new("iPod", 15)
zune = Product.new("Zune", 3)
zelda = Product.new("Zelda", 23)
mario = Product.new("Mario", 10)
witcher = Product.new("Witcher 3", 3)
#assign products to categories
gameboy.category = consoles
playstation.category = consoles
xbox.category = consoles
ipod.category = mp3_players
zune.category = mp3_players
zelda.category = games
mario.category = games
witcher.category = games
# Add this, and any future, products to the Array.
all_products.push(gameboy, playstation, xbox, ipod, zune, zelda, mario, witcher)
print "What do you want to do? (q)uit, (l)ist all, (a)dd product: "
answer = gets.chomp
while answer != "q"
if answer == "l"
all_products.each do |product|
puts "#{product.name}"
end
else answer == "a"
print "New Product: "
product = gets.chomp.downcase
print "How many #{product}s are there: "
quantity = gets.chomp.downcase
print "What category would #{product} fall under? "
product = Product.new(product, quantity)
category = gets.chomp.downcase
category = Category.new(category)
product.category = category
all_products.push(product)
end
print "What do you want to do? (q)uit, (l)ist all, (a)dd product:"
answer = gets.chomp
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment