Skip to content

Instantly share code, notes, and snippets.

@Junee01
Last active August 20, 2016 00:43
Show Gist options
  • Save Junee01/dfbd6bf9de3a58f6fe75f7eb9a24da2b to your computer and use it in GitHub Desktop.
Save Junee01/dfbd6bf9de3a58f6fe75f7eb9a24da2b to your computer and use it in GitHub Desktop.
Design ParkingLot to code with Ruby
#Car ํด๋ž˜์Šค๋Š” ์ฃผ์ฐจํ•˜๋Ÿฌ์˜จ ์ฐจ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
class Car
attr_reader :plateNumber
#์ฐจ๋Ÿ‰ ๋“ฑ๋ก ๋ฒˆํ˜ธ๋ฅผ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค. ์ฐจ๋Ÿ‰ ๋ฒˆํ˜ธ๋Š” ์šฐ์„  String ํ˜•ํƒœ๋กœ ๋˜์–ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค.
def initialize plateNumber
@plateNumber = plateNumber #์ฐจ๋Ÿ‰ ๋“ฑ๋ก๋ฒˆํ˜ธ
end
end
#Lot ํด๋ž˜์Šค๋Š” ๊ฐ ์ฃผ์ฐจ๊ณต๊ฐ„์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
class Lot
attr_reader :spaceNumber, :available, :parkedCarNumber
#์ฃผ์ฐจ ๊ณต๊ฐ„ ํ•˜๋‚˜ํ•˜๋‚˜์— ๋Œ€ํ•œ ์ดˆ๊ธฐํ™”์ž…๋‹ˆ๋‹ค.
def initialize spaceNumber
@spaceNumber = spaceNumber #์ฃผ์ฐจ ๊ณต๊ฐ„์˜ ๋ฒˆํ˜ธ
@available = true #๋น„์–ด์žˆ๋Š”์ง€ ์—ฌ๋ถ€
@parkedCarNumber = "" #์ฃผ์ฐจ๋œ ์ฐจ๋Ÿ‰์˜ ๋ฒˆํ˜ธ
end
#parkinglot ๋ถ€๋ถ„์—์„œ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ๊ณต๊ฐ„์ด ์žˆ๋Š”์ง€, ์ฃผ์ฐจ๋œ ์ฐจ์™€ ์ผ์น˜ํ•˜๋Š” ๊ฒƒ์ด ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜์—ฌ ๊ฑธ๋Ÿฌ๋‚ด๋ฏ€๋กœ ์—ฌ๊ธฐ์„œ๋Š” ์ฃผ์ฐจ์™€ ๋ฐ˜ํ™˜๋งŒ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค.
#๊ณต๊ฐ„์ด ์žˆ๋‹ค๋ฉด ์ฐจ๋ฅผ ์ฃผ์ฐจ๋ฅผ ์™„๋ฃŒํ•ฉ๋‹ˆ๋‹ค.
def park car
@available = false
@parkedCarNumber = car.plateNumber
print car.plateNumber.to_s + "์ฐจ๋Ÿ‰์„" + self.spaceNumber.to_s + "์— ์ฃผ์ฐจํ–ˆ์Šต๋‹ˆ๋‹ค.\n"
end
#์š”์ฒญํ•œ ๋ฒˆํ˜ธํ™” ์ฃผ์ฐจ๋œ ์ฐจ ๋ฒˆํ˜ธ๋ฅผ ๋น„๊ตํ•˜๊ณ  ๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•œ๋‹ค๋ฉด ์ฐจ๋ฅผ ๋บ๋‹ˆ๋‹ค.
def pull plateNumber
print @parkedCarNumber + "์ฐจ๋Ÿ‰์„ ์„ฑ๊ณต์ ์œผ๋กœ ์ฐจ๋ฅผ ๋บ์Šต๋‹ˆ๋‹ค.\n"
@available = true
@parkedCarNumber = ""
end
end
#Parkinglot Class๋Š” ์ฃผ์ฐจ์žฅ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
class ParkingLot
attr_reader :lots
#์ฃผ์ฐจ์žฅ์„ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค. 'capacity'๊ฐœ์˜ Lots๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
def initialize capacity
@lots = capacity.times.map { |i| Lot.new(i) }
print capacity.to_s + "๊ฐœ์˜ ๊ณต๊ฐ„์„ ๊ฐ€์ง„ ์ฃผ์ฐจ์žฅ์ด ๋งŒ๋“ค์–ด์กŒ์Šต๋‹ˆ๋‹ค.\n"
end
#์ฃผ์ฐจ ์š”์ฒญ์„ ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค.
def storeCar car
spaceAvailable = getAvailableSpace
if spaceAvailable != false
spaceAvailable.park(car)
else
print "๊ณต๊ฐ„์ด ๋ถ€์กฑํ•˜์—ฌ ์ฃผ์ฐจํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\n"
return false
end
end
#์ฐจ ๋ฐ˜ํ™˜ ์š”์ฒญ์„ ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค.
def getCar plateNumber
spaceOfCar = findCar(plateNumber)
if spaceOfCar != false
spaceOfCar.pull(plateNumber)
else
return false
end
end
#์ฃผ์ฐจ์žฅ์— ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ๊ณต๊ฐ„์ด ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค.
#์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ๊ณต๊ฐ„์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๊ฒ€์ƒ‰์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.
def getAvailableSpace
@lots.each do |lot|
if lot.available == true
return lot
end
end
return false
end
#์ฃผ์ฐจ๋˜์–ด์žˆ๋Š” ์ฐจ ์ค‘ ํ•ด๋‹น ๋ฒˆํ˜ธ์˜ ์ฐจ๊ฐ€ ์ฃผ์ฐจ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค.
#์กด์žฌํ•œ๋‹ค๋ฉด ํ•ด๋‹น ์ฐจ๋Ÿ‰์˜ ๊ณต๊ฐ„์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๊ฒ€์ƒ‰์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.
def findCar plateNumber
@lots.each do |lot|
if lot.parkedCarNumber == plateNumber
return lot
end
end
print "๊ทธ๋Ÿฌํ•œ ๋ฒˆํ˜ธ์˜ ์ฐจ๋Ÿ‰์€ ์—†์Šต๋‹ˆ๋‹ค.\n"
return false
end
end
#์ฃผ์ฐจ ๊ณต๊ฐ„์€ ์ฒ˜์Œ ์ดํ›„๋กœ ๋ณ€ํ™”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋Š˜ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ์ƒํƒœ๋ผ๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค.
#์ฃผ์ฐจ ๊ณต๊ฐ„์˜ ํฌ๊ธฐ๋Š” ์–ด๋– ํ•œ ์ฐจ๋ผ๋„ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ์„ ์ •๋„๋กœ ํ•œ ๊ณต๊ฐ„์ด ์—ฌ์œ ๋กญ์Šต๋‹ˆ๋‹ค. ๋•Œ๋ฌธ์— ์ฐจ์˜ ์ข…๋ฅ˜๋‚˜ ํฌ๊ธฐ๋Š” ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
#์ฃผ์ฐจ ๊ฐ€๊ฒฉ ๋ฐ ์ด์šฉ์‹œ๊ฐ„์€ ๋ฌด๋ฃŒ/๋ฌด์ œํ•œ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋จผ์ € ์„ ์ ํ•œ ์‚ฌ๋žŒ์ด ์ฃผ์ธ์ด ๋ฉ๋‹ˆ๋‹ค. ์ฐจ๋ฅผ ๋นผ๋Š” ์ˆœ๊ฐ„ ๊ถŒํ•œ์ด ์‚ฌ๋ผ์ง‘๋‹ˆ๋‹ค.
#์˜ˆ์™ธ์ฒ˜๋ฆฌ๋Š” ๋ชจ๋‘ return fasle๋กœ ํ•ด๋†“์•˜์Šต๋‹ˆ๋‹ค.
#์ฃผ์ฐจ ๊ณต๊ฐ„์€ 0๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ๊ฐ€๊นŒ์šด ๊ณณ์— ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ , ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๊ณณ๋ถ€ํ„ฐ ์ฐจ๋ก€์ฐจ๋ก€ ์ฑ„์›Œ ๋„ฃ๋Š”๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค.
require_relative('../ChattingCat_Assignment/car')
require_relative('../ChattingCat_Assignment/lot')
require_relative('../ChattingCat_Assignment/parkinglot')
ChattingCatPark = ParkingLot.new(5) #์ฃผ์ฐจ ๊ณต๊ฐ„์„ ํ•˜๋‚˜ ๋งŒ๋“ค๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฃผ์ฐจ ๊ณต๊ฐ„์€ 5๊ฐœ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
Cars = Array.new
10.times do |i|
Cars << Car.new("Car " + i.to_s) #์ž๋™์ฐจ๋ฅผ "Car 0" ~ "Car 9"๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
end
print "10๊ฐœ์˜ ์ฐจ๊ฐ€ ๋งŒ๋“ค์–ด์กŒ์Šต๋‹ˆ๋‹ค.\n"
#10๊ฐœ์˜ ์ž๋™์ฐจ๋ฅผ 5๊ฐœ์˜ ๊ณต๊ฐ„์— ์ฐจ๋ก€์ฐจ๋ก€ ์ฃผ์ฐจํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
#์˜ˆ์ƒ๋Œ€๋กœ๋ผ๋ฉด, ์•ž์˜ 5๊ฐœ๋Š” ์„ฑ๊ณต ๋’ค์˜ 5๊ฐœ๋Š” ์‹คํŒจ์ž…๋‹ˆ๋‹ค.
Cars.each do |car|
ChattingCatPark.storeCar(car)
end
#์ฐจ Car 0, Car 3์— ํ•ด๋‹นํ•˜๋Š” ์ฐจ๋ฅผ ๋นผ๊ฒ ์Šต๋‹ˆ๋‹ค.
#๋˜ํ•œ Car 000๋ผ๋Š” ๊ฑธ ๋นผ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฐ ์ฐจ๋Š” ์ฃผ์ฐจ๋˜์–ด ์žˆ์ง€ ์•Š๊ธฐ๋•Œ๋ฌธ์— ์‹คํŒจ์ž…๋‹ˆ๋‹ค.
ChattingCatPark.getCar("Car 0")
ChattingCatPark.getCar("Car 3")
#์ฃผ์ฐจ ๊ณต๊ฐ„์ด 2๊ฐœ๊ฐ€ ๋‚˜์™”๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ ์ค‘ 2๊ฐœ๊ฐ€ ์„ฑ๊ณตํ•ฉ๋‹ˆ๋‹ค.
ChattingCatPark.storeCar(Cars[5]) #๊ฐ๊ฐ 0
ChattingCatPark.storeCar(Cars[6]) #3์ด ์ฃผ์ฐจ๋˜์–ด์žˆ๋˜ ๊ณต๊ฐ„์— ์ฃผ์ฐจ๋ฉ๋‹ˆ๋‹ค.
ChattingCatPark.getCar("Car 000") #ํ•ด๋‹น ์ฐจ ๋ฒˆํ˜ธ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์—†๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
@Junee01
Copy link
Author

Junee01 commented Aug 19, 2016

์ฐธ๊ณ ๋กœ ํ…Œ์ŠคํŠธ๋Š” ๋Œ๋ ค๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผํ™”๋ฉด์ด ๋‚˜์˜ต๋‹ˆ๋‹ค.

5๊ฐœ์˜ ๊ณต๊ฐ„์„ ๊ฐ€์ง„ ์ฃผ์ฐจ์žฅ์ด ๋งŒ๋“ค์–ด์กŒ์Šต๋‹ˆ๋‹ค.
10๊ฐœ์˜ ์ฐจ๊ฐ€ ๋งŒ๋“ค์–ด์กŒ์Šต๋‹ˆ๋‹ค.
Car 0์ฐจ๋Ÿ‰์„0์— ์ฃผ์ฐจํ–ˆ์Šต๋‹ˆ๋‹ค.
Car 1์ฐจ๋Ÿ‰์„1์— ์ฃผ์ฐจํ–ˆ์Šต๋‹ˆ๋‹ค.
Car 2์ฐจ๋Ÿ‰์„2์— ์ฃผ์ฐจํ–ˆ์Šต๋‹ˆ๋‹ค.
Car 3์ฐจ๋Ÿ‰์„3์— ์ฃผ์ฐจํ–ˆ์Šต๋‹ˆ๋‹ค.
Car 4์ฐจ๋Ÿ‰์„4์— ์ฃผ์ฐจํ–ˆ์Šต๋‹ˆ๋‹ค.
๊ณต๊ฐ„์ด ๋ถ€์กฑํ•˜์—ฌ ์ฃผ์ฐจํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
๊ณต๊ฐ„์ด ๋ถ€์กฑํ•˜์—ฌ ์ฃผ์ฐจํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
๊ณต๊ฐ„์ด ๋ถ€์กฑํ•˜์—ฌ ์ฃผ์ฐจํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
๊ณต๊ฐ„์ด ๋ถ€์กฑํ•˜์—ฌ ์ฃผ์ฐจํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
๊ณต๊ฐ„์ด ๋ถ€์กฑํ•˜์—ฌ ์ฃผ์ฐจํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
Car 0์ฐจ๋Ÿ‰์„ ์„ฑ๊ณต์ ์œผ๋กœ ์ฐจ๋ฅผ ๋บ์Šต๋‹ˆ๋‹ค.
Car 3์ฐจ๋Ÿ‰์„ ์„ฑ๊ณต์ ์œผ๋กœ ์ฐจ๋ฅผ ๋บ์Šต๋‹ˆ๋‹ค.
Car 5์ฐจ๋Ÿ‰์„0์— ์ฃผ์ฐจํ–ˆ์Šต๋‹ˆ๋‹ค.
Car 6์ฐจ๋Ÿ‰์„3์— ์ฃผ์ฐจํ–ˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋Ÿฌํ•œ ๋ฒˆํ˜ธ์˜ ์ฐจ๋Ÿ‰์€ ์—†์Šต๋‹ˆ๋‹ค.

@Junee01
Copy link
Author

Junee01 commented Aug 20, 2016

์šฐ์„  ๊ท€ํ•œ ์‹œ๊ฐ„๋‚ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์—์ดํ”„๋ฆด๋‹˜. ์ผ€๋นˆ๋‹˜. ๋ฉด์ ‘์€ ์ฐธ ์žฌ๋ฏธ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
์–ด์ œ ๊ธฐํ•œ๋‚ด๋กœ ๋๋‚ด๊ฒ ๋‹ค๊ณ  ํ–ˆ๊ณ , ๊ทธ ๋‚  ๋งˆ๋ฌด๋ฆฌ๋ฅผ ์ง“๊ณ  ์ง‘์— ๊ฐ”์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ผ€๋นˆ๋‹˜์ด ์ƒ๊ฐํ•˜์‹œ๋Š” ๋งŒํผ์˜ ์ฃผ์ฐจ์žฅ์ธ์ง€๋Š” ์ž˜ ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค.
ํŒจํ„ด, ๋ฆฌํŒฉํ† ๋ง ๊ทธ๋ฆฌ๊ณ  ๋ฃจ๋น„์˜ ํŠน์ • ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ ์ˆ˜๋ฅผ ์ค„์ด๊ณ  ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์šฐ์„ ์€ ์œ„์˜ ์ฝ”๋“œ๊ฐ€ ์ œ๊ฐ€ ์ƒ๊ฐํ•˜๋Š” ์ตœ์†Œํ•œ์˜ ์ฃผ์ฐจ์žฅ์ž…๋‹ˆ๋‹ค. ์ƒํ™ฉ์— ๋”ฐ๋ผ ์‚ฌ์šฉ์ž, ์š”๊ธˆ ๋“ฑ์ด ์ถ”๊ฐ€ ๋  ์ˆ˜ ์žˆ์ง€๋งŒ ์šฐ์„ ์€ ์ œ์™ธํ•˜๊ณ  ์ฃผ์ฐจ์™€ ์ฐจ ๋นผ๊ธฐ์— ์ดˆ์ ์„ ๋‘๊ณ  ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

'๋‚ด ๊ฒƒ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์‚ฌ๋žŒ' ์ด๋ผ๋Š” ๋ง์— ๊ณต๊ฐ์„ ๋งŽ์ด ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€๋Š” ๋นจ๋ฆฌ ๊ฒฐ๊ณผ๋ฌผ์„ ๋งŒ๋“ค์–ด์„œ ํฌํŠธํด๋ฆฌ์˜ค์— ์˜ฌ๋ฆฌ๊ณ  ๋‹ค๋ฅธ ๊ฑธ ๋งŒ๋“œ๋Š” ๋ฐ์— ์ดˆ์ ์„ ๋‘์—ˆ๋Š”๋ฐ, ๋ ˆ์ผ์ฆˆ ํŠœํ† ๋ฆฌ์–ผ๋„ ์•„์ง 100% ์ดํ•ดํ•˜์ง€ ๋ชปํ•˜๊ณ  ์ž‘์—…์„ ํ•ด์„œ๊ทธ๋Ÿฐ์ง€ ๊ธฐ์ดˆ์ ์ธ ์งˆ๋ฌธ์— ๋งŒ์กฑ์Šค๋Ÿฝ๊ฒŒ ๋Œ€๋‹ตํ•˜์ง€ ๋ชปํ–ˆ๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฃจ๋น„ ๊ธฐ์ดˆ ๋ฌธ๋ฒ•๋ถ€ํ„ฐ ๋‹ค์‹œ ์‹œ์ž‘ํ•˜๋ฉด์„œ ์ƒ๊ฐ์„ ๋ฐ”๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋„๋ก ์—ฐ์Šต ํ•ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment