diff --git a/Jays-Game/Justins-Game/controller.rb b/Jays-Game/Justins-Game/controller.rb new file mode 100644 index 0000000..4a38603 --- /dev/null +++ b/Jays-Game/Justins-Game/controller.rb @@ -0,0 +1,33 @@ +require_relative 'view' +require_relative 'model' + +class GameController + include GameView + + def run! + todo_list = List.new + + Print::welcome + + loop do + Print::menu + case Print::fetch_input + when "V" + Print::print_list(todo_list.todos) + when "A" + Print::add_todo(Print::serialize_todo) + when "C" + Print::complete_todo(Print::completed_id.to_i) + when "D" + Print::delete_todo(Print::deleted_id.to_i) + when "Q" + puts "Thanks for playing!" + exit + else + Print::error_message + end + end + end +end + +GameController.new.run! diff --git a/Jays-Game/Justins-Game/model.rb b/Jays-Game/Justins-Game/model.rb new file mode 100644 index 0000000..30291c0 --- /dev/null +++ b/Jays-Game/Justins-Game/model.rb @@ -0,0 +1,47 @@ +class Todo + attr_reader :id, :name, :description, :completed + + def initialize(args) + @id = args[:id] + @name = args[:name] + @description = args[:description] + @completed = false + end + + def complete + @completed = true + end + + def completed? + @completed + end +end + +class List + attr_reader :todos + + def initialize + @pid = 0 + @todos = [] + end + + def add_todo(input) + @todos << Todo.new(input.merge(find_id)) + end + + def complete_todo(id) + completed_item = @todos.select{ |todo| todo.id == id } + unless completed_item + fail "Cannot find todo with id of #{id}" + end + end + + def delete_todo(id) + @todos.delete_if{ |n| n.id == id } + end + + private + def find_id + {id: @pid += 1} + end +end diff --git a/Jays-Game/Justins-Game/view.rb b/Jays-Game/Justins-Game/view.rb new file mode 100644 index 0000000..37dc04f --- /dev/null +++ b/Jays-Game/Justins-Game/view.rb @@ -0,0 +1,69 @@ +module GameView + module Print + class << self + def error_message + puts "Oops, that doesn't appear to be a command key. Please try again!" + end + + def welcome +welcome = < (V)iew your todos + -> (A)dd a todo + -> (C)omplete a todo + -> (D)elete a todo + -> (Q)uit program + ********************** +MEN + puts menu + end + + def print_list(todos) + todos.each do |todo| + if todo.completed? + puts "[X] #{todo.id} || #{todo.name} - #{todo.description}" + else + puts "[X] #{todo.id} || #{todo.name} - #{todo.description}" + end + end + end + + def serialize_todo + {}.tap do |obj| + ["\nName: ", "\nDescription: "].each do |t| + if obj.empty? + obj[:title] = fetch_input(t) + else + obj[:description] = fetch_input(t) + end + end + end + end + + def deleted_id + given_id = "Enter the id of the todo you want to delete: " + fetch_input(given_id) + end + + def completed_id + given_id = "Enter the id of the todo you want to mark as completed: " + fetch_input(given_id) + end + + def fetch_input(question=nil) + puts question if question + print "> " + gets.chomp + end + end + end +end diff --git a/Jays-Game/view.rb b/Jays-Game/view.rb index e90ae7c..4b18410 100644 --- a/Jays-Game/view.rb +++ b/Jays-Game/view.rb @@ -12,7 +12,7 @@ def run_spinner def error_message puts "That's not a command key. Try again!" end - + def title_screen title = <