Created
May 26, 2017 16:05
-
-
Save gmmowry/4c95dd875740d7250d6dd95995c60453 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# 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 new_location(location) | |
if !@list[location] | |
@list[location] = [] | |
else | |
p "We're already stopping at #{location}! Add tasks to your list." | |
end | |
end | |
def add_task(task, location) | |
new_location(location) | |
if @list[location].include? task | |
p "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 | |
p "Looks like you don't have that on your list" | |
end | |
end | |
def show_list | |
@list.each do |location, tasks| | |
p "At #{location}:" | |
tasks.each do |task| | |
p task | |
end | |
end | |
end | |
def is_past_due?(current_day) | |
days_of_week = [ | |
"Sunday", | |
"Monday", | |
"Tuesday", | |
"Wednesday", | |
"Thursday", | |
"Friday", | |
"Saturday" | |
] | |
past_due = nil | |
if @list.values.any? { |tasks| tasks != [] } | |
if days_of_week.index(current_day.capitalize) > days_of_week.index(@due_date.capitalize) | |
past_due = true | |
else | |
p "You've still got time to finish your tasks!" | |
past_due = false | |
end | |
else | |
p "You have no more tasks. Good job!" | |
past_due = false | |
end | |
p past_due | |
end | |
end | |
# saturday_list = TaskList.new("Andrew", "Wednesday") | |
# saturday_list.new_location("Target") | |
# saturday_list.add_task("get AA batteries", "Target") | |
# saturday_list.add_task("get 4 avocadoes", "Marianos") | |
# saturday_list.add_task("get 4 avocadoes", "Marianos") | |
# saturday_list.show_list | |
# saturday_list.is_past_due?("Monday") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment