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
나는 프로그램을 실행하면 , 메뉴가 표시되고 첫 번째 방법을 호출하는 구성표에 모듈을 추가하도록 선택합니다. 나는 단계를 따르
이- 은 구성표 이름 를 입력 - 메시지가 표시되는 방식은
- 는
- 입력 학년도 모듈 ID를 입력 모듈 이름
- 를 입력 시스템에 추가 된 것을 주장 모듈이 속한 - 해당 모듈이 그 해의 구성표에 추가되었다는 메시지가 표시됩니다.
- 다른 모듈을 추가할지 묻는 메시지가 표시되므로 예라고 말합니다.
- 프로그램은 동일한 단계를 다시 실행하지만 이번에는 첫 번째 모듈을 건너 뛰고 모듈 이름을 입력 할 때 시작합니다. - 단계를 다시 수행하면 두 번째 모듈이 표시된 다른 메시지가 표시됩니다 두 번째로 지정한 연도에 대해 동일한 체계에 추가됨
- 'n'을 입력 한 다른 모듈을 추가할지 묻는 메시지가 표시되고 원래 메뉴로 돌아갑니다.
- 이 시간이 나는 구조에서 모듈을 제거하는 옵션을 선택
- 난에서 모듈을 제거하고자하는 방식을 요구하고있어, 그래서 내가
- I에 모듈을 추가했던 하나를 입력 그러면 어떤 모듈을 제거하고 싶은지 물어 보았 기 때문에 이전에 추가 한 모듈 중 하나를 입력했지만 그 모듈은이 구성에 존재하지 않는다고 들었습니다.
이것은 첫 번째 방법 (모듈 추가)을 호출 할 때 변수에 저장 한 데이터가 두 번째 방법 (모듈 제거)을 호출 할 때 방금 폐기되었음을 의미합니다.
이 정보가 손실되지 않도록하려면 어떻게해야합니까? 데이터베이스를 설정하고 연결해야하는 데이터베이스가 있습니까? 아니면 세션 및 세션 변수를 사용해야합니까? 아니면 완전히 다른 것입니까?
도움이 될 것입니다.
Re. 코드의 첫 번째 부분 인'$ schemes'는 사용자가 'add module'메소드를 호출 할 때 추가 된 스키마를 저장 한 배열입니다. 스키마가 이미 추가되지 않은 경우 - 제거 할 관련 모듈이 없으므로 사용자에게 알려야합니다. 그렇지 않으면 (즉, 스키마가 이미 추가 된 경우) 사용자에게 해당 스키마와 관련된 모듈을 요청해야합니다 그들은 제거하고 싶다. – Someone2088
Re. 코드의 두 번째 부분 인'scheme_exists'는'module_exists'와 유사 합니다만, 모듈이 존재하는지 여부와 반대로 스키마가 존재하는지 검사하는 것입니다 ... 스키마와 모듈은 두 가지 다른 것입니다. – Someone2088
@ Someone2088 코드가 동일하며지도 만 다릅니다. 같은 코드. 당신이 말하는 것을 반드시 믿고 이해할 필요는 없지만 궁극적으로 다른 사람들이 코드를 읽을 수있게하려면 공정한 금액을 변경해야합니다. 지금 당장 grok하는 것은 정말로 어렵습니다. –