Skip to content

Instantly share code, notes, and snippets.

@arn-e
Created October 17, 2012 03:36
Show Gist options
  • Save arn-e/3903567 to your computer and use it in GitHub Desktop.
Save arn-e/3903567 to your computer and use it in GitHub Desktop.
hospital
require './user'
require './employee'
class Administrator < Employee
def initialize(name)
super
@record_access = :admin
@title = "Administrator"
@password = default_password
end
def default_password
"superadmin2"
end
end
require './user'
require './patient'
require './doctor'
class Database
attr_accessor :patient_list, :staff_list
def initialize
@patient_list = {}
@staff_list = {}
end
def display_patient_list
@patient_list.sort.each do |key,value|
puts "patient UUID : #{key}"
puts "patient name : #{value.name}"
puts "patient record(s) : #{value.record}"
puts
end
end
def display_patient_medical_record(uuid)
puts "patient UUID : #{uuid}"
puts "patient name : #{@patient_list[uuid].name}"
puts "patient record(s) : #{@patient_list[uuid].record}"
puts
end
def add_patient_record(patient_to_add)
@patient_list[patient_to_add.uuid] = patient_to_add
end
def add_staff_record(staff_to_add)
@staff_list[staff_to_add.uuid] = staff_to_add
end
def delete_patient_record(patient_to_delete)
@patient_list.delete(patient_to_delete.uuid)
end
def delete_staff_record(staff_to_delete)
@staff_list.delete(staff_to_delete.uuid)
end
def add_patient_medical_record(uuid,record)
lookup_by_uuid(uuid).add_medical_record(record)
end
def lookup_by_uuid(uuid)
@patient_list[uuid]
end
end
class Doctor < Employee
def initialize(name)
super
@title = "Doctor"
end
end
class Employee < User
attr_accessor :name,:title
def initialize(name)
super(name)
end
def default_password
"hospital1"
end
end
require 'securerandom'
class Hospital
attr_accessor :name,:location
def initialize
@name = "Dev Bootcamp Asylum?"
@location = "717 California St"
@employee_count = 7
@patient_count = 54
end
end
class Database
attr_accessor :patient_list, :staff_list
def initialize
@patient_list = {}
@staff_list = {}
end
def display_patient_list
@patient_list.sort.each do |key,value|
puts "patient UUID : #{key}"
puts "patient name : #{value.name}"
puts "patient record(s) : #{value.record}"
puts
end
end
def display_patient_medical_record(uuid)
puts "patient UUID : #{uuid}"
puts "patient name : #{@patient_list[uuid].name}"
puts "patient record(s) : #{@patient_list[uuid].record}"
puts
end
def add_patient_record(patient_to_add)
@patient_list[patient_to_add.uuid] = patient_to_add
end
def add_staff_record(staff_to_add)
@staff_list[staff_to_add.uuid] = staff_to_add
end
def delete_patient_record(patient_to_delete)
@patient_list.delete(patient_to_delete.uuid)
end
def delete_staff_record(staff_to_delete)
@staff_list.delete(staff_to_delete.uuid)
end
def add_patient_medical_record(uuid,record)
lookup_by_uuid(uuid).add_medical_record(record)
end
def lookup_by_uuid(uuid)
@patient_list[uuid]
end
end
class User
attr_accessor :name, :password, :uuid, :username, :record_access
def initialize(name)
@name = name
@uuid = SecureRandom.uuid
@username = @name.downcase.split(' ')[1] +"."+ @name.downcase.split(' ')[0]
@record_access = :all
@password = default_password
end
def default_password
raise NotImplementedError, "subclasses must define default_password"
end
end
class Patient < User
attr_accessor :record
def initialize(name)
super(name)
@record_access = :self
@record = {}
end
def add_medical_record(record_to_add)
@record[SecureRandom.uuid] = record_to_add
end
def default_password
"changeme"
end
end
class Employee < User
attr_accessor :name,:title
def initialize(name)
super(name)
end
def default_password
"hospital1"
end
end
class Doctor < Employee
def initialize(name)
super
@title = "Doctor"
end
end
class Janitor < Employee
def initialize(name)
super
@record_access = :none
@title = "Janitor"
end
end
class Receptionist < Employee
def initialize(name)
super
@title = "Receptionist"
end
end
class Administrator < Employee
def initialize(name)
super
@record_access = :admin
@title = "Administrator"
@password = default_password
end
def default_password
"superadmin2"
end
end
class MenuInterface
def initialize(database)
@database = database
@privilege_list = {
:admin => 5,
:all => 4,
:self => 3,
:none => 0
}
@command_list = {
"list_patients" => 4,
"view_records" => 3,
"add_record" => 5,
"remove_record" => 5
}
end
def show_banner
puts "Hello Boots! Welcome!" + "\n"
end
def accept_user_input
puts "Please enter your user name : "
input_username = gets.chomp
puts "Please enter your password : "
input_password = gets.chomp
val_result = validate_credentials(input_username,input_password)
if val_result == false
puts "Incorrect Credentials"
accept_user_input
else
show_menu(input_username,val_result)
end
end
def validate_credentials(validate_username, validate_password)
@combined_users = @database.patient_list.sort + @database.staff_list.sort
@combined_users.each do |i,j|
return j.record_access if j.username == validate_username && j.password == validate_password
end
false
end
def show_menu(user,record_access)
puts "Welcome #{user}, your access level is : #{record_access.upcase}"
numerical_privilege = @privilege_list[record_access]
puts "What would you like to do?"
puts "Options : " if numerical_privilege > 1
puts "- list_patients" if numerical_privilege >= 4
puts "- view_records <patient_id>" if numerical_privilege >= 3
puts "- add_record <patient_id>" if numerical_privilege == 5
puts "- remove_record <patient_id>" if numerical_privilege == 5
command = gets.chomp
parse_command(command,numerical_privilege,user,record_access)
end
def parse_command(command,numerical_privilege,user,record_access)
command = command.split(' ')
if @command_list[command[0]] > numerical_privilege
puts "You lack permissions, peasant!"
show_menu(user,record_access)
end
command.length > 1 ? execute_command(command[0],user,record_access,command[1..100]) : execute_command(command[0],user,record_access,)
end
def execute_command(command, user, record_access, argument = nil)
argument = argument.join(' ') if argument != nil
puts argument.inspect
case
when command == "list_patients" then @database.display_patient_list
when command == "view_records" then @database.display_patient_medical_record(argument)
when command == "add_record" then @database.add_patient_record(Patient.new(argument))
when command == "remove_record" then @database.delete_patient_record(@database.lookup_by_uuid(argument))
else puts "Invalid command"
end
show_menu(user,record_access)
end
end
#create a patient
patient = Patient.new("Fellow DBC Student")
patient_zero = Patient.new("Second DBC Student")
patient.add_medical_record("too much battleship")
patient.add_medical_record("not enough baking experience")
patient_zero.add_medical_record("too many nasty ass dishes")
database = Database.new
database.add_patient_record(patient)
database.add_patient_record(patient_zero)
p database.patient_list
puts
#create an employee
doctor = Doctor.new("Doctor Bob")
janitor = Janitor.new("Janitor Steve")
administrator = Administrator.new("Supreme Overlord")
database.add_staff_record(doctor)
database.add_staff_record(janitor)
database.add_staff_record(administrator)
p database.staff_list
database.delete_staff_record(janitor)
p database.staff_list
puts
puts
database.display_patient_list
p database.patient_list
menu = MenuInterface.new(database)
menu.accept_user_input
puts
class Hospital
attr_accessor :name,:location
def initialize
@name = "Dev Bootcamp Asylum?"
@location = "717 California St"
@employee_count = 7
@patient_count = 54
end
end
class Janitor < Employee
def initialize(name)
super
@record_access = :none
@title = "Janitor"
end
end
require './administrator'
require './patient'
require './database'
require './doctor'
require './employee'
require './hospital'
require './user'
class MenuInterface
def initialize(database)
@database = database
@privilege_list = {
:admin => 5,
:all => 4,
:self => 3,
:none => 0
}
@command_list = {
"list_patients" => 4,
"view_records" => 3,
"add_record" => 5,
"remove_record" => 5
}
end
def show_banner
puts "Hello Boots! Welcome!" + "\n"
end
def accept_user_input
puts "Please enter your user name : "
input_username = gets.chomp
puts "Please enter your password : "
input_password = gets.chomp
record_access = validate_credentials(input_username,input_password)
if record_access == false
puts "Incorrect Credentials"
accept_user_input
else
show_menu(input_username,record_access)
end
end
def validate_credentials(validate_username, validate_password)
@combined_users = @database.patient_list.sort + @database.staff_list.sort
@combined_users.each do |i,j|
return j.record_access if j.username == validate_username && j.password == validate_password
end
false
end
def show_menu(user,record_access)
puts "Welcome #{user}, your access level is : #{record_access.upcase}"
numerical_privilege = @privilege_list[record_access]
puts "What would you like to do?"
puts "Options : " if numerical_privilege > 1
puts "- list_patients" if numerical_privilege >= 4
puts "- view_records <patient_id>" if numerical_privilege >= 3
puts "- add_record <patient_id>" if numerical_privilege == 5
puts "- remove_record <patient_id>" if numerical_privilege == 5
command = gets.chomp
parse_command(command,numerical_privilege,user,record_access)
end
def parse_command(command,numerical_privilege,user,record_access)
command = command.split(' ')
if @command_list[command[0]] > numerical_privilege
puts "You lack permissions, peasant!"
show_menu(user,record_access)
end
command.length > 1 ? execute_command(command[0],user,record_access,command[1]) : execute_command(command[0],user,record_access)
end
def execute_command(command, user, record_access, argument = nil)
argument = argument.join(' ') if argument != nil
case
when command == "list_patients" then @database.display_patient_list
when command == "view_records" then @database.display_patient_medical_record(argument)
when command == "add_record" then @database.add_patient_record(Patient.new(argument))
when command == "remove_record" then @database.delete_patient_record(@database.lookup_by_uuid(argument))
else puts "Invalid command"
end
show_menu(user,record_access)
end
end
#execution code
#create a patient
patient = Patient.new("Fellow DBC Student")
patient_zero = Patient.new("Second DBC Student")
patient.add_medical_record("too much battleship")
patient.add_medical_record("not enough baking experience")
patient_zero.add_medical_record("too many nasty ass dishes")
database = Database.new
database.add_patient_record(patient)
database.add_patient_record(patient_zero)
p database.patient_list
puts
#create an employee
doctor = Doctor.new("Doctor Bob")
janitor = Janitor.new("Janitor Steve")
administrator = Administrator.new("Supreme Overlord")
database.add_staff_record(doctor)
database.add_staff_record(janitor)
database.add_staff_record(administrator)
p database.staff_list
database.delete_staff_record(janitor)
p database.staff_list
puts
puts
database.display_patient_list
p database.patient_list
menu = MenuInterface.new(database)
menu.accept_user_input
puts
class Patient < User
attr_accessor :record
def initialize(name)
super(name)
@record_access = :self
@record = {}
end
def add_medical_record(record_to_add)
@record[SecureRandom.uuid] = record_to_add
end
def default_password
"changeme"
end
end
class User
attr_accessor :name, :password, :uuid, :username, :record_access
def initialize(name)
@name = name
@uuid = SecureRandom.uuid
@username = @name.downcase.split(' ')[1] +"."+ @name.downcase.split(' ')[0]
@record_access = :all
@password = default_password
end
def default_password
raise NotImplementedError, "subclasses must define default_password"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment