我正在用Ruby編寫課程管理程序,以允許用戶向模式添加/刪除模塊。Ruby-節省用戶輸入
目前,我的程序將允許用戶添加模塊,但是當我嘗試刪除它們時,我被告知它們不存在。
我使用添加的模塊的方法是:
def self.add_module
# schemes = {}
scheme_exists = false
add_another_scheme = true
# module_exists = false
add_another_module = true
while add_another_scheme
print "Enter scheme name: "
scheme_name = gets
$schemes.has_key?(scheme_name.chop) ? scheme_exists = true : scheme_exists = false
if !scheme_exists
$schemes[scheme_name.chop] = []
puts "Scheme #{scheme_name.chop} has been added to the system"
elsif
scheme_exists = true
puts "This scheme has already been added"
end
while add_another_module
print "Enter module name: "
module_name = gets
$schemes[scheme_name.chop].include?(module_name.chop) ? true : $schemes[scheme_name.chop] << module_name.chop
# puts "Module #{module_name.chop} has been added to #{scheme_name.chop}"
# 22/08/2012 at 14:15 Now need to read in each module's unique identifier and year it belongs to
print "Enter module ID: "
$module_ID =gets
$schemes[scheme_name.chop].include?($module_ID.chop) ? true : $schemes[scheme_name.chop] << $module_ID.chop
$schemes.has_key?($module_ID.chop) ? module_exists = true : module_exists = false
print "Enter the academic year to which the module belongs: "
module_year = gets
$schemes[scheme_name.chop].include?(module_year.chop) ? true : $schemes[scheme_name.chop] << module_year.chop
if !$module_exists
$schemes[$module_ID.chop] = []
puts "Module #{$module_ID.chop} : #{module_name.chop} has been added to #{scheme_name.chop} for the year #{module_year}"
elsif
$module_exists = true
puts "A module with this ID has already been added to the scheme, please check if the module already exists, or choose another ID "
else
# puts "Module #{module_name.chop}, #{module_ID.chop} has been added to #{scheme_name.chop} for the year #{module_year}"
end
# puts "Module #{module_name.chop}, #{module_ID.chop} has been added to #{scheme_name.chop}"
print "Add another module? "
ask_if_user_wants_to_add_another_module = gets
if(ask_if_user_wants_to_add_another_module.chop == "y" or ask_if_user_wants_to_add_another_module == "yes")
add_another_scheme = false
else if(ask_if_user_wants_to_add_another_module.chop != "y" or ask_if_user_wants_to_add_another_module != "yes")
Application.main_menu
end
end
end
和我使用盡量去除模塊的方法是:
def self.remove_module
print "Which scheme would you like to remove a module from? "
scheme_name = gets
$schemes.has_key?(scheme_name.chop) ? scheme_exists = true : scheme_exists = false
if !scheme_exists
$schemes[scheme_name.chop] = []
puts "Scheme #{scheme_name.chop} doesn't exist"
else
scheme_exists = true
puts "Which module would you like to remove from #{scheme_name.chop}?"
$module_ID = gets
if !$module_exists
$schemes[$module_ID.chop] = []
puts "Module #{$module_ID.chop} : does not exist in #{scheme_name.chop} "
else
module_exists = true
puts "Module #{$module_ID.chop} has been removed from #{scheme_name.chop} "
# puts "Module #{module_name.chop}, #{module_ID.chop} has been added to #{scheme_name.chop} for the year #{module_year}"
end
end
end
當我運行程序,會顯示一個菜單,我選擇將模塊添加到調用第一個方法的方案中。我跟進的步驟:
- 輸入方案名稱 - 將顯示一個消息,說明該方案已添加到系統
- 輸入模塊名稱
- 輸入模塊ID
- 輸入學年到該模塊所屬 - 將顯示一個消息,說明該模塊已被添加到該計劃當年
- 我問我是否要添加另一個模塊,所以我說是
- 程序再次運行相同的步驟,但這次跳過第一個,並開始輸入模塊名稱 - 當我再次執行這些步驟時,顯示另一條消息,指出第二個模塊已經添加到相同的計劃,無論我指定的第二次
- 然後我被問到是否要添加另一個模塊,我輸入'n',並返回到原始菜單。
- 這一次,我選擇的選項從計劃中刪除模塊
- 我問我想刪除從一個模塊,該模塊方案,所以我輸入我已經添加了模塊
- 我的一個然後問我想刪除哪個模塊,因此我輸入了之前添加的模塊之一,但我被告知該模塊不存在於該模式中。
這表明我調用第一種方法(添加模塊)時我存儲在變量中的數據剛剛在我調用第二種方法(刪除模塊)時被丟棄。
如何確保這些信息不會丟失?是否有需要設置並連接我的程序的數據庫,還是需要使用會話和會話變量?或者是完全不同的東西?
任何幫助將不勝感激!
Re。代碼「$ schemes」的第一部分是我存儲了用戶調用'add module'方法時添加的方案的數組。如果該方案尚未添加 - 我需要讓用戶知道,因爲沒有關聯的模塊要移除,否則(即如果該方案已被添加),那麼我需要詢問用戶哪個模塊與該方案相關聯他們想要刪除。 – Someone2088
Re。代碼'scheme_exists'的第二部分與'module_exists'類似,只是它檢查方案是否存在,而不是模塊是否存在...方案和模塊是兩個不同的東西。 – Someone2088
@ Someone2088該代碼是相同的,只有地圖是不同的。相同的代碼。您不一定需要相信或理解我所說的內容,但最終如果您希望其他人能夠閱讀您的代碼,則需要對其進行相當程度的更改。現在真的很難相處。 –