-
Notifications
You must be signed in to change notification settings - Fork 0
/
todolist.rb
209 lines (177 loc) · 6.6 KB
/
todolist.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
class TodoList
# methods and stuff go here
attr_accessor :title, :items
# Initialize todo list with a title and no items
def initialize(list_title)
@title = list_title
@items = Array.new # Starts empty! No Items yet!
@tdate = Time.now.strftime("%m/%d/%Y") # Todays' Date
end
$report_file = File.new("newlist.txt", "w+") #new file with wright permission
#-------------------- Start Add ---------------------#
# adds new item with time due as options
def add_item(new_item, options = {})
new_item = Item.new(new_item.capitalize)
# add due time if given
if options[:due_time]
new_item.due_time = options[:due_time]
end
# See if item already exists else push item and conferm with message
@items.empty?? add_new_item(new_item) : item_exist(new_item)? conf_message(new_item.description, "Already Exists") : add_new_item(new_item)
end
# adds it to the array of Items
def add_new_item(new_item)
@items.push(new_item)
end
# --------------------- End Add ----------------#
#---------------------Start validations --------------#
# see if item with description exist
def is_item_with_description_exist(new_item)
item_found = @items.find{|item| item.description == new_item }
return !item_found.nil?
end
# see if item exist for both new and rename
def item_exist(new_item)
if new_item =~ /[A-Za-z]/
is_item_with_description_exist(new_item)
elsif new_item.is_a?(Item)
is_item_with_description_exist(new_item.description)
end
end
def index_exist(index_no)
@items.length >= index_no - 1
end
#---------------------End validations --------------#
#-------------- Stard Delete ----------------#
# clears item by description from the list with validations
def clear_item(delete_item)
@items.empty?? conf_message(delete_item, "does not exist") : item_delete(delete_item)
end
# deletes item from the list if found
def item_delete(delete_item)
@items.delete_if {|e| e.description == delete_item.capitalize}
conf_message(delete_item, "removed")
end
# removes by index form the list with validations
def remove_item(index_no)
index_exist(index_no)? exist_index_remove_item(index_no) : conf_message(index_no, "does not exist")
end
# deletes item from the list if found
def exist_index_remove_item(index_no)
index_no -= 1
@r_item = @items[index_no].description
@items.delete_at(index_no)
conf_message(@r_item, "removed")
end
#-------------- End Delete ----------------#
#-------------- Start change status ---------#
# Chanage status of item with validations
def change_status(index_no)
index_exist(index_no)? index_change_status(index_no) : conf_message(index_no, "does not exist")
end
# Change status
def index_change_status(index_no)
index_no -= 1
items[index_no].update_status
end
#-------------- End change status ---------#
# ----------------- Start change title -----#
# Change the title of the list
def retitle(retitle)
@o_title = title
@title = retitle.upcase
conf_message(@o_title, "retitled")
end
# ----------------- End change title -----#
# -------------------- Start change item description --------- #
# Change Item description
def rename_item(index_no, rename)
index_exist(index_no)? index_exist_rename(index_no, rename) : conf_message(index_no, "does not exist")
end
# verifies if same description exists
def index_exist_rename(index_no, rename)
item_exist(rename)? conf_message(rename, "already Exists") : exist_item_remane(index_no, rename)
end
# changes description of item
def exist_item_remane(index_no, rename)
index_no -= 1
@o_description = @items[index_no].description
@items[index_no].description = rename.capitalize
conf_message(@o_description, "replaced")
end
# -------------------- End change item description --------- #
# confirm message of the actions
def conf_message(idescription, message)
print_line
puts "#{idescription} #{message}"
print_line
end
#------------------ Start print --------------------#
# Prints header
def print_header
"#{"Sl."} #{"Description".ljust(20)} #{"Created time".ljust(10)} #{"Due by".ljust(10)} #{"Status"}"
end
# Prints title
def print_title
"#{''.ljust(20)} #{title}"
end
# Prints Prepared date
def print_date
"#{''.ljust(15)} Prepared Date: #{@tdate}"
end
# Prints report to file
def report_print_items
$report_file.puts print_line
$report_file.puts print_date
$report_file.puts print_title
$report_file.puts print_line
$report_file.puts print_header
$report_file.puts print_line
items.each_with_index do |item, index_no|
$report_file.puts item.print_item_details(index_no)
end
$report_file.puts print_line
end
# Out put list to terminal
def print_items
puts print_line
puts print_date
puts print_title
puts print_line
puts print_header
puts print_line
items.each_with_index do |item, index_no|
puts item.print_item_details(index_no)
end
puts print_line
end
def print_line
"-" * 60
end
#------------------ End print --------------------#
end
class Item
# methods and stuff go here
attr_accessor :description, :completed_status, :created_time, :due_time
# Initialize item with a description and marked as
# not complete
def initialize(item_description)
@description = item_description
@completed_status = false
@created_time = Time.now.strftime("%H:%M") #Time now
@due_time = (Time.now + 18000).strftime("%H:%M") # TIme now = 5hrs
end
# toggles completed status of the item
def update_status
@completed_status = !completed_status
puts "#{description} Completed"
end
# prints items details
def print_item_details(index_no)
"#{index_no +1} #{@description.ljust(25)} #{@created_time.ljust(10)} #{@due_time.ljust(12)} #{task_complete?? "\u2611" : "\u2610"}"
end
# verify status
def task_complete?
@completed_status
end
end