Created
April 10, 2014 09:56
-
-
Save anonymous/10363489 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ToDo | |
class List | |
include Enumerable | |
def initialize | |
@items = [] | |
end | |
def each(&block) | |
@items.each(&block) | |
end | |
def <<(item) | |
@items << item | |
end | |
def [](index) | |
@items[index] | |
end | |
def delete_at(index) | |
@items.delete_at(index) | |
end | |
def size | |
@items.size | |
end | |
end | |
class UI | |
def initialize | |
@list = List.new | |
end | |
def run | |
choice = nil | |
until choice == :exit | |
choice = menu | |
case choice | |
when :add then add | |
when :delete then delete | |
when :show then show | |
when :exit then true # do nothing | |
when nil then invalid_choice | |
end | |
end | |
puts "Good bye!" | |
end | |
def get_integer | |
gets.to_i | |
end | |
def invalid_choice | |
puts "You just entered a wrong option, Going back to main menu" | |
end | |
def add | |
puts "You selected to add a item to ToDo list", | |
"Type your todo" | |
@list << gets.chomp | |
end | |
def delete | |
puts "You selected 2 to delete an item", | |
"Enter a list no to delete?" | |
number = get_integer | |
puts "Deleting #{number} #{@list[number-1]}", | |
"Are you sure?" | |
if gets.chomp == "yes" | |
@list.delete_at(number-1) | |
puts "Deleted" | |
else | |
puts "item not deleted" | |
end | |
end | |
def show | |
puts "You selected 3 to display the list" | |
@list.each.with_index(1) do |todo, number| | |
puts "#{number}: #{todo}" | |
end | |
end | |
def menu | |
puts "Enter 1 to add, 2 delete, 3 to display the list, 4 to exit" | |
[nil, :add, :delete, :show, :exit][get_integer] | |
end | |
end | |
end | |
ToDo::UI.new.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment