Skip to content

Instantly share code, notes, and snippets.

@ltw
Forked from gmmowry/task_list.rb
Last active May 26, 2017 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ltw/668cacda4b9ae46b43fc7c10021ac5ac to your computer and use it in GitHub Desktop.
Save ltw/668cacda4b9ae46b43fc7c10021ac5ac to your computer and use it in GitHub Desktop.
# Today we'll be creating a Task List for an individual
# who has a lot of errands and tasks to complete and multiple
# locations or stores to go to in order to complete them.
# Create a class for a Task List.
# All TaskList instances should have an owner and a dute date
# passed in on creation. For instance, our owner could be "Tyler"
# and his due date would be "Sunday". The owner should not be
# changeable but you should be able to read it outside of the class.
# The due date should be readable and writeable outside of the class.
# We may have multiple locations to go to in order to complete our
# tasks, for instance we may need to go to Target to pick up batteries.
# Create a list instance variable which can hold the location and tasks at
# each location. It should be empty on creation.
# When we think of a new location we need to go to, we'll need to save
# it in our list and set it up to hold multiple tasks. Create an instance
# method that will save the new location to the list with the ability to
# hold multiple tasks. If the location already exists in our list, notify
# the user that the location is already on the list.
# When we add a new task to the list, we'll need to also say which location
# the task should be completed. Create an instance method to save a task and its
# location to the list. If the location doesn't exist in the list yet,
# we'll need to create it, and then add the task. If the location
# already exists, then we'll need to check if that task already exists
# for the location. Check if that tasks is in our records for the location,
# if it's not, add it. If the task already exists, notify the user
# that the task is already on their list!
# Congrats, you completed a task - cross it off your list! Create an instance
# method to delete a task off it's location records. If the specified
# location includes the task, delete it. If the specified location
# does not include the task, notify the user that they don't
# have that task on their list.
# Man, you sure have a lot to do! Print out your list in a user
# friendly way, printing each location and then each of the tasks
# needed to be accomplished there below it. Like the following:
#
# At Target:
# - pick up batteries
# - get new toothpaste
# At pet store:
# - pick out new chew toy
# At post office:
# - mail Grandma's birthday present
# - buy new stamps
# Uh oh, have you done everything on your task list by the due date?
# Create a predicate instance method is_past_due? that uses the current
# day as an argument and checks to see if your task list is past due.
# You'll need to compare the days of the week to each other in some way.
# Don't forget that if you have no tasks on your list, then nothing is past due!
class TaskList
attr_reader :owner
attr_accessor :due_date
def initialize(owner, due_date)
@owner = owner
@due_date = due_date
@list = {}
end
def go_to_new_location(location)
if @list[location].nil?
@list[location] = []
else
puts "We're already stopping at #{location}! Add tasks to your list."
end
end
def add_task(task, location)
go_to_new_location(location)
if @list[location].include? task
puts "Already have #{task} on the list!"
else
@list[location] << task
end
end
def remove_task(task, location)
if @list[location].include? task
@list[location].delete(task)
else
puts "Looks like you don't have that on your list"
end
end
def show_list
@list.each do |location, tasks|
puts "At #{location}:"
tasks.each do |task|
puts '- ' + task
end
end
end
def is_past_due?(current_day)
days_of_week = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
]
past_due = false
if @list.values.all? { |tasks| tasks.empty? }
puts "You have no more tasks. Good job!"
else
if days_of_week.index(current_day.capitalize) > days_of_week.index(@due_date.capitalize)
past_due = true
else
puts "You've still got time to finish your tasks!"
end
end
past_due
end
end
# wednesday_list = TaskList.new("Andrew", "Wednesday")
#
# wednesday_list.go_to_new_location("Target")
# wednesday_list.add_task("get AA batteries", "Target")
# wednesday_list.add_task("get 4 avocadoes", "Marianos")
# wednesday_list.add_task("get 4 avocadoes", "Marianos")
# wednesday_list.show_list
# wednesday_list.is_past_due?("Monday")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment