Now there is no shortest path finding and same friend in the network will be counted redundantly. \\
Alumnus will be stored in one global hash "$alumni{name, alumnusobj}"
. \\
Friends of each alumnus will be stored in one instance array "@friend{name}". \\
There are "getter" & "setter" for instance method and variables.
Input anything except the "correct" options to see the printFriend function which is a traverse over all the friends.\\
#!/usr/bin/ruby
$alumni=Hash.new
module Lib
def Lib.isAlumnus(name)
#check if is already a alumnus before adding
#return value: true(is), false(not)
if $alumni.has_key?(name)
true
else
false
end
end
def Lib.getAlumnusByName(name)
#load a name, then return Alumnus obj
return $alumni[name]
end
end
class Person
def initialize(name)
@name=name
end
def getName #name getter
@name
end
def setName(name) #name setter
#set and return name
@name=name
end
end
class Alumnus < Person
include Lib
def initialize(name)
super(name)
@friend=Array.new
$alumni.store(name,self)
end
def numFriend
@friend.length
end
def isFriend(name)
#check if is already a friend before adding
#return value: true(isFriend), false(notFriend)
if @friend.include?(name)
true
else
false
end
end
def listFriend
return @friend
end
def printFriend
#this is the travers function can be used to list friends in total depth.
#bug: it will create a dead loop in case of n1 -> n2 -> n3 -> n1
@friend.each do |name|
print name + " -> "
Lib.getAlumnusByName(name).printFriend.each {|x| puts " "+x.to_s, ", "}
puts "; "
end
end
def addFriend(name)
#add friend by name
#1. [check] to see if the name exists
#1.1 if so, [get the id], then add array{@id,name} to the $alumni
#1.2 if not, create an alumnus, then add array{@id, name} to the $alumni
if isFriend(name) || @name==name #if name is self or already, do not do anything
puts(name +" is already a friend or yourself!")
else #name is not self or already a friend, now we can add it to @friend
if !Lib.isAlumnus(name) #if name is not alumnus, create silently new alumnus using name
Alumnus.new(name)
end
@friend << name#add name to @friend
puts(name +" is now your friend!")#new friend added
end
end
end
option=0
while option != "q"
puts "#######################################"
puts "1.Create new alumnus"
puts "2.Find alumnus by name"
puts "3.Add friend to current alumnus"
puts "4.List friends of current alumnus"
puts "5.List all alumnus"
puts "#######################################"
puts " "
print "Choose an option from the list: "
option = gets
option.chomp!
case option
when "1" then
print "1.Enter new name please: "
name=gets
name.chomp!
if !Lib.isAlumnus(name)
a1=Alumnus.new(name)
puts "Alumnus '"+name+"' created!"
else
a1=Lib.getAlumnusByName(name)
puts "'"+name+"' is already alumnus"
end
when "2" then
print "2.Look for name: "
name=gets
name.chomp!
if Lib.isAlumnus(name)
a1=Lib.getAlumnusByName(name)
puts "'"+name+"' is already alumnus"
puts "'"+name+"' has "+a1.numFriend.to_s+" friend(s)."
else
puts "No such alumnus!"
end
when "3" then
if !(a1.nil?)
print "3. Enter friend's name please: "
name=gets
name.chomp!
a1.addFriend(name)
puts "'"+a1.getName+"' has "+a1.numFriend.to_s+" friend(s)."
else
puts "Find a valid alumnus or create one before adding friend!"
end
when "4" then
j=0
if !(a1.nil?)
if a1.numFriend>0
for i in a1.listFriend
puts "#"+i+"# direct friend"
puts Lib.getAlumnusByName(i).listFriend
j+=Lib.getAlumnusByName(i).numFriend
end
end
puts "'"+a1.getName+"' has "+a1.numFriend.to_s+" direct friend(s) and "+j.to_s+" networked friend(s)."
else
puts "Find a valid alumnus or create one before adding friend!"
end
when "5" then
puts $alumni.keys
i=$alumni.length
if i==1
puts "There is "+i.to_s+" alumnus."
elsif i>1
puts "There are "+i.to_s+" alumnus."
else
puts "No alumnus."
end
when "q" then
puts "#######"
puts "Goodbye"
puts "#######"
else
a1.printFriend
puts "Wrong option!"
puts " "
end
end