Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finished MVCgame #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Wes-Game/controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require_relative 'view'
require_relative 'model'

class GameController
include GameView

def run!
wineList = List.new

loop do
Print::menu
case Print::fetch_user_input
when "V"
Print::print_list(wineList.wines)
when "A"
wineList.add_wine(Print::serialize_wine)
when "D"
wineList.delete_wine(Print::deleted_name)
when "Q"
puts "We're done"
exit
else
Print::error_message
end
end
end
end

GameController.new.run!
25 changes: 25 additions & 0 deletions Wes-Game/model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Wine
attr_reader :name, :type, :comments, :rating
def initialize args
@name = args[:name]
@type = args[:type]
@comments = args[:comments]
@rating = args[:rating]
end
end

class List
attr_reader :wines
def initialize
@wines = []
end

def add_wine(input)
@wines << Wine.new(input)
end

def delete_wine(name)
@wines.delete_if { |n| n.name == name }
end

end
63 changes: 63 additions & 0 deletions Wes-Game/view.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module GameView

module Print

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for these nested modules? Why not just be satisfied with GameView?

class << self

def error_message
puts "That's not a command key. Try again!"
end

def menu
menu = <<EOS

***** Welcome *****
- (V)iew your wines
- (A)dd a wine
- (D)elete a wine
- (Q)uit program
***** *****

EOS
puts menu
end

#does print_list know too much about the underlying object???

def print_list(wines)
wines.each do |wine|
puts "====================="
puts wine.name
puts "====================="
puts "Type: #{wine.type}"
puts "Comments: #{wine.comments}"
puts "Rating: #{wine.rating}"
end
end

def serialize_wine
new_wine = {}
print "\nEnter the name: "
new_wine[:name] = gets.chomp
print "\nEnter the type: "
new_wine[:type] = gets.chomp
print "\nEnter comments: "
new_wine[:comments] = gets.chomp
print "\nEnter a rating (0-10): "
new_wine[:rating] = gets.chomp
return new_wine
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ruby standard is to omit return in situations like this. new_wine will do the trick by itself.

end

def deleted_name
print "\nEnter the name of wine you want to DELETE: "
gets.chomp
end

def fetch_user_input(question=nil)
puts question if question
print "> "
gets.chomp
end
end
end
end