2

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:

  1. adding 5 guests (not to a db or textfile only to a array or so) when the program starts
  2. 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 
1
  • You may want to use a hash to represent the relationship between a user and a room.You may have multiple guests per room. Commented Aug 23, 2010 at 14:03

3 Answers 3

1

Creating an array:

number_array = [1, 2, 3, 4, 5]

Accessing the elements of an array:

number_array[2]
# this would return the integer 3

Adding a new element to an array:

number_array << 6
# this would return [1, 2, 3, 4, 5, 6]

You can create a new guest by doing something like this:

Guest.new("John", "Doe", "1500 main street", "123-456-7890", "1/1/2010")

Since this is a homework assignment, I'll leave it to you to combine everything into a working solution ;)

Sign up to request clarification or add additional context in comments.

Comments

0

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 :

empty_rooms = (1..20).to_array

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

2 Comments

Thanks! Im not sure in what class i should put the code. Any tip?
If you only have Guest class and the main program, I guess I can answer you with this question : is a Guest responsible for selecting a room ?
0

I think what you mean is that you want 5 guest objects. You could put them in an array by creating an array literal and then adding guests to it

@guests = []
@guests << Guest.new()
@guests << Guest.new()

now your @guests array has two guests, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.