From df85272d1866b06503ab4815632fe0bee5d24dfe Mon Sep 17 00:00:00 2001 From: Lovinder Pnag Date: Tue, 26 May 2015 17:50:06 -0400 Subject: [PATCH 1/3] started a working zoo --- LoRyder1/controller.rb | 32 ++++++++++++++++++ LoRyder1/model.rb | 43 ++++++++++++++++++++++++ LoRyder1/view.rb | 74 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 LoRyder1/controller.rb create mode 100644 LoRyder1/model.rb create mode 100644 LoRyder1/view.rb diff --git a/LoRyder1/controller.rb b/LoRyder1/controller.rb new file mode 100644 index 0000000..e6e6399 --- /dev/null +++ b/LoRyder1/controller.rb @@ -0,0 +1,32 @@ +require_relative 'view' +require_relative 'model' + +class GameController + include GameView + + def run! + animalsZoo = Zoo.new + + Print::run_spinner + Print::title_screen + + loop do + Print::menagerie + case Print::get_user_input + when "A" + Print::print_zoo(animalsZoo.animals) + when "B" + animalsZoo.add_animal(Print::serialize_animal) + when "C" + "hey" + when "D" + puts "Thanks for visiting the Zoo" + exit + else + Print::error_message + end + end + end +end + +GameController.new.run! diff --git a/LoRyder1/model.rb b/LoRyder1/model.rb new file mode 100644 index 0000000..adf54a0 --- /dev/null +++ b/LoRyder1/model.rb @@ -0,0 +1,43 @@ +require 'faker' + +class Animal + attr_reader :id, :name, :description, :weight, :dangerous + + def initialize args + @id = args[:id] + @name = args[:name] + @description = args[:description] + @weight = args[:weight] + @dangerous = nil + end +end + +class Zoo + attr_reader :animals + + def initialize + @primary_id = 0 + @animals = [] + import_animals + end + + def add_animal(input) + @animals << Animal.new(input.merge(fetch_id)) + end + + def sell_animal(id) + @animals.delete_if { |n| n.id == id} + end + + def import_animals + add_animal(name: "Zebra", description: "part of the horse family with distinctive black and white stripes", weight: 700) + add_animal(name: "Elephant", description: "large mammals, herbivores, and gray", weight: 15000) + add_animal(name: "Lion", description: "big cat, male has mane, carnivore", weight: 440) + end + + private + + def fetch_id + {id: @primary_id +=1} + end +end \ No newline at end of file diff --git a/LoRyder1/view.rb b/LoRyder1/view.rb new file mode 100644 index 0000000..e6dcc17 --- /dev/null +++ b/LoRyder1/view.rb @@ -0,0 +1,74 @@ +module GameView + + module Print + class << self + def run_spinner + print "Loading (please wait) " + 3.times { print "."; sleep 1; } + print "\n" + end + + def error_message + puts "That key will not work. Have another go!" + end + + def title_screen + title = <<-EOT + + ============================= + You Bought A Zoo + ============================= + + + EOT + puts title + end + + def menagerie + menagerie = <<-EOS + + === Welcome===== + + - (A) View your animals + - (B) Add a animal + - (C) Export a animal + - (D) Quit program + + ======== + EOS + + puts menagerie + end + + def print_zoo(animals) + animals.each do |animal| + puts "#{animal.id} || #{animal.name} - #{animal.description} - #{animal.weight}lbs" + end + end + + def serialize_animal + {}.tap do |obj| + ["\nEnter the animal name:", "\nEnter the description:", "\nEnter the weight:"].each do |t| + if obj.empty? + obj[:name] = get_user_input(t) + else + obj[:description] = get_user_input(t) + end + end + end + + end + + def deleted_id + gimme_id = "\nEnter the id of the animal you want to sell" + get_user_input(the_id) + end + + def get_user_input(question=nil) + puts question if question + print "> " + gets.chomp + end + end + end +end \ No newline at end of file From acafc1d3b54ccfdce361b4c088560e446f2b8d2e Mon Sep 17 00:00:00 2001 From: Lovinder Pnag Date: Wed, 27 May 2015 13:14:37 -0400 Subject: [PATCH 2/3] working zoo app --- LoRyder1/controller.rb | 2 +- LoRyder1/model.rb | 4 ++++ LoRyder1/view.rb | 7 +++++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/LoRyder1/controller.rb b/LoRyder1/controller.rb index e6e6399..52a27d2 100644 --- a/LoRyder1/controller.rb +++ b/LoRyder1/controller.rb @@ -18,7 +18,7 @@ def run! when "B" animalsZoo.add_animal(Print::serialize_animal) when "C" - "hey" + animalsZoo.export_animal(Print::deleted_id.to_i) when "D" puts "Thanks for visiting the Zoo" exit diff --git a/LoRyder1/model.rb b/LoRyder1/model.rb index adf54a0..bea5e40 100644 --- a/LoRyder1/model.rb +++ b/LoRyder1/model.rb @@ -29,6 +29,10 @@ def sell_animal(id) @animals.delete_if { |n| n.id == id} end + def export_animal(id) + @animals.delete_if { |n| n.id == id } + end + def import_animals add_animal(name: "Zebra", description: "part of the horse family with distinctive black and white stripes", weight: 700) add_animal(name: "Elephant", description: "large mammals, herbivores, and gray", weight: 15000) diff --git a/LoRyder1/view.rb b/LoRyder1/view.rb index e6dcc17..9384a51 100644 --- a/LoRyder1/view.rb +++ b/LoRyder1/view.rb @@ -49,10 +49,13 @@ def print_zoo(animals) def serialize_animal {}.tap do |obj| ["\nEnter the animal name:", "\nEnter the description:", "\nEnter the weight:"].each do |t| + # puts obj.inspect if obj.empty? obj[:name] = get_user_input(t) - else + elsif obj.count == 1 obj[:description] = get_user_input(t) + else + obj[:weight] = get_user_input(t) end end end @@ -60,7 +63,7 @@ def serialize_animal end def deleted_id - gimme_id = "\nEnter the id of the animal you want to sell" + the_id = "\nEnter the id of the animal you want to sell" get_user_input(the_id) end From ca1e99e9c25398b611d57d1293ef18ad31b1789f Mon Sep 17 00:00:00 2001 From: LoRyder1 Date: Wed, 27 May 2015 13:16:37 -0400 Subject: [PATCH 3/3] changed method name --- LoRyder1/model.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LoRyder1/model.rb b/LoRyder1/model.rb index bea5e40..a72e155 100644 --- a/LoRyder1/model.rb +++ b/LoRyder1/model.rb @@ -22,7 +22,7 @@ def initialize end def add_animal(input) - @animals << Animal.new(input.merge(fetch_id)) + @animals << Animal.new(input.merge(get_id)) end def sell_animal(id) @@ -41,7 +41,7 @@ def import_animals private - def fetch_id + def get_id {id: @primary_id +=1} end end \ No newline at end of file