close

class Teacher
  attr_accessor :name, :id, :course
  def initialize name, id, course
    @name = name
    @id = id
    @course = course
  end
  def report
    puts "I'm a teacher, my name is "+@name+", my id is "+@id+", my course is "+@course+"."
  end
end

class Student
  attr_accessor :name, :id
  def initialize name, id
    @name = name
    @id = id
    @scores = []
  end
  def getScore i
    @scores[i]
  end
  def setScore i, s
    @scores[i] = s
  end
  def average
    sum = 0.0
    count = 0
    @scores.each do |x|
      sum += x
      count = count+1
    end
    sum/count
  end
  def report
    print "I'm a student, my name is "+@name+", my id is "+@id+", my scores are: "
    @scores.each do |x|
      print x.to_s+" "
    end
    print "\n"
  end
end

class ScoreSys
  attr_accessor :teacher
  def initialize
    @st_list = []
  end
  def addStudent i, st
    @st_list[i] = st
  end
  def getStudent i
    @st_list[i]
  end
  def getStudentAverage i
    @st_list[i].average
  end
  def getExamAverage i
    sum = 0.0
    count = 0
    @st_list.each do |s|
      sum += s.getScore i
      count = count+1
    end
    sum/count
  end
  def report
    puts "Score system report:"
    @teacher.report
    @st_list.each do |s|
      s.report
    end
    print "Student average: "
    3.times do |i|
      print getStudentAverage(i).to_s+" "
    end
    print "\n"
    print "Exam average: "
    3.times do |i|
      print getExamAverage(i).to_s+" "
    end
    print "\n"
  end
end

sys = ScoreSys.new

sys.teacher = Teacher.new "Cathy", "AUXXXX", "Mathematics"

stu1 = Student.new "John", "AM999999"
stu1.setScore 0, 90
stu1.setScore 1, 80
stu1.setScore 2, 70
sys.addStudent 0, stu1

stu2 = Student.new "Mary", "AM999998"
stu2.setScore 0, 60
stu2.setScore 1, 50
stu2.setScore 2, 40
sys.addStudent 1, stu2

stu3 = Student.new "Henry", "AM999997"
stu3.setScore 0, 30
stu3.setScore 1, 20
stu3.setScore 2, 10
sys.addStudent 2, stu3

sys.report

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 lupohsunrock 的頭像
    lupohsunrock

    lupohsunrock的部落格

    lupohsunrock 發表在 痞客邦 留言(1) 人氣()