Skip to content

Instantly share code, notes, and snippets.

@SyunWatanabe
Created February 15, 2020 14:44
Show Gist options
  • Select an option

  • Save SyunWatanabe/802c68a8084ed95ea8225e4918eebd72 to your computer and use it in GitHub Desktop.

Select an option

Save SyunWatanabe/802c68a8084ed95ea8225e4918eebd72 to your computer and use it in GitHub Desktop.
# 改札機プログラム(自力作)
STATION = ['harajyuku', 'omotesando', 'nogizaka']
class Gate
attr_accessor :name
def initialize(name)
@name = name
end
def enter(ticket)
@@from_station = STATION.index(self.name.to_s)
@price = ticket.price
end
def exit(ticket)
@@dest_station = STATION.index(self.name.to_s)
@price = ticket.price
length = @@dest_station - @@from_station
case @price
when 150
puts length < 2
when 190
puts length < 3
end
end
end
class Ticket
attr_accessor :price
def initialize(price)
@price = price
end
end
# 改札機オブジェクトの作成
harajyuku = Gate.new(:harajyuku)
omotesando = Gate.new(:omotesando)
nogizaka = Gate.new(:nogizaka)
# 150円の切符を原宿で購入し乃木坂で降車する(NG)
ticket = Ticket.new(150)
harajyuku.enter(ticket)
nogizaka.exit(ticket) # => false
# 190円の切符を原宿で購入し乃木坂で降車する(OK)
ticket = Ticket.new(190)
harajyuku.enter(ticket)
nogizaka.exit(ticket) # => true
# 150円の切符を原宿で購入し表参道で降車する(OK)
ticket = Ticket.new(150)
harajyuku.enter(ticket)
omotesando.exit(ticket) # => true
# 150円の切符を表参道で購入し乃木坂で降車する(OK)
ticket = Ticket.new(150)
omotesando.enter(ticket)
nogizaka.exit(ticket) # => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment