Advertisement
Guest User

Untitled

a guest
Aug 9th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.07 KB | None | 0 0
  1.  
  2.  
  3. class Car
  4.     attr_accessor :title, :mileage
  5.     def initialize(title, price)
  6.         @title = title
  7.         @price = price
  8.     end
  9.     def get_price
  10.         return @price
  11.     end
  12. end
  13.  
  14. class UsedCar < Car
  15.     def initialize(title, price, mileage)
  16.         @title = title
  17.         @mileage = mileage
  18.         @price = price
  19.     end
  20.     def get_price
  21.         if @mileage > 50000
  22.         price_used_car = (1 - 50000 / 100000) * @price
  23.         else
  24.             price_used_car = (1 - @mileage / 100000) * @price
  25.         end
  26.     end
  27.  
  28.  
  29. end
  30.  
  31. class CarPriceCalculator
  32.     def initialize(cars)
  33.         @cars = cars
  34.     end
  35.    
  36.     def check(title, price)
  37.        
  38.         audi.title is costs audi.get_price
  39.         # "#{bmw.title} is costs #{bmw.get_price}"
  40.         # "#{honda.title} is costs #{honda.get_price}, because its mileage is #{honda.mileage}"
  41.     end
  42. end
  43.  
  44.  
  45. @cars = []
  46.  
  47. audi = Car.new("Audi", 6000)
  48. # # puts "#{audi.title} is costs #{audi.get_price}"
  49.  
  50. bmw = Car.new("BMW", 5000)
  51. # # puts "#{bmw.title} is costs #{bmw.get_price}, because its mileage is #{bmw.mileage}"
  52.  
  53. honda = UsedCar.new("Honda", 4200, 10000)
  54.  
  55.  
  56. @cars << audi
  57. @cars << bmw
  58. @cars << honda
  59.  
  60.  
  61. check(title, price)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement