2010-08-02 102 views
2

嗨我得到一個核心錯誤,這是真正的標準,我想在Ruby中,但不知道該怎麼做。我有一個我寫的程序。其目的是在露營地註冊客人。你有一個菜單有5個不同的選項。 1.簽入。當我這樣做時,我得到一個未定義的方法generateParkingLot' for #<Camping:0x10030a768> (NoMethodError) When I choose Checkout I get a undefined local variable or method出發'爲菜單:類(NameError)。 所以,請讓我有人對我的問題有一個線索,那將會很棒。我將在這裏粘貼我的所有代碼。代碼被分隔在不同的文件中,我已經使用require來提供不同的文件。雖然我將把所有的代碼粘貼到一條曲線中。感謝所有幫助。幫助! check_in':未定義的方法`推'爲零:NilClass(NoMethodError)

-Sebastien

require 'guest' 

require 'parking_lot' 

class Camping 

attr_accessor :current_guests, :parking_lots, :all_guests, :staticGuests 

def initialize(current_guests, parking_lots, all_guests, staticGuests) 

    @current_guests = Array.new() 

# initiera husvagnsplatserna 
@parking_lots = Array.new(32) 
32.times do |nr| 
    @parking_lots[nr] = Parking_Lot.new(nr) 

    @staticGuests = Array[ 
    Guest.new("Logan Howlett", "Tokyo", "07484822",1, @parking_lots[0]), 
    Guest.new("Scott Summers", "Chicago", "8908332", 2, @parking_lots[1]), 
     Guest.new("Hank Moody", "Boston", "908490590", 3, @parking_lots[2]), 
     Guest.new("Jean Grey", "Detroit", "48058221", 4, @parking_lots[3]), 
     Guest.new("Charles Xavier","Washington DC", "019204822",5, @parking_lots[4]) 
      ] 

    end 

@all_guests = []  

    @staticGuests.each do |guest| 
    @current_guests[guest.plot.nr] = guest 
    @all_guests.push(guest) 
end        
end 


def to_s 
    # creates an empty string 
    list = " " 

    # loop from 1 to 32 
    (1..32).each do |n| 
     if ([email protected]_guests[n-1].nil?) 
      list += @current_guests[n-1].to_s 
     else 
      # else adds the text "Vacant" 
      list += n.to_s + ": Vacant!\n" 
     end 
    return list 
end 

def generateParkingLot 

    randomNr = 1+rand(32) 
    # exists a guest at the (0-based) position? 
    if ([email protected]_guests[randomNr-1].nil?) 
     # if so generate a new figure 
     generateEmpty(array) 
    else 
     # returns the generated number 
     return randomNr 
    end 
    end 
    end 
    end 

class Guest 

attr_accessor :firstname, :lastname, :address, :phone, :departure 
attr_reader :arrived, :plot 


def initialize (firstName, lastName, address, phone, plot) 
@firstName = firstName 
@lastName = lastName 
@address = address 
@phone = phone 
@arrived = arrived 
@plot = plot   

def to_s 
    "Personal information: 
    (#{@firstName}, #{@lastName}, #{@address}, #{@phone}, #{@arrived}, #{@departure},  #{@plot})" 
    end 
end      


require 'ruby_camping' 
require 'camping_guests' 

class Main 

if __FILE__ == $0 
    $camping = Camping.new(@current_guests, @all_guests, @parking_lots,@staticGuests) 
    puts "\n" 
    puts "Welcome to Ruby Camping!" 

while (true) 
    Menu.menu 
    end 
end 
end 


require 'date'      
require 'camping_guests' 
require 'guest' 

class Menu 

def initialize(guests = []) 
    @camping = Camping.new(guests) 
end 



def self.menu 
    puts "---------------------------" 
    puts "  Menu"       
    puts " 1. Checkin" 
    puts " 2. Checkout" 
    puts " 3. List current guests" 
    puts " 4. List all guests" 
    puts " 5. Exit\n" 
    puts "" 
    puts " What do you want to do?" 
    puts "---------------------------" 
    print ": " 
    action = get_input 
    do_action(action) 
end 

# fetches menu choice and returns chosen alternativ 
def self.get_input 
    input = gets.chomp.to_i 

while (input > 5 || input < 1) do 
    puts "Ooups, please try again." 
    input = gets.chomp.to_i 
end 
    return input 
end 

def self.do_action(action) 
    case action 
    when 1: 
     check_in 
    when 2: 
     check_out 
    when 3: 
     puts $camping.current_guests 
     when 4: 
     puts $camping.all_guests 
     when 5: 
     puts "You are now leaving the camping, welcome back!" 
     exit  
    end 
    end 


def self.check_in 
    puts "Welcome to the checkin" 
    puts "Please state your first name: " 
    firstName = gets.chomp 
    puts "Please state your last name:" 
    lastName = gets.chomp 
    puts "Write your address: " 
    address = gets.chomp 
    puts "and your phone number: " 
    phone = gets.chomp 
    puts "finally, your arrival date!" 
    arrived = gets.chomp 
    newPLot = $camping.generateParkingLot 
    newGuest = Guest.new(firstName, lastName, address, phone,arrived,$camping.parking_lots[newPLot-1]) 
    $camping.current_guests[newPLot-1] = newGuest 
    @all_guests.push(newGuest) 
    puts "The registration was a success!! You have received the " + newPLot.to_s + "." 
    end       

    def self.check_out 
    puts "Welcome to checkout!" 
    puts $camping.all_guests 
    puts "State plot of the person to checkout!" 
    plot = gets.chomp.to_i 
    puts "Ange utcheckningsdatum: " 
    departureDate = gets.chomp.to_i 
    guest = $camping.current_guests[plot-1]  
    @departure = departure 
    guest.departure = departureDate 
    guestStayedDays = departureDate - guest.arrived  
    guest.plot.increase(guestStayedDays)     
    puts guest          
    $camping.current_guests[plot-1] = nil    
    end 
end 


class Parking_Lot 

attr_accessor :nr 
attr_reader :electricity_meter 

def initialize (nr) 
    @nr = nr 
    @electricity_meter = 4000-rand(2000)  
    end 

def increase_meter(days) 
    generatedUse = (10+rand(70))*days 
    puts "Increases the meter with " + generatedUse.to_s + " kWh." 
    @electricity_meter += generatedUse 
end 

def to_s 

    "Plot #{@nr+1} Electricity meter: #{@electricity_meter} kWh" 

end 
end 
+1

您的文章有一些嚴重的格式問題。請編輯。 – 2010-08-02 13:54:01

+0

是的,你需要在每行代碼的開始處放置四個空格,因此它被格式化爲代碼 – 2010-08-02 14:14:01

+0

感謝幫助格式化代碼的人。 – Sebastien 2010-08-02 14:44:48

回答

1

它看起來(雖然我還沒有嘗試過這一點),像一些你對你「端的是錯誤的。

的一個,這是造成你的第一個錯誤(generateParkingLot未定義)是generateParkingLot實際上是定義 to_s,所以你需要在你的to_s方法的最後一個額外的「結束」。

至於第二誤差(無法識別出發),在self.check_out如下因素線處於故障:

@departure = departure 

,因爲沒有「出發」的變量。 (也許你的意思是DepartureDate?)。我懷疑此代碼可能存在其他一些問題,但恐怕我現在沒有時間檢查。

一個我注意到的是,當你有

32.times do |nr| 
    @parking_lots[nr] = Parking_Lot.new(nr) 

我想你可能想結束這種,要麼被「端」或大括號,例如

32.times do |nr| 
    @parking_lots[nr] = Parking_Lot.new(nr) 
end 

雖然這將使你的其他塊不匹配。一般情況下,只要嘗試,並確保你的塊都被定義恰當(例如,任何事物都有一個匹配的結束)。

+0

感謝您的幫助格蘭特。我照你說的做了,並開始檢查他的街區,看看他們都有匹配的結局。雖然沒有發現任何遺漏。你有什麼線索?! – Sebastien 2010-08-03 14:06:20

+0

你知不知道你需要結束那些'做'循環,就像我的回覆中的** 32 times do | nr | **一樣?它不像C#等,你可以在沒有結束塊的情況下擁有一行代碼,並且在執行return語句時不會結束。這就是爲什麼你的generateParkingLot方法不起作用,因爲你的**(1..32)。to_s中的每個do | n | **方法都沒有關閉,所以to_s方法沒有關閉。在你的to_s方法的末尾放上另一個結尾,你至少應該讓generateParkingLot方法工作。 – 2010-08-03 21:43:23

+0

棘手找到一個體面的教程只是'做'塊,但這可能有所幫助:http://ruby.about.com/od/rubyfeatures/a/loops_2.htm – 2010-08-03 21:48:22

相關問題