In this exercise, you will write a program to help you prepare to buy a vehicle.
You have two tasks: determine if you will need to get a license, and estimate the acceptable price for a used vehicle.
Some kinds of vehicles require a drivers license to operate them.
Assume only vehicles containing car or truck require a license; everything else can be operated without a license.
Implement a pattern {action} pair that matches when the first field is needs_license.
It should get the kind of vehicle from the second field, and print "true" if you need a license for that kind of vehicle.
If you don't need a license, you don't need to print anything.
echo "needs_license,dumptruck" | gawk -f vehicle-purchase.awk
# => true
echo "needs_license,bike" | gawk -f vehicle-purchase.awk
# => (empty output)Now that you have made your decision, you want to make sure you get a fair price at the dealership. Since you are interested in buying a used vehicle, the price depends on how old the vehicle is. For a rough estimate, assume if the vehicle is less than 3 years old, it costs 80% of the original price it had when new. If it is more than 10 years old, it costs 50%. If the vehicle is at least 3 years old but not older than 10 years, it costs 70% of the original price.
Implement a pattern {action} pair that matches when the first field is resell_price.
It should get the original price of the vehicle from the second field, and the age from the third field.
The action should print the estimated price.
echo "resell_price,1000,1" | gawk -f vehicle-purchase.awk
# => 800
echo "resell_price,1000,5" | gawk -f vehicle-purchase.awk
# => 700
echo "resell_price,1000,15" | gawk -f vehicle-purchase.awk
# => 500In this exercise, you will write a program to help you prepare to buy a vehicle.
You have two tasks: determine if you will need to get a license, and estimate the acceptable price for a used vehicle.
Some kinds of vehicles require a drivers license to operate them.
Assume only vehicles containing car or truck require a license; everything else can be operated without a license.
Implement a pattern {action} pair that matches when the first field is needs_license.
It should get the kind of vehicle from the second field, and print "true" if you need a license for that kind of vehicle.
If you don't need a license, you don't need to print anything.
echo "needs_license,dumptruck" | gawk -f vehicle-purchase.awk
# => true
echo "needs_license,bike" | gawk -f vehicle-purchase.awk
# => (empty output)Now that you have made your decision, you want to make sure you get a fair price at the dealership. Since you are interested in buying a used vehicle, the price depends on how old the vehicle is. For a rough estimate, assume if the vehicle is less than 3 years old, it costs 80% of the original price it had when new. If it is more than 10 years old, it costs 50%. If the vehicle is at least 3 years old but not older than 10 years, it costs 70% of the original price.
Implement a pattern {action} pair that matches when the first field is resell_price.
It should get the original price of the vehicle from the second field, and the age from the third field.
The action should print the estimated price.
echo "resell_price,1000,1" | gawk -f vehicle-purchase.awk
# => 800
echo "resell_price,1000,5" | gawk -f vehicle-purchase.awk
# => 700
echo "resell_price,1000,15" | gawk -f vehicle-purchase.awk
# => 500