Last active
August 29, 2015 14:08
-
-
Save flybayer/b503c67ee1854fc9c960 to your computer and use it in GitHub Desktop.
This file contains 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
# A Parking Lot Simulation by Brandon Bayer | |
# | |
# To run the simulation: | |
# Create a Lot object and a Car object | |
# - lot = ParkingLot.new | |
# - car = Car.new | |
# | |
# Use the following Car methods | |
# - Car#get_ticket(Lot) | |
# - Car#enter(Lot) | |
# - Car#return_ticket | |
# - Car#pay(cost) | |
# - Car#exit | |
# | |
# NOTE: You must use the Car methods to cause the car | |
# to interact with the parking lot. If you directly | |
# call the parking lot methods, the simulation may not work correctly | |
# | |
# NOTE 2: The simulation will not work correctly if you pass | |
# unexpected arguments to the methods (there is no argument verification) | |
require 'time' | |
# Parking lots! | |
class ParkingLot | |
attr_reader :number_of_spaces | |
attr_reader :available_spaces | |
def initialize(number_of_spaces = 10) | |
@number_of_spaces = number_of_spaces | |
@hourly_rate = 5 # dollars | |
@amount_owed = 0 | |
@available_spaces = @number_of_spaces | |
end | |
# Get a ticket from the lot. Returns a ticket if open spots | |
def get_ticket | |
if @available_spaces == 0 | |
p "Sorry, there is currently no room in this parking lot." | |
nil | |
else | |
Ticket.new | |
end | |
end | |
# Return a ticket to the lot. Reports elasped time and cost | |
def return_ticket(ticket) | |
elapsed_hours = (Time.now - ticket.timestamp)/600 | |
p "Your total time in the parking lot is #{elapsed_hours.round(2)} hours" | |
@amount_owed = calculate_cost(@hourly_rate, elapsed_hours) | |
true | |
end | |
# Calculate the parking fee based on elasped time | |
def calculate_cost(hourly_rate, elapsed_hours) | |
if elapsed_hours < 1 | |
p "This is your lucky day, you don't have to pay!" | |
else | |
cost = (elapsed_hours * hourly_rate).floor | |
p "Your parking fee is $#{cost}" | |
end | |
cost ||= 0 | |
end | |
# Accept payment and return any change | |
def pay(paid) | |
# Make sure they owe something | |
if @amount_owed == 0 | |
p "Well, thanks for the offer, but you don't owe anything!" | |
return paid | |
end | |
remaining_balance = @amount_owed - paid | |
if remaining_balance == 0 | |
@amount_owed = 0 | |
p "Thank you! Have a phenomonal day!" | |
true | |
elsif remaining_balance > 0 | |
@amount_owed = remaining_balance | |
p "Thank you, but you still owe $#{remaining_balance}" | |
false | |
elsif remaining_balance < 0 | |
@amount_owed = 0 | |
p "Thank you! Your change is $#{-remaining_balance}." | |
p "Have an outstanding day!" | |
remaining_balance *= -1 # Return the change | |
end | |
end | |
# Lets a car into the parking lot | |
def enter(car) | |
if @available_spaces == 0 | |
p "Oops, sorry, there is currently no more room in this lot" | |
return false | |
elsif car.ticket.nil? | |
p "Hey, not so fast! You must first take a ticket" | |
return false | |
end | |
@available_spaces -= 1 | |
p "You've entered the lot, please stay as long as you like!!" | |
true | |
end | |
# Lets a car out of the parking lot as long as they have paid | |
def exit(car) | |
if car.ticket && @amount_owed > 0 | |
p "Whoa there, buddy! You must pay before leaving!" | |
return false | |
end | |
@available_spaces += 1 | |
p "You have exited the lot. Please come back soon!" | |
true | |
end | |
end | |
# Cars! | |
class Car | |
attr_accessor :ticket | |
@in_parking_lot = false | |
# Attempts to enter a lot, returns the outcome | |
def enter(lot) | |
@lot = lot | |
if @in_parking_lot | |
p "Are you serious? You are already in the lot" | |
return false | |
end | |
if success = @lot.enter(self) | |
@in_parking_lot = true | |
end | |
success | |
end | |
# Attempts to exit a lot, returns the outcome | |
def exit | |
if !@in_parking_lot | |
p "Are you crazy? You can't exit before you enter!" | |
return false | |
end | |
if @lot.exit(self) | |
@in_parking_lot = false | |
return true | |
end | |
false | |
end | |
# Attempts to get a ticket, returns the ticket or false | |
def get_ticket(lot) | |
@lot = lot | |
if @in_parking_lot | |
p "Um, sir, you already have a ticket!" | |
return false | |
end | |
@ticket = @lot.get_ticket | |
end | |
# Attempts to return a ticket | |
def return_ticket | |
if @ticket.nil? && !@in_parking_lot | |
p "Hello? What do you think you are trying to do?" | |
return false | |
end | |
@lot.return_ticket(@ticket) | |
@ticket = nil | |
true | |
end | |
# Pays the specified amount to the lot, returns success or change | |
def pay(amount) | |
@lot.pay(amount) | |
end | |
end | |
# Tickets! | |
class Ticket | |
attr_reader :timestamp | |
def initialize | |
@timestamp = Time.now | |
p "Thank you! Your entry time is #{@time}" | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment