I'm writing my first Ruby on Rails program, which is an application to assist with managing/ organising courses at a university, the students enlisted on them, and the modules belonging to each of the courses.
I currently have two classes: the first, called Application, is the one I am using to interface with the user, looks like this:
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.addModuleToScheme
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
The second, called courseModules is the one I will be using to allow the user to add new courses, and add modules to those courses. This class currently looks like this:
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
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
# Create an empty hash for the schemes
schemes = Hash.new()
# Allow user to enter scheme names into a set of variables, and use each scheme name as a hash/ array of modules.
# Then allow the user to enter the the modules for each scheme into each of the hashes
# Create specific hash elements by using the following line:
schemes = {:scheme1 => scheme1variable, :scheme2 => scheme2variable}
puts "What is the name of the scheme that you would like to add a module to? "
schemeName = gets
# Need to use an if statement here to check whether or not the scheme already exists, if it doesn't, create it, if it does,
# tell the user that it does.
if schemes.has_key?(schemeName)
puts "This scheme has already been added "
else
schemes.add(schemeName)
end
noOfModulesInScheme + 1
moduleName.moduleScheme = schemeName
end
def removeModuleFromScheme
moduleName.moduleScheme = nil
end
def queryModule
end
end
Currently, I'm just working on the first option in the menu- "Add module to scheme". When I try to run the program from my Application.rb class, the menu is displayed, and I enter: "1", which is the option I want to choose.
However, I'm then getting an error saying that my method "addModuleToScheme" is undefined. Could someone point out what I'm doing wrong with my "addModuleToScheme" method?
Many thanks.
Edit 21/08/2012 at 00:35
OK, I think that seems to have solved my addModuleToScheme method, however I'm now getting an error when trying to actually add the scheme name to the hash:
when I select '1' from the menu now, I'm asked for the name of the scheme, however, when I type that in, I get an undefined method error,
undefined method 'add' for {}:Hash (NoMethodError)"
on the line
schemes.add(schemeName)
in my if else statement. Any ideas what I'm doing wrong here?