Last active
October 20, 2020 10:37
-
-
Save Haumer/b4c8e91c19b052c3b5c3a7c235a305d3 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 Patient | |
| attr_accessor :room, :id | |
| def initialize(attributes = {}) | |
| @id = attributes[:id] | |
| @name = attributes[:name] | |
| @cured = attributes[:cured] || false | |
| 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 "patient" | |
| require "csv" | |
| class PatientRepository | |
| def initialize(csv_file, room_repository) | |
| @room_repository = room_repository | |
| @csv_file = csv_file | |
| @patients = [] | |
| @next_id = 1 | |
| load_csv | |
| end | |
| def add_patient(patient) | |
| patient.id = @next_id | |
| @patients << patient | |
| @next_id += 1 | |
| end | |
| def load_csv | |
| csv_options = { headers: :first_row, header_converters: :symbol } | |
| CSV.foreach(@csv_file, csv_options) do |row| | |
| row[:id] = row[:id].to_i # Convert column to Integer | |
| row[:cured] = row[:cured] == "true" # Convert column to boolean | |
| room = @room_repository.find(row[:room_id].to_i) | |
| patient = Patient.new(row) | |
| patient.room = room | |
| @patients << patient | |
| @next_id = row[:id] | |
| end | |
| # if @patients.empty? | |
| # @next_id = 1 | |
| # else | |
| # @next_id = @patients.last.id + 1 | |
| # end | |
| @next_id = @patients.empty? ? 1 : @patients.last.id + 1 | |
| end | |
| end | |
| repo = PatientRepository.new("patients.csv") | |
| katherine = Patient.new(name: "kat") | |
| repo.add_patient(katherine) | |
| # p repo |
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 | name | cured | room_id | |
|---|---|---|---|---|
| 1 | anne | true | 1 | |
| 2 | rebecca | false | 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 :capacity | |
| attr_accessor :id | |
| def initialize(attributes = {}) | |
| # STATE | |
| @capacity = attributes[:capacity] || 0 | |
| @patients = attributes[:patients] || [] # Instances of the Patient class | |
| end | |
| # BEHAVIOUR | |
| def full? | |
| @patients.length == @capacity | |
| end | |
| def add_patient(patient) | |
| raise StandardError, "Room is full!" if full? | |
| patient.room = self | |
| @patients << patient | |
| end | |
| end | |
| p room_1 = Room.new(capacity: 1) | |
| # john = Patient.new(name: "john", cured: false) | |
| # steve = Patient.new(name: "steve", cured: false) | |
| # room_1.add_patient(john) | |
| # room_1.add_patient(steve) | |
| # # p room_1 | |
| # p john.room | |
| # room_1.patients | |
| # p room_1.full? |
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 RoomRepository | |
| # def initialize(csv_file) | |
| # @csv_file = csv_file | |
| # @rooms = [] | |
| # load_csv | |
| # end | |
| # def find(id) | |
| # @rooms.select do |room| | |
| # room.id == id | |
| # end.first | |
| # end | |
| # def load_csv | |
| # TODO | |
| # end | |
| # end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment