-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLog.rb
179 lines (165 loc) · 4.14 KB
/
Log.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
# Log
# Abigail My Tran
# December 3rd, 2016
require './LogItem'
require 'date'
class Log < Hash
private
#Log is a hash with:
# key: date
# value: Array of LogItems(name,times) in that day.
#
def initialize()
@log = Hash.new
f = File.open("DietLog.txt")
f.each do |line|
line.chomp!
if line != ' '
array = line.split(',')
date_array = array[0].split('/')
year = date_array[2].to_i
day = date_array[1].to_i
month = date_array[0].to_i
food_date = Date.new(year, month, day)
food_name = array[1]
if (@log.size() == 0 || [email protected]_key?(food_date))
contain = Array.new
contain.push( LogItem.new( food_name, 1 ) )
@log.store(food_date, contain)
else
added = "no"
food_array_at_current_day = @log[food_date]
for i in 0..(food_array_at_current_day.size-1)
if (food_array_at_current_day[i].name == food_name)
new_food_occur = @log[food_date][i].occur + 1
@log[food_date].delete_at(i)
@log[food_date].push( LogItem.new( food_name, new_food_occur ) )
added = "yes"
break
end
end
if (added == "no")
@log[food_date].push( LogItem.new( food_name, 1 ) )
end
end
end
end
end
public
attr_reader :log
###
# LOG FOOD
###
def add( food_date = Date.new(), food_name = String.new() )
if (log.size() == 0 || !log.has_key?(food_date))
contain = Array.new
contain.push( LogItem.new( food_name, 1 ) )
log.store(food_date, contain)
else
added = "no"
food_array_at_current_day = log[food_date]
for i in 0..(food_array_at_current_day.size-1)
if (food_array_at_current_day[i].name == food_name)
new_food_occur = @log[food_date][i].occur + 1
log[food_date].delete_at(i)
log[food_date].push( LogItem.new( food_name, new_food_occur ) )
added = "yes"
break
end
end
if (added == "no")
log[food_date].push( LogItem.new( food_name, 1 ) )
end
end
end
###
# SHOW ALL
###
def show_all()
if log.size == 0
puts "The food log is empty"
else
dates = log.keys.sort
print_string = ''
dates.each do |day|
log[day].each do |item|
name = item.name
time = item.occur
print_string.insert(0, " #{name} (#{time})\n")
end
print_string.insert(0, "#{day}: \n")
end
puts(print_string)
end
end
###
# SHOW
##
def show(food_date)
if (!log.has_key? food_date)
puts("No data for this day")
else
print_string = "#{food_date}: \n"
log[food_date].each do |item|
name = item.name
time = item.occur
print_string.insert(-1, " #{name} (#{time})\n")
end
puts(print_string)
end
end
###
# TO LOG
###
def to_log()
log_text = ""
if (log.size == 0)
return log_text
else
log.keys.each do |food_date|
food_year = food_date.year
food_month = food_date.mon
food_day = food_date.mday
log_date = "#{food_month}/#{food_day}/#{food_year}"
array = log[food_date]
array.each do |item|
food_name = item.name
food_occur = item.occur
while (food_occur >0)
log_text.insert(-1, "#{log_date},#{food_name}\n")
food_occur -= 1
end
end
end
return log_text
end
end
###
# SAVE
###
def save(text)
File.open("DietLog.txt", 'w') { |file| file.write(text) }
end
###
# DELETE
###
def delete(food_date, food_name)
if (!log.has_key? food_date)
puts("No data for this day")
else
log[food_date].each do |item|
if (item.name == food_name)
if (item.occur == 1)
log[food_date].delete( item )
break
else
new_occur = item.occur - 1
log[food_date].delete( item )
log[food_date].push( LogItem.new(food_name,new_occur) )
break
end
end
end
end
end
end