0

I have the following class, which I will be using as the menu for my program:

class Application
  # To change this template use File | Settings | File Templates.
  def initialize
    mainMenu
  end

  def navigateTo(what)
    what.new.display
    mainMenu
  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"
    navigateTo Module
    addModule
  when "2"
    navigateTo Module
  when "3"
    navigateTo Module
  when "4"
    navigateTo Module
  when "5"
    navigateTo Student
  when "6"
    navigateTo Student
  when "7"
    navigateTo Student
end
end
Application.new
end

However, when I run the class, I tried selecting option 1 from the menu, and this line was printed out in the console:

#<Module:0x1bd2c90>What would you like to do?

followed by a printout of the menu again.

Option 1 should navigate to my Module class, which looks like this:

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

  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

  def addModule
    moduleName = Module.new(30)
    moduleRefNo = Random(100)
    #moduleTitle = @moduleTitle
    moduleYear(4)

    print "What is the name of the module you would like to add?"
    moduleName = gets
    moduleRefNo
    printf "Which year does the module belong to?"
    @@moduleYear = gets
    puts "#{moduleName}, belonging to #{@@moduleYear} has been added to the system, with reference number #{moduleRefNo}."
    navigateTo Application

  end

  def addModuleToScheme
    moduleName.moduleScheme = schemeName
  end
  def removeModuleFromScheme
    moduleName.moduleScheme = nil
  end

  def queryModule

  end

end

Once the the user has selected option 1 from the main menu, and the program has navigated to the Module class, I expected it to run that class fully, i.e. display the prompts to the user, and read in whatever they type on the keyboard, then navigate back to the menu, as indicated by the line

navigateTo Application

at the end of my 'addModule' function. However, for some reason, it seems to either not be navigating to the Module class, or just skipping straight to the end of it. Can anyone point out what I'm doing wrong here?

Edit 16/08/2012 at 14:53

Ok, so I've made the changes suggested and have altered the following function to include the (v):

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

However, I'm now getting a list of errors:

  1. (Application class filepath) in 'mainMenu' uninitialised constant Application: :CourseModules (nameError)

This occurs on the line navigateTo CourseModules under when "1" in the menu.

  1. There's then another error on the line mainMenu in the initialize method, which just says from (filepath) in 'initialize'

  2. A similar one which just says from (filepath) 'new' on the line Application.new at the end of the Application.rb class, along with one that says in 'class:Application' on the same line

  3. Then I have one on the first line that says (filepath) in 'top (required)'

  4. Finally, I have another two error which are quite different to the previous ones:

    from -e:1:in 'load'

    from -e:1:in 'main'

Any other ideas about what I'm now doing wrong?

*Edit 16/08/2012 at 16:15 *

My full script for my Application.rb class is:

class Application
  # To change this template use File | Settings | File Templates.
  def initialize
    mainMenu
  end

  def navigateTo(what)
    what.new(v).display
    mainMenu
  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"
    navigateTo CourseModules
  when "2"
    navigateTo CourseModules
  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

and the full script for my courseModules.rb class is:

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

   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

   def addModule
     moduleName = Module.new(30)
     moduleRefNo = Random(100)
     #moduleTitle = @moduleTitle
     moduleYear(4)

     print "What is the name of the module you would like to add?"
     moduleName = gets
     moduleRefNo
     printf "Which year does the module belong to?"
     @@moduleYear = gets
     puts "#{moduleName}, belonging to #{@@moduleYear} has been added to the system, with reference number #{moduleRefNo}."
     navigateTo Application

   end

   def addModuleToScheme
     moduleName.moduleScheme = schemeName
   end
   def removeModuleFromScheme
     moduleName.moduleScheme = nil
   end

   def queryModule

   end

 end
8
  • class Module is a baaad idea. Commented Aug 14, 2012 at 15:59
  • What would you recommend instead? Commented Aug 14, 2012 at 16:00
  • Pick another name. There is already a class with this name in standard library Commented Aug 14, 2012 at 16:06
  • Ok, so I've renamed the class to CourseModules, and changed all of the references to that class in my Application class. However, when I now try to run the Application class, and select option 1 from the menu, I get a nameError... any idea how I can correct this? Commented Aug 14, 2012 at 16:21
  • You forgot to require it, maybe? require_relative 'course_module' Commented Aug 14, 2012 at 16:39

1 Answer 1

2

The reason your application never shows your CourseModules menu (ie never calls addModule) is because it is stuck in Application.mainMenu method. When you press '1', the Application.navigateTo method is called. This will output the CourseModules object, which is the #<Module:0x1bd2c90> you are seeing, and then display the mainMenu again. You keep doing this forever, which is why you never get to the addModule line.

There are quite a few issues with the code, so I am not really sure how to suggest a fix. For example,

1) what.new.display will fail due to the CourseModules initialize method requires 1 parameter but you are passing in none

2) The addModule method you want to call from the mainMenu method will fail due to the method not existing in the Application class.

3) The addModule method will fail since the navigateTo method does not exist for the CourseModules class.

I think you need to re-evaluate how you are organizing the code. Perhaps put all the interface related methods in one class. That class would handle displaying prompts and passing any inputs to the appropriate classes for processing.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for your reply. So with regard to point 1, would I write [code]what.new.display(initialize)[/code] referring to the initialize method in the CourseModules class?
It would be what.new(v).display, where v is the value you want the @val to equal in the CourseModules class.
Ok, I've made the change you suggested, however, when I now run the code, I'm getting several errors- I've updated my original post to show the new errors.
Can you put your full script? There have been quite a few changes so I want to make sure I am using your updated code.
I've now posted my full script. Thanks for your help with this.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.