-
-
Save csmalin/51f322ba8acb07680c1d to your computer and use it in GitHub Desktop.
Solution for Hospital Interface
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
# Solution for Challenge: Hospital Interface. Started 2013-02-27T02:57:40+00:00 | |
require 'rubygems' | |
require 'highline/import' | |
class Hospital | |
attr_reader :name, :city, :employees, :patients | |
def initialize(name, city, capacity=500) | |
@name = name | |
@city = city | |
@capacity = capacity | |
@employees = [] | |
@patients = [] | |
end | |
def hire_employee(name, position, username, password) | |
@employees << Employee.new(name, position, username, password) | |
end | |
def admit_patient(name, illness) | |
@patients << Patient.new(name, illness) | |
end | |
end | |
class System | |
def initialize(hospital) | |
@hospital = hospital | |
@attempted_username = nil | |
@password_try_counter = 2 | |
end | |
def login | |
puts "Welcome to #{@hospital.name} in #{@hospital.city}" | |
puts "------------------------------------------------" | |
ask_username | |
puts "------------------------------------------------" | |
end | |
def ask_username | |
username = ask("Enter your username: ") | |
username(username) | |
end | |
def ask_password | |
password = ask("Enter super-secret password (#{@password_try_counter + 1} attempts remaining): ") { |q| q.echo = "*" } | |
password(password) | |
end | |
def username(username) | |
username_list = [] | |
@hospital.employees.each_with_index do |x, i| | |
username_list << x.username | |
@attempted_username = i | |
end | |
if username_list.include?(username) | |
ask_password | |
else | |
puts "User Not Found, Try again" | |
ask_username | |
end | |
end | |
def password(password) | |
until @password_try_counter == 0 | |
if @hospital.employees[@attempted_username].password == password | |
menu | |
@password_try_counter = 2 | |
break | |
else | |
puts "Wrong password, please try again." | |
@password_try_counter -= 1 | |
ask_password | |
end | |
end | |
if @password_try_counter == 0 | |
puts "Authentication failed, goodbye." | |
exit | |
end | |
end | |
def menu | |
puts "------------------------------------------------" | |
puts "What would you like to do?" | |
puts "Options:" | |
puts "1. List patients" | |
puts "2. View records <patient id>" | |
puts "3. Add record <patient id>" | |
puts "4. Remove patient <patient id>" | |
puts "5. Exit" | |
selection = ask("Enter number: ") | |
case selection.to_i | |
when 1 then list_patients | |
when 2 then view_records | |
when 3 then add_record | |
when 4 then delete_record | |
when 5 then exit | |
else menu | |
end | |
end | |
def list_patients | |
@hospital.patients.each do |x| | |
puts "#{x.name} #{x.patient_id}" | |
end | |
menu | |
end | |
def view_records | |
entered_id = ask("Please enter the patient ID: ") | |
@hospital.patients.each do |x| | |
puts x if x.patient_id.to_i == entered_id.to_i | |
end | |
menu | |
end | |
def add_record | |
name = ask("Please enter the patient's name: ") | |
illness = ask("Please enter the patient's illness: ") | |
@hospital.admit_patient(name, illness) | |
menu | |
end | |
def delete_record | |
entered_id = ask("Please enter the patient ID: ") | |
delete_index = nil | |
@hospital.patients.each_with_index do |x, i| | |
delete_index = i if x.patient_id.to_i == entered_id.to_i | |
end | |
@hospital.patients.delete_at(delete_index) | |
menu | |
end | |
end | |
class Patient | |
attr_reader :patient_id, :name, :illness | |
def initialize(name, illness) | |
@name = name | |
@illness = illness | |
@patient_id = self.object_id | |
end | |
def to_s | |
puts "Name: #{name}" | |
puts "Illness: #{illness}" | |
puts "Patient ID: #{patient_id}" | |
end | |
end | |
class Employee | |
attr_reader :name, :position, :username, :password | |
def initialize(name, position, username, password) | |
@name = name | |
@position = position | |
@username = username | |
@password = password | |
end | |
end | |
hospital = Hospital.new('DBC Hospital', 'San Francisco') | |
hospital.hire_employee('Chris Malin', 'Head Neurosurgeon', 'chris', 'thepassword') | |
hospital.admit_patient('David Wen', 'Coding-phobia') | |
system = System.new(hospital) | |
system.login | |
# _,,aaaaa,,_ | |
# _,dP"'' `""""Ya,_ | |
# ,aP"' `"Yb,_ | |
# ,8"' `"8a, | |
# ,8" `"8,_ | |
# ,8" "Yb, | |
# ,8" `8, | |
# dP' 8I | |
# ,8" bg,_ ,P' | |
# ,8' "Y8"Ya,,,,ad" | |
# ,d" a,_ I8 `"""' | |
# ,8' ""888 | |
# dP __ `Yb, | |
# dP' _,d8P::::Y8b, `Ya | |
# ,adba8',d88P::;;::;;;:"b:::Ya,_ Ya | |
# dP":::"Y88P:;P"""YP"""Yb;::::::"Ya, "Y, | |
# 8:::::::Yb;d" _ "_ dI:::::::::"Yb,__,,gd88ba,db | |
# Yb:::::::"8(,8P _d8 d8:::::::::::::Y88P"::::::Y8I | |
# `Yb;:::::::""::"":b,,dP::::::::::::::::::;aaa;:::8( | |
# `Y8a;:::::::::::::::::::::;;::::::::::8P""Y8)::8I | |
# 8b"ba::::::::::::::::;adP:::::::::::":::dP::;8' | |
# `8b;::::::::::::;aad888P::::::::::::::;dP::;8' | |
# `8b;::::::::""""88" d::::::::::b;:::::;;dP' | |
# "Yb;::::::::::Y8bad::::::::::;"8Paaa""' | |
# `"Y8a;;;:::::::::::::;;aadP"" | |
# ``""Y88bbbdddd88P""8b, | |
# _,d8"::::::"8b, | |
# ,dP8"::::::;;:::"b, | |
# ,dP"8:::::::Yb;::::"b, | |
# ,8P:dP:::::::::Yb;::::"b, | |
# _,dP:;8":::::::::::Yb;::::"b | |
# ,aaaaaa,,d8P:::8":::::::::::;dP:::::;8 | |
# ,ad":;;:::::"::::8"::::::::::;dP::::::;dI | |
# dP";adP":::::;:;dP;::::aaaad88"::::::adP:8b,___ | |
#d8:::8;;;aadP"::8'Y8:d8P"::::::::::;dP";d"'`Yb:"b | |
#8I:::;""":::::;dP I8P"::::::::::;a8"a8P" "b:P | |
#Yb::::"8baa8"""' 8;:;d"::::::::d8P"' 8" | |
# "YbaaP::8;P `8;d::;a::;;;;dP ,8 | |
# `"Y8P"' Yb;;d::;aadP" ,d' | |
# "YP:::"P' ,d' | |
# "8bdP' _ ,8' | |
# ,8"`""Yba,d" ,d" | |
# ,P' d"8' ,d" | |
# ,8' d'8' ,P' | |
# (b 8 I 8, | |
# Y, Y,Y, `b, | |
# ____ "8,__ `Y,Y, `Y""b, | |
# ,adP""""b8P""""""""Ybdb, Y, | |
# ,dP" ,dP' `"" `8 | |
# ,8" ,P' ,P | |
# 8' 8) ,8' | |
# 8, Yb ,aP' | |
# `Ya Yb ,ad"' | |
# "Ya,___ "Ya ,ad"' | |
# ``""""""`Yba,,,,,,,adP"' | |
# `"""""""' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment