logo CodeStepByStep logo

frequentFlier

Language/Type: Java file processing Scanner
Related Links:
Author: Jessica Miller (on 2021/06/13)

Write a method named frequentFlier that accepts as its parameter a Scanner for an input file. The data in the Scanner represents all of the flights a person has taken. Your method should compute and return an integer representing the total frequent flyer miles the person has earned. The input consists of pairs of tokens where each pair begins with the type of ticket that the person bought ("coach", "firstclass", or "discount", case-sensitively) and is followed by the number of miles of the flight. The rules for awarding frequent flier miles are:

  • 1 frequent flyer mile is earned for each mile traveled in coach.
  • 2 frequent flyer miles are earned for each mile traveled in first class.
  • 0 frequent flyer miles are earned on a discounted flight.

For example, if a Scanner named input1 refers to an input file containing the following text:

coach 1500   firstclass 2000   discount 900   coach 3500

In this example, the person earns 1500 frequent flyer miles for the first flight, 4000 frequent flyer miles for the second flight, 0 frequent flyer miles for the third flight, and 3500 frequent flyer miles for the fourth flight. Therefore the call of frequentFlier(input1) should return 9000 (a total of 1500 + 2*2000 + 3500).

The input might span multiple lines and might have different spacing between tokens. You are to process all tokens on all lines together. For example, if a Scanner named input2 refers to an input file containing the following text:

firstclass        5000  coach         1500         coach
100 firstclass

2000    discount 300

Then the call of frequentFlier(input2) should return 15600 (a total of 2*5000 + 1500 + 100 + 2*2000).

You may assume that the input file exists and follows the format above; that the file has an even number of tokens and contains at least 1 pair of tokens; that every other token is an integer; and that the other tokens are valid ticket types.

Method: Write a Java method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.