Skip to content

Instantly share code, notes, and snippets.

@flakyfilibuster
Created October 19, 2012 02:58
Show Gist options
  • Save flakyfilibuster/3915984 to your computer and use it in GitHub Desktop.
Save flakyfilibuster/3915984 to your computer and use it in GitHub Desktop.
hospital app
class Hospital
attr_accessor :name, :employees, :patients
def initialize(name, location)
@name, @location, @employees, @patients = name, location, [], []
end
end
class Person
attr_reader :name, :clearance, :username, :id
def initialize(name, username, password, id)
@name, @username, @password, @id = name, username, password, id
end
def username
@username
end
def password
@password
end
end
class Patient < Person
attr_reader :name, :username, :password, :condition
def initialize(name, username, password, condition, id)
super(name, username, password, id)
@condition, @clearance = condition, 2
end
end
class Employees < Person
attr_reader :name, :username, :password, :condition
def initialize(name, username, password)
@name, @username, @password, @clearance = name, username, password, condition, 2
end
end
class Doctor < Employees
@clearance = 1
end
class Admin < Employees
@clearance = 0
end
# =>
########################################################
class UserInterface
def initialize(hospital)
@hospital = hospital
@username = ""
@password = ""
welcome
end
def welcome
puts "Welcome to #{@hospital.name}s database interface".ljust(40)
puts "".ljust(40,"-")
login
end
def login
while true
puts "Please enter your username or enter 'exit'"
print ">"
@username = gets.chomp
#puts @username
break if username_check
break if @username == "exit"
end
while true
break if @username == "exit"
puts "Please enter your password or enter 'exit'"
print ">"
@password = gets.chomp
#puts @password.gsub(/./,"*")
break if password_check
break if @password == "exit"
end
select_screen
end
def username_check
array = (@hospital.patients+@hospital.employees).map{|user| user.username}
array.include?(@username) ? true : false
end
def password_check
array = (@hospital.patients+@hospital.employees)
array.each{|user| return user.username == @username && user.password == @password}
end
# def clearance_setter
# array = (@hospital.patients+@hospital.employees)
# array.
def select_screen
prompt =
"""
Please select one of the following options:
------------------------------------------
> 'ls p' to list all patients
> 'ls d' to list all doctors
> 'ap' add patient
> 'rp' remove patient
> 'ad' add doctor
> 'rd' remove doctor
------------------------------------------
"""
puts prompt
print ">"
selection = gets.chomp
list_patients if selection == "ls p"
add_patient if selection == "ap"
remove_patient if selection == "rp"
end
def list_patients
@hospital.patients.each{|patient| puts "ID: #{patient.id} || name: #{patient.name} || username: #{patient.username} || password: #{patient.password} || condition: #{patient.condition}"}
select_screen
end
def add_patient
puts "Patient add screen:"
puts "please specify patients name:"
print ">"
patient_name = gets.chomp
puts "please specify patients username:"
print ">"
patient_username = gets.chomp
puts "please specify patients password:"
print ">"
patient_password = gets.chomp
puts "please specify patients condition:"
print ">"
patient_condition = gets.chomp
id = @hospital.patients.size
@hospital.patients << id = Patient.new(patient_name, patient_username, patient_password, patient_condition, id)
puts "name: #{patient_name} || username: #{patient_username} || password: #{patient_password} || patients condition: #{patient_condition}"
select_screen
end
def remove_patient
puts "Patient remove screen:"
puts "please specify patients ID:"
print ">"
patient_id = gets.chomp.to_i
@hospital.patients do |patient|
if @hospital.patients.id == patient_id
@hospital.patients.delete_at(patient)
end
end
puts "patient removed"
select_screen
end
end
sf_central = Hospital.new("SF Central", "San Francisco")
sf_central.employees << admin = Admin.new("Ferdi Cam", "admin", "12345")
sf_central_mainframe = UserInterface.new(sf_central)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment