Created
January 28, 2020 11:14
-
-
Save Oceantidote/1dd6cc12e4513b1310d860c957378de8 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
| class Appointment | |
| attr_accessor :id, :doctor, :patient, :date | |
| def initialize(attributes = {}) | |
| @id = attributes[:id] | |
| @doctor = attributes[:doctor] | |
| @patient = attributes[:patient] | |
| @date = attributes[:date] | |
| end | |
| end |
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
| class AppointmentRepository | |
| def initialize(csv_file_path, patient_repository, doctor_repository) | |
| end | |
| end |
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_relative 'view' | |
| require_relative 'appointment' | |
| class AppointmentsController | |
| def initilaize(doc_repo, patient_repo) | |
| @doctor_repository = doc_repo | |
| @patient_repository = patient_repo | |
| @view = View.new | |
| end | |
| def create_appointment | |
| doctor_id = @view.ask_for_doctor_id | |
| patient_id = @view.ask_for_patient_id | |
| appointment = Appointment.new | |
| appointment.doctor = @doctor_repository.find(doctor_id) | |
| appointment.patient = @patient_repository.find(patient_id) | |
| @appointment_repository.add(appointment) | |
| end | |
| end |
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
| class Doctor | |
| attr_accessor :id | |
| def initialize(attributes) | |
| @id = attributes[:id] | |
| @name = attributes[:name] | |
| end | |
| end |
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
| class Patient | |
| attr_reader :name, :cured | |
| attr_accessor :room, :id | |
| def initialize(attributes = {}) | |
| @name = attributes[:name] | |
| @id = attributes[:id] | |
| @cured = attributes[:cured] || false | |
| end | |
| def cure | |
| @cured = true | |
| end | |
| end |
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 'csv' | |
| require_relative "patient" | |
| require_relative 'room_repository' | |
| class PatientRepository | |
| attr_reader :patients | |
| def initialize(file_path, room_repository) | |
| @room_repository = room_repository | |
| @csv_file = file_path | |
| @patients = [] | |
| @next_id = 1 | |
| load_csv | |
| end | |
| private | |
| def save_csv | |
| csv_options = { headers: :first_row, header_converters: :symbol } | |
| CSV.open(@csv_file, "wb", csv_options) do |csv| | |
| csv << ["id","name","cured","room_id"] | |
| @patients.each do |patient| | |
| csv << [patient.id, patient.name,patient.cured,patient.room.id] | |
| end | |
| end | |
| end | |
| def load_csv | |
| csv_options = { headers: :first_row, header_converters: :symbol } | |
| @next_id = 0 | |
| CSV.foreach(@csv_file, csv_options) do |row| | |
| row[:id] = row[:id].to_i # Convert column to Integer | |
| row[:cured] = row[:cured] == "true" | |
| row[:room_id] = row[:room_id].to_i | |
| patient = Patient.new(row) | |
| patient.room = @room_repository.find(row[:room_id]) | |
| # patient.room.add_patient(patient) | |
| patients << patient | |
| @next_id = patient.id | |
| end | |
| @next_id += 1 | |
| end | |
| end | |
| room_repo = RoomRepository.new("rooms.csv") | |
| repo = PatientRepository.new("patients.csv", room_repo) | |
| p repo.patients |
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
| id | cured | name | room_id | |
|---|---|---|---|---|
| 1 | FALSE | bob | 2 | |
| 2 | true | shane | 2 | |
| 3 | FALSE | tony | 1 |
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_relative "patient" | |
| class Room | |
| attr_reader :patients, :capacity | |
| attr_accessor :id | |
| def initialize(attributes = {}) | |
| @capacity = attributes[:capacity] || 0 | |
| @id = attributes[:id] | |
| @patients = attributes[:patients] || [] | |
| end | |
| def full? | |
| @capacity == @patients.length | |
| end | |
| def add_patient(patient) | |
| raise RoomFullException, "Room is full!" if full? | |
| @patients << patient | |
| patient.room = self | |
| end | |
| def puts_self | |
| p self.capacity | |
| end | |
| end | |
| # room = Room.new(capacity: 2) | |
| # room_two = Room.new(capacity: 3) | |
| # room.puts_self | |
| # room_two.puts_self | |
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 'csv' | |
| require_relative "room" | |
| class RoomRepository | |
| attr_reader :rooms | |
| def initialize(file_path) | |
| @csv_file = file_path | |
| @rooms = [] | |
| @next_id = 1 | |
| load_csv | |
| end | |
| def find(id) | |
| @rooms[id - 1] | |
| end | |
| def add_room(room) | |
| room.id = @next_id | |
| @rooms << room | |
| @next_id += 1 | |
| end | |
| private | |
| def save_csv | |
| csv_options = { headers: :first_row, header_converters: :symbol } | |
| CSV.open(@csv_file, "wb", csv_options) do |csv| | |
| csv << ["id","capacity"] | |
| @rooms.each do |room| | |
| csv << [room.id, room.capacity] | |
| end | |
| end | |
| end | |
| def load_csv | |
| csv_options = { headers: :first_row, header_converters: :symbol } | |
| @next_id = 0 | |
| CSV.foreach(@csv_file, csv_options) do |row| | |
| row[:id] = row[:id].to_i | |
| @next_id = row[:id] # Convert column to Integer | |
| @rooms << Room.new(row) | |
| end | |
| @next_id += 1 | |
| end | |
| end | |
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
| id | capacity | |
|---|---|---|
| 1 | 3 | |
| 2 | 3 | |
| 3 | 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment