require 'date'
class Memo
attr_accessor :title, :content, :category, :created_at
def initialize(title, content, category)
@title = title
@content = content
@category = category
@created_at = DateTime.now
end
def to_s
"#{@title}: #{@content} (Category: #{@category}) [Created at: #{@created_at}]"
end
end
class MemoApp
def initialize
@memos = []
load_memos
end
def run
loop do
show_menu
choice = gets.chomp.to_i
case choice
when 1
create_memo
when 2
display_memos
when 3
search_memos
when 4
break
else
puts "Invalid choice. Please try again."
end
end
end
private
def show_menu
puts "\nMemo App Menu"
puts "1. Create Memo"
puts "2. Display Memos"
puts "3. Search Memos"
puts "4. Exit"
print "Enter your choice: "
end
def create_memo
print "Enter memo title: "
title = gets.chomp
print "Enter memo content: "
content = gets.chomp
print "Enter memo category: "
category = gets.chomp
memo = Memo.new(title, content, category)
@memos << memo
save_memos
puts "Memo created successfully!"
end
def display_memos
if @memos.empty?
puts "No memos found."
else
puts "\nMemos:"
@memos.each_with_index do |memo, index|
puts "#{index + 1}. #{memo}"
end
end
end
def search_memos
print "Enter search term: "
term = gets.chomp.downcase
results = @memos.select { |memo| memo.title.downcase.include?(term) || memo.content.downcase.include?(term) || memo.category.downcase.include?(term) }
if results.empty?
puts "No memos found matching the search term."
else
puts "\nSearch Results:"
results.each { |memo| puts memo }
end
end
def load_memos
if File.exist?("memos.txt")
File.open("memos.txt", "r") do |file|
file.each_line do |line|
title, content, category, created_at = line.chomp.split(": ")
@memos << Memo.new(title, content, category)
end
end
end
end
def save_memos
File.open("memos.txt", "w") do |file|
@memos.each do |memo|
file.puts "#{memo.title}: #{memo.content}: #{memo.category}: #{memo.created_at}"
end
end
end
end
# メモアプリの実行
memo_app = MemoApp.new
memo_app.run