コース管理システムをシミュレートする Ruby プログラムを作成しています。私のプログラムの現状では、ユーザーが最初にプログラムをロードすると、メニューが表示されます。私は現在、メニューの最初のオプションに取り組んでいます。これは、モジュールをスキームに追加することです。
ユーザーがメニューからこのオプションを選択すると、この手順を段階的に進めていく際に、スキーム名を入力するように求められます。スキーム名は、'schemes' と呼ばれるハッシュにキーとして保存されます。次に、入力したスキームに属するキーの下に、ハッシュに保存されるモジュール名を入力するよう求められます。
次に、スキームとモジュールがシステムに追加されたことをユーザーに知らせる行が出力されます。
次に、別のモジュールを追加するかどうかを尋ねられます。ここでいくつか質問があります。
A. ユーザーが「y」と入力すると、スキームを入力するように求められます。入力したスキームが既に存在する場合は、スキームが存在することが通知され、存在しない場合は作成され、モジュール名を尋ねられます。次に、別のモジュールを追加するかどうかを尋ねられます。ユーザーが「n」と入力すると、現在存在するスキームとそのモジュールが出力され、プログラムが終了します。
B. この場合、同じスキームに 2 つのモジュールを追加しましたが、最後のモジュールのみが出力されたので、最初に入力したモジュールをオーバーライドしていると考えられます。最初のモジュールが 'schemes' ハッシュに保存された後、後続のモジュールはハッシュの最後に追加され、既に存在するモジュールは置き換えられませんか?
ユーザーが手順 A で 'n' を入力したときにプログラムが終了せず、メイン プログラム メニューに戻るようにするにはどうすればよいですか?
ハッシュに保存された最初のデータが、ハッシュに保存された後続のデータによって上書きされず、それらが別個のエントリになるようにするにはどうすればよいですか? (ここでのアイデアは、数値的に自由に増加する次の配列要素にデータの次のビットを自動的に追加する、Java ArrayList に似たものを持つことです)
私が持っている 2 つのクラスは、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
* 2012 年 8 月 21 日 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
それは言う
has_key
add_module': undefined method
?」nil:NilClass (NoMethodError) の場合
これを解決するために私に何ができるか知っている人はいますか?