farematrix.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env ruby
  2. require 'csv'
  3. # current as of July 2013
  4. fare_types = {
  5. "pub_aircon" => {
  6. "fare" => 12.00,
  7. "firstkm" => 5,
  8. "perkm" => 2.2,
  9. "limit" => 60
  10. },
  11. "pub_ordinary" => {
  12. "fare" => 10.00,
  13. "firstkm" => 5,
  14. "perkm" => 1.85,
  15. "limit" => 60
  16. },
  17. "puj" => {
  18. "fare" => 8.00,
  19. "firstkm" => 4,
  20. "perkm" => 1.4,
  21. "limit" => 50,
  22. "override" => {
  23. 10 => { "discounted" => '13.25' },
  24. 12 => { "discounted" => '15.50' },
  25. 14 => { "discounted" => '17.75' },
  26. 18 => { "discounted" => '22.25' },
  27. 20 => { "discounted" => '24.50' },
  28. 22 => { "discounted" => '26.75' },
  29. 33 => { "discounted" => '38.75' },
  30. 35 => { "discounted" => '41.25' },
  31. 37 => { "discounted" => '43.50' },
  32. 39 => { "discounted" => '45.75' },
  33. 45 => { "discounted" => '52.50' },
  34. 47 => { "discounted" => '54.75' }
  35. }
  36. }
  37. }
  38. def round(original)
  39. "%.2f" % ((original*4).round/4.0)
  40. end
  41. def if_override(original, type, dist, name)
  42. value = original
  43. value = type['override'][dist][name] if type['override'] && type['override'][dist] && type['override'][dist][name]
  44. value
  45. end
  46. fare_types.each do |key, type|
  47. CSV.open(key+'.csv', 'wb') do |csv|
  48. csv << ['distance', 'regular', 'discounted']
  49. type['limit'].times do |x|
  50. dist = x+1
  51. fare = type['fare']
  52. if dist > type['firstkm']
  53. fare += type['perkm'] * (dist - type['firstkm'])
  54. end
  55. discounted = round(fare*0.8)
  56. fare = round(fare)
  57. discounted = if_override(discounted, type, dist, 'discounted')
  58. fare = if_override(fare, type, dist, 'fare')
  59. csv << [dist, fare, discounted]
  60. end
  61. end
  62. end