So i need some support with my Ruby assignment, I’m not from US so you have to excuse my English.
We are building a hotel and this is the second assignment. It’s a console based application and I have a class called main.rb that handles the runtime and a guest-class.
In this second assignment we are to preload the app with five guest-objects, I guess I have to use an array but don’t really know how. Below are my guest class and my main class is simply a while-loop with a case statement.
I need help with:
- adding 5 guests (not to a db or textfile only to a array or so) when the program starts
- the hotel has 20 rooms and i need to randomize the room number and exclude already rented rooms
Hope you can help! Thanks!
class Guest
#Instance variables.
attr_accessor :firstName,
:lastName,
:address,
:phone,
:arrival,
:plot,
:gauge
#Constructor sets the guest details.
def initialize(first, last, adress, phone, arrival)
@firstName = first
@lastName = last
@address = address
@phone = phone
@arrival = arrival
@plot = range_rand(1,32)
@gauge = range_rand(2000,4000)
end
#Using rand()-method to randomize a value between min and max parameters.
def range_rand(min,max)
min + rand(max-min)
end
def to_string
"Name = #{@firstName} , Plot = #{@plot}"
end
end
Other people have already answered the first part of your question, so I’ll help you with the second one (I’ll provide the minimum, so that you still have some work to do 🙂 )
You could create an array containing the 20 room numbers :
Then for each guest :
1) Take a random number in this array ( hint : randomize the index )
2) Remove this number from the array
3) And assign the room number to a Guest
4) Add the guest to the array of guests