0

我正在编写一个 Ruby 程序来模拟课程管理系统。就我的程序目前而言,当用户第一次加载程序时,他们会看到一个菜单。我目前正在处理菜单中的第一个选项——将模块添加到方案中。

一步一步地工作,当用户从菜单中选择这个选项时,他们被要求输入一个方案名称,该名称被保存为名为“方案”的哈希中的键。然后他们被要求输入一个模块名称,该名称保存到哈希中,在属于他们刚刚输入的方案的键下。

然后打印出一行,告诉用户该方案和模块已添加到系统中。

然后询问他们是否要添加另一个模块。这是我有几个问题的地方:

A. 如果用户键入'y',他们被要求输入方案——如果他们输入的方案已经存在,他们被告知,如果不存在,它被创建,然后他们被要求输入模块名称。然后询问他们是否要添加另一个模块。如果用户键入“n”当前存在的方案,连同它们的模块一起被打印出来,程序退出。

B. 在这种情况下,我在同一个方案中添加了两个模块,但只打印了最后一个模块,所以大概是覆盖了输入的第一个模块 - 我如何确保不会发生这种情况,并且在第一个模块被保存到“方案”散列之后,任何后续模块都附加到散列的末尾,而不是替换已经存在的模块?

  1. 如何确保当用户在步骤 A 键入“n”时程序不会退出,而是返回到主程序菜单?

  2. 如何确保保存到散列的第一条数据不会被我保存到散列的任何后续数据覆盖,而是它们成为单独的条目?(这里的想法是有一些类似于 Java ArrayList 的东西,它会自动将下一位数据添加到下一个数组元素中,该数组元素可以自由增加数值)

我拥有的两个类是 Application(充当我与用户的接口)和 CourseModules(存储用户输入的数据的位置)。

Application.rb 目前看起来像这样:

class Application
# To change this template use File | Settings | File Templates.
require './courseModules.rb'
def initialize
  mainMenu
end

=begin
  def navigateTo(what)
    what.new(v).display
    mainMenu
  end
=end

  def mainMenu
    puts "What would you like to do?
      1: Add module to a scheme
      2: Remove module from a scheme
      3: Query modules
      4: Modify module
      5: Register a student on a scheme
      6: Remove a student from a scheme
      7: Register a student on a module
      8: Remove a student from a module"
    case gets.strip
      when "1"
        CourseModules.add_module
      when "2"
        CourseModules.removeModuleFromScheme
      when "3"
        navigateTo CourseModules
      when "4"
        navigateTo CourseModules
      when "5"
        navigateTo Student
      when "6"
        navigateTo Student
      when "7"
        navigateTo Student
    end
  end
  Application.new
end

CourseModules.rb 目前看起来像这样:

class CourseModules
# To change this template use File | Settings | File Templates.
@@moduleScheme = nil
@@moduleYear = nil
#@moduleTitle = ""
@noOfModulesInScheme = 0

def self.moduleYear
  @@moduleYear
end

def initialize(v)
  @val = v
end
# Set and get the @val object value
def set (v)
  @val = v
end
def get
  return @val
end


# Attempt at add_module method on 21/08/2012 at 12:35
def self.add_module
  schemes = {}
  scheme_exists = false
  add_another_module = true

 while add_another_module
   print "Enter scheme name: "
   scheme_name = gets
   schemes.has_key?(scheme_name.chop) ? scheme_exists = true : scheme_exists = false

   if !scheme_exists
     print "Enter module name: "
     module_name = gets
     schemes[scheme_name.chop] = module_name.chop
     puts "Scheme #{scheme_name.chop} with module #{module_name} has been added to the system"
   else
     scheme_exists = false
     puts "This scheme has already been added"
     puts "Enter module name: "
     module_name = gets
     schemes[scheme_name.chop] = module_name.chop
     puts "Scheme #{scheme_name.chop} with module #{module_name} has been added to the system"
   end

   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.chop == "yes")
     add_another_module = false
   end
 end
 puts schemes
 end




 end
 def removeModuleFromScheme
   moduleName.moduleScheme = nil
 end

 def queryModule

end

* 21/08/2012 18:00 编辑**

好的,我的 courseModules 类现在看起来像这样:

class CourseModules
# To change this template use File | Settings | File Templates.
@@moduleScheme = nil
@@moduleYear = nil
#@moduleTitle = ""
@noOfModulesInScheme = 0

def self.moduleYear
 @@moduleYear
end

def initialize(v)
 @val = v
end
# Set and get the @val object value
def set (v)
 @val = v
end
def get
 return @val
end


# Attempt at add_module method on 21/08/2012 at 16:30
def self.add_module
 schemes = {}
 scheme_exists = false
 add_another_scheme = true
 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"
   else
     scheme_exists = false
     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
     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
   end

 end

 print "Add another scheme? "
 ask_if_user_wants_to_add_another_scheme = gets
 if !(ask_if_user_wants_to_add_another_scheme.chop == "y" or ask_if_user_wants_to_add_another_scheme.chop == "yes")
   add_another_scheme = false
 end
 puts @schemes

end

 while add_another_module
   print "Enter scheme name: "
   scheme_name = gets
   schemes.has_key?(scheme_name.chop) ? scheme_exists = true : scheme_exists = false

   if !scheme_exists
     print "Enter module name: "
     module_name = gets
     schemes[scheme_name.chop] = module_name.chop
     puts "Scheme #{scheme_name.chop} with module #{module_name} has been added to the system"
   else
     scheme_exists = false
     puts "This scheme has already been added"
     puts "Enter module name: "
     module_name = gets
     schemes[scheme_name.chop] = module_name.chop
     puts "Scheme #{scheme_name.chop} with module #{module_name} has been added to the system"
   end

   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.chop == "yes")
     add_another_module = false
   end
 end
 puts schemes
end

end

但是现在,当我从菜单中选择“1”并输入方案名称时,我在线上出现错误:

@schemes.has_key?(scheme_name.chop) ? scheme_exists = true : scheme_exists = false

它说

add_module': undefined methodhas_key 中?对于 nil:NilClass (NoMethodError)

有谁知道我能做些什么来解决这个问题?

4

1 回答 1

0

正如我在另一个线程上告诉你的,如果你需要方案和模块之间的 1:M 关系,你需要将模块存储在数组中。您还需要将方案哈希重新定义为实例变量。即在

def initialize
   @schemes = {}
   # ... your other code
end

def self.add_module
    scheme_exists = false
    add_another_scheme = true

    while add_another_scheme
        add_another_module = true 
        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"
        else
            scheme_exists = false
            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
            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.chop == "yes")
               add_another_module = false
            end 
        end

        print "Add another scheme? "
        ask_if_user_wants_to_add_another_scheme = gets
        if !(ask_if_user_wants_to_add_another_scheme.chop == "y" or ask_if_user_wants_to_add_another_scheme.chop == "yes")
           add_another_scheme = false
        end
    end
    puts @schemes
end
于 2012-08-21T14:59:32.453 回答