-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiary_manager.rb
101 lines (84 loc) · 2.35 KB
/
diary_manager.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
require_relative 'entry'
require 'json'
require 'date'
class DiaryManager
DATA_FILE = 'data.json'
def initialize
@entries = load_entries
end
def load_entries
return [] unless File.exist?(DATA_FILE)
file = File.read(DATA_FILE)
entries_data = JSON.parse(file)
entries_data.map { |entry| Entry.new(entry['content'], entry['date']) }
end
def save_entries
entries_data = @entries.map { |entry| { content: entry.content, date: entry.date } }
File.write(DATA_FILE, JSON.pretty_generate(entries_data))
end
def create_entry
puts "Enter your diary entry (type 'exit' to finish):"
entry_text = ""
while (line = gets.chomp) != 'exit'
entry_text += line + "\n"
end
entry = Entry.new(entry_text)
@entries << entry
save_entries
puts "Entry saved!"
end
def view_entries
if @entries.empty?
puts "No entries found."
else
@entries.each_with_index do |entry, index|
puts "\nEntry ##{index + 1} (#{entry.date}):"
puts entry.content
end
end
end
def edit_entry
view_entries
puts "Enter the number of the entry you want to edit:"
entry_number = gets.chomp.to_i - 1
if entry_number.between?(0, @entries.size - 1)
puts "Current Entry:"
puts @entries[entry_number].content
puts "Enter new content (type 'exit' to finish):"
new_content = ""
while (line = gets.chomp) != 'exit'
new_content += line + "\n"
end
@entries[entry_number].content = new_content
save_entries
puts "Entry updated!"
else
puts "Invalid entry number."
end
end
def delete_entry
view_entries
puts "Enter the number of the entry you want to delete:"
entry_number = gets.chomp.to_i - 1
if entry_number.between?(0, @entries.size - 1)
@entries.delete_at(entry_number)
save_entries
puts "Entry deleted!"
else
puts "Invalid entry number."
end
end
def search_entries
puts "Enter a keyword to search:"
keyword = gets.chomp
found_entries = @entries.select { |entry| entry.content.include?(keyword) }
if found_entries.empty?
puts "No entries found with the keyword '#{keyword}'."
else
found_entries.each_with_index do |entry, index|
puts "\nEntry ##{index + 1} (#{entry.date}):"
puts entry.content
end
end
end
end