2012-08-22 37 views
1

我正在用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 

當我運行程序,會顯示一個菜單,我選擇將模塊添加到調用第一個方法的方案中。我跟進的步驟:

  1. 輸入方案名稱 - 將顯示一個消息,說明該方案已添加到系統
  2. 輸入模塊名稱
  3. 輸入模塊ID
  4. 輸入學年到該模塊所屬 - 將顯示一個消息,說明該模塊已被添加到該計劃當年
  5. 我問我是否要添加另一個模塊,所以我說是
  6. 程序再次運行相同的步驟,但這次跳過第一個,並開始輸入模塊名稱 - 當我再次執行這些步驟時,顯示另一條消息,指出第二個模塊已經添加到相同的計劃,無論我指定的第二次
  7. 然後我被問到是否要添加另一個模塊,我輸入'n',並返回到原始菜單。
  8. 這一次,我選擇的選項從計劃中刪除模塊
  9. 我問我想刪除從一個模塊,該模塊方案,所以我輸入我已經添加了模塊
  10. 我的一個然後問我想刪除哪個模塊,因此我輸入了之前添加的模塊之一,但我被告知該模塊不存在於該模式中。

這表明我調用第一種方法(添加模塊)時我存儲在變量中的數據剛剛在我調用第二種方法(刪除模塊)時被丟棄。

如何確保這些信息不會丟失?是否有需要設置並連接我的程序的數據庫,還是需要使用會話和會話變量?或者是完全不同的東西?

任何幫助將不勝感激!

回答

1

(不是答案。)

我有問題,閱讀你的代碼。例如:

$schemes.has_key?(scheme_name.chop) ? scheme_exists = true : scheme_exists = false 
# Did you mean: 
scheme_exists = $schemes.has_key?(scheme_name.chop) 

和:

if !scheme_exists 
    $schemes[scheme_name.chop] = [] 
    puts "Scheme #{scheme_name.chop} doesn't exist" 
else 
    scheme_exists = true 
    # ... 

你爲什麼設置scheme_exists爲真?你剛剛測試過它不是是真的。

您的「方案存在」看起來很像您的「模塊存在」,如果它們相同,則使它們相同。有很多chop平進行,也許你應該只是在輸入後切斷,並停止切入其他地方–它似乎很容易出錯,並增加了很多噪音。

總的來說,我發現很難推斷出你的代碼,而沒有實際的介紹它。我建議重構,將事情看作「大聲」,比如你想要解決的問題。

我們也不知道是否有其他東西碰到$schemes。你是否應該「使用數據庫」取決於你的目標,你的約束,你如何運行應用程序等,你完全可以使用。你也可以序列化YAML,編寫一個純文本文件,各種各樣的東西。

如果您重新啓動/重新運行應用程序,您顯然會失去所有未持久的數據。如果你始終在應用程序中,那麼很可能是代碼或你的假設都是錯誤的,但是由於它的結構和命名方式,查看代碼並確定它是一項繁重的任務。

+0

Re。代碼「$ schemes」的第一部分是我存儲了用戶調用'add module'方法時添加的方案的數組。如果該方案尚未添加 - 我需要讓用戶知道,因爲沒有關聯的模塊要移除,否則(即如果該方案已被添加),那麼我需要詢問用戶哪個模塊與該方案相關聯他們想要刪除。 – Someone2088

+0

Re。代碼'scheme_exists'的第二部分與'module_exists'類似,只是它檢查方案是否存在,而不是模塊是否存在...方案和模塊是兩個不同的東西。 – Someone2088

+0

@ Someone2088該代碼是相同的,只有地圖是不同的。相同的代碼。您不一定需要相信或理解我所說的內容,但最終如果您希望其他人能夠閱讀您的代碼,則需要對其進行相當程度的更改。現在真的很難相處。 –