Created
October 16, 2012 17:42
-
-
Save arn-e/3900798 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
# return puts "Insufficient Privileges" unless access_privilege == :admin || access_privilege == :all | |
@patient_list.sort.each do |key,value| | |
puts "patient UUID : #{key}" | |
puts "patient name : #{value.name}" | |
puts "patient record(s) : #{value.record}" | |
end | |
end | |
def display_patient_record(uuid) | |
# return puts "Insufficient Privileges" unless access_privilege == :admin || access_privilege == :all || (uuid == requestor_uuid) | |
puts "patient UUID : #{uuid}" | |
puts "patient name : #{@patient_list[uuid].name}" | |
puts "patient record(s) : #{@patient_list[uuid].record}" | |
end | |
def add_patient_record(patient_to_add) | |
# return puts "Insufficient Privileges" unless access_privilege == :admin | |
@patient_list[patient_to_add.uuid] = patient_to_add | |
end | |
def add_staff_record(staff_to_add) | |
# return puts "Insufficient Privileges" unless access_privilege == :admin | |
@staff_list[staff_to_add.uuid] = staff_to_add | |
end | |
def delete_patient_record(patient_to_delete) | |
# return puts "Insufficient Privileges" unless access_privilege == :admin | |
@patient_list.delete(patient_to_delete.uuid) | |
end | |
def delete_staff_record(staff_to_delete) | |
# return puts "Insufficient Privileges" unless access_privilege == :admin | |
@staff_list.delete(staff_to_delete.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 | |
#can access all patient records | |
def initialize(name) | |
super | |
@title = "Doctor" | |
end | |
end | |
class Janitor < Employee | |
#can access nothing | |
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 | |
#can access all patient records and add / remove / update patient & staff information | |
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 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> <record_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],command[1]) : execute_command(command[0]) | |
end | |
def execute_command(command,argument = nil) | |
case | |
when command == "list_patients" | |
@database.display_patient_list | |
when command == "view_records" | |
@database.display_patient_record(argument) | |
when command == "add_record" | |
@database.add_patient_record #needs work! | |
when command == "remove_record" | |
@database.delete_patient_record | |
else | |
puts "Invalid command" | |
end | |
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 | |
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,:admin) | |
database.add_patient_record(patient_zero,:admin) | |
p database.patient_list | |
# database.delete_patient_record(patient,:admin) | |
# 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,:admin) | |
database.add_staff_record(janitor,:admin) | |
database.add_staff_record(administrator,:admin) | |
p database.staff_list | |
database.delete_staff_record(janitor,:admin) | |
p database.staff_list | |
puts | |
puts | |
database.display_patient_list(:admin) | |
p database.patient_list | |
menu = MenuInterface.new(database) | |
menu.accept_user_input | |
# menu.validate_credentials("dbc.fellow","changeme") | |
#database.display_patient_record | |
# interface = MenuInterface.new | |
# interface.accept_user_input | |
# def display_patient_record(uuid,requestor_uuid = nil) | |
# def accept_user_input | |
# puts "Please enter your user name : " | |
# input_username = gets.chomp | |
# puts "input username : #{input_username}" | |
# puts "Please enter your password : " | |
# input_password = gets.chomp | |
# puts "password : #{input_password}" | |
# if validate_credentials(input_username,input_password) == false | |
# puts "Incorrect Credentials" | |
# accept_user_input | |
# end | |
# show_menu(input_username) | |
# end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment