Skip to content

Instantly share code, notes, and snippets.

@PDegenPortnoy
Last active December 22, 2015 06:29
Show Gist options
  • Save PDegenPortnoy/6431176 to your computer and use it in GitHub Desktop.
Save PDegenPortnoy/6431176 to your computer and use it in GitHub Desktop.
Samples and examples of application framework for Launch Academy
# Sample ReportOutputter & child class (FileOutputter)
class ReportOutputter
attr_accessort :report_generator
def initialize(report_generator)
@report_generator = report_generator
end
def write
print_all_students
print_average_grades
print_min_grade
print_std_dev
end
protected
def print_all_students
@report_generator.sort_students.each{|s| write_line(s.last_name + ", " + s.first_name + ": " + s.average)
end
def print_average_grades;
def print_max_grade;
def print_min_grade;
def print_std_dev;
def write_line
raise NoMethodError, "Must be implemented in child class!"
end
end
class FileOutputter << ReportOutputter
def initialize
@file = File.open("Student_Report", "w")
end
def write_line(line)
@file.write(line)
end
end
# Sample use of case statement for conciseness
def letter_grade
case
when average >= 90; return "A"
when average >= 80; return "B"
when average >= 70; return "C"
else; return "F"
end
end
# Sample Application Level Object for "owning" responsibility of creating other classes that have Single Responsiblity
class StudentGradePrinter
file_reader = FileReader.new(input_file)
students = file_reader.get_students
report_generator = ReportGenerator.new(students)
report_generator.generate_report
outputters = []
outputters << FileOutputter.new
outputters << ConsoleOutputter.new
outputters.each { |o| o.write(report_generator) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment