In: Computer Science
Project 1: Frequent Flyer Miles Calculator
Write a Ruby program that calculates how many frequent flyer
miles are needes for a free ticket on a new startup airline,
CorsairAir. Frequent flyer miles are charged for a free ticket
depending on the class of service (more for first class, less for
coach), depending on the day flying (more if flying on Friday,
Saturday or Monday, less for other days of the week), depending on
the distance traveled, and a surcharge if flying to Canada, Mexico
or the Carribean. Tickets start with a cost of 10,000 frequent
flyer miles. Then, you should calculate the distance charge which
is 1,000 frequent flyer miles for each 250 miles flown. Then, you
should charge an additional 40% charge of frequent flyer miles if
the passenger wants to fly first class. If flying on a Friday,
Saturday or Monday, the ticket will cost an additional 5,000
frequent flyer miles. Travel to Canada, Mexico or the Carribean
needs to cost an additional 7,500 frequent flyer miles. Be sure
your program does not allow for negative miles flown or incorrect
answers to the yes/no questions asked.
The sample program dialogs below should help you to see how to
perform this calculation.
CorsairAir Calculator
How far are your flying:-20
Sorry Charlie!
Continue(y/n)? y
How far are you flying (in
miles):500
Want first class (y/n):y
Flying on a Friday, Saturday or Monday
(y/n):n
Flying to Canada, Mexico or the Carribean
(y/n):n
10000 base cost
2000 distance charge
4800 First class charge
You will need 16800 frequent flyer miles for this ticket. Enjoy
your trip!
Continue(y/n)? y
How far are you flying (in
miles):500
Want first class (y/n):n
Flying on a Friday, Saturday or Monday
(y/n):y
Flying to Canada, Mexico or the Carribean
(y/n):n
10000 base cost
2000 distance charge
5000 day of the week charge
You will need 17000 frequent flyer miles for this ticket. Enjoy
your trip!
Continue(y/n)? y
How far are you flying (in
miles):500
Want first class (y/n):n
Flying on a Friday, Saturday or Monday
(y/n):y
Flying to Canada, Mexico or the Carribean
(y/n):foobar
Sorry Charlie!
Continue(y/n)? y
How far are you flying (in
miles):500
Want first class (y/n):n
Flying on a Friday, Saturday or Monday
(y/n):n
Flying to Canada, Mexico or the Carribean
(y/n):n
10000 base cost
2000 distance charge
You will need 12000 frequent flyer miles for this ticket. Enjoy
your trip!
Continue(y/n)? n
****This requires a lot of effort so please drop a like if you are satisfied with the solution..*****
Ruby Code
puts "CorsairAir Calculator"
loop do
basePrice=10000
distanceCharge=0
firstClass=0
dayOfTheWeekCharge=0
placeCharge=0
puts "How far are you flying(in miles):"
miles=gets.to_i
if miles>0
distanceCharge=(miles/250)*1000
puts "Want first class(y/n):"
choice=gets
if(choice <=> "y")==1
firstClass=(distanceCharge+basePrice)*0.4
elsif(choice <=> "n")==-1
puts "Sorry Charlie!"
puts "continue (y/n):"
choice=gets
if(choice <=> "y")==1
next
else
break
end
end
puts "Flying on a Friday, Saturday or Monday (y/n):"
choice=gets
if (choice <=>"y")==1
dayOfTheWeekCharge=5000
elsif(choice <=> "n")==-1
puts "Sorry Charlie!"
puts "continue (y/n):"
choice=gets
if(choice <=> "y")==1
next
else
break
end
end
puts "Flying to Canada, Mexico or the Carribean (y/n):"
choice=gets
if (choice <=>"y")==1
placeCharge=7500
elsif(choice <=> "n")==-1
puts "Sorry Charlie!"
puts "continue (y/n):"
choice=gets
if(choice <=> "y")==1
next
else
break
end
end
puts "#{basePrice} base cost"
if distanceCharge>0
puts "#{distanceCharge} distance charge"
end
if firstClass>0
puts "#{firstClass} first class charge"
end
if dayOfTheWeekCharge>0
puts "#{dayOfTheWeekCharge} day of the week charge"
end
if placeCharge>0
puts "#{placeCharge} Destination Charge"
end
puts "You will need #{basePrice+distanceCharge+firstClass+dayOfTheWeekCharge+placeCharge} frequent flyer miles for this ticket. Enjoy your trip!"
else
puts "Sorry Charlie!"
end
puts "continue (y/n):"
choice=gets
break if (choice <=>"y")==-1
end
output obtained:
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux] CorsairAir Calculator How far are you flying(in miles): -20 Sorry Charlie! continue (y/n): y How far are you flying(in miles): 500 Want first class(y/n): y Flying on a Friday, Saturday or Monday (y/n): n Flying to Canada, Mexico or the Carribean (y/n): n 10000 base cost 2000 distance charge 4800.0 first class charge You will need 16800.0 frequent flyer miles for this ticket. Enjoy your trip! continue (y/n): y How far are you flying(in miles): 500 Want first class(y/n): n Flying on a Friday, Saturday or Monday (y/n): y Flying to Canada, Mexico or the Carribean (y/n): n 10000 base cost 2000 distance charge 5000 day of the week charge You will need 17000 frequent flyer miles for this ticket. Enjoy your trip! continue (y/n): y How far are you flying(in miles): 500 Want first class(y/n): n Flying on a Friday, Saturday or Monday (y/n): y Flying to Canada, Mexico or the Carribean (y/n): foobar Sorry Charlie! continue (y/n): y How far are you flying(in miles): 500 Want first class(y/n): n Flying on a Friday, Saturday or Monday (y/n): n Flying to Canada, Mexico or the Carribean (y/n): n 10000 base cost 2000 distance charge You will need 12000 frequent flyer miles for this ticket. Enjoy your trip! continue (y/n): n => nil