RUBY programming language
asmt4_Foudah.rb
require "#{File.dirname(__FILE__)}/functs_Foudah" class StudentDB include Functs_Foudah #open a txt file for reading def initialize() @rec = Hash.new #prompt for d/b name to load/create print("Enter database to open/create: ") @filename = gets.chomp if File::exists?(@filename) @fh = File.open(@filename,'r') while line = @fh.gets #transfer to hash myarray = line.split(":") id = myarray[0] value = myarray[1] @rec[id] = value end else @fh = File.open(@filename,'a+') end end #close file: quit program def quit_program() #prompt to save print("Save records (y/n): ") prompt = gets.chomp if (prompt=='y') @fh.close() @fh = File.open(@fh,'w') @rec.each do|key,value| #build up string temp_string = key + ":" + value @fh.puts(temp_string) end @fh.close() end end end #After class, main loop db = StudentDB.new choice = 0 until (choice == 5) #print out menu puts() puts("Student Database") puts("------------------------------") puts("1 = Add new record to database") puts("2 = Edit existing record in database") puts("3 = Remove existing record from database") puts("4 = Display record(s) in database") puts("5 = Quit") puts() print("Enter choice: ") choice_string = gets.chomp #prompt for users choice: choice_string choice = choice_string.to_i case(choice) when 1 db.add_record() when 2 db.update_record() when 3 db.delete_record() when 4 db.print_records() when 5 db.quit_program() end end
__MACOSX/._asmt4_Foudah.rb
functs_Foudah.rb
module Functs_Foudah def add_record() #prompt for id print("Enter Student ID: ") id = gets.chomp #check if id already exists if (@rec[id]) puts("Student ID already exists!") return end #prompt for ln print("Enter last name: ") ln = gets.chomp #prompt for fn print("Enter first name: ") fn = gets.chomp #prompt for major print("Enter major: ") major = gets.chomp #prompt for year print("Enter catalog year: ") year = gets.chomp #join all fields joined = ln + "," + fn + "," + major + "," + year @rec[id] = joined end def update_record() #prompt for id print("Enter Student ID: ") id = gets.chomp #check if id doesnt already exists if (!@rec[id]) puts("Student ID doesn't exist!") return end #prompt for ln puts("Update last name") print("\tCurrent value: ") ln = gets.chomp print("\tNew value: ") ln = gets.chomp #prompt for fn puts("Update first name") print("\tCurrent value: ") fn = gets.chomp print("\tNew Value: ") fn = gets.chomp #prompt for major puts("Update major") print("\tCurrent value: ") major = gets.chomp print("\tNew value: ") major = gets.chomp #prompt for year puts("Update catalog year") print("\tCurrent value: ") year = gets.chomp print("\tNew value: ") year = gets.chomp #join all fields joined = ln + "," + fn + "," + major + "," + year @rec[id] = joined end def delete_record() #prompt for id print("Enter Student ID: ") id = gets.chomp #check if id doesnt already exists if (!@rec[id]) puts("Student ID doesn't exist!") return end @rec.delete(id) end def print_records() puts("------------------------------------------------------------------------") puts("ID \t\t | Last Name \t | First Name \t | Major \t | Year") puts("------------------------------------------------------------------------") @rec.each do|key,value| fields = value.split(",") #print ID print key if key.length < 5 print ("\t") end print "\t | " #print last name print ln = fields[0] if ln.length < 5 print("\t") end print "\t | " #print first name print fn = fields[1] if fn.length < 5 print("\t") end print "\t | " #print major print major = fields[2] if major.length < 5 print("\t") end print "\t | " #print year puts year = fields[3] end puts("------------------------------------------------------------------------") end end