Write a method frequentFlier that accepts a Scanner for an input file of ticket
ID: 3915079 • Letter: W
Question
Write a method frequentFlier that accepts a Scanner for an input file of ticket type / mileage pairs and reports how many frequent-flier miles the person earned. • 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, given the input below, your method should return 15600 (2*5000 + 1500 + 100 + 2*2000). firstclass 5000 coach 1500 coach 100 firstclass 2000 discount 300
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
class Test
{
public static void main(String[] args) throws java.lang.Exception
{
Scanner input = new Scanner(new File("Input.txt"));
frequentFlier(input);
}
public static void frequentFlier(Scanner input){
Scanner lines = new Scanner(input.nextLine());
//Scanner lines = new Scanner(System.in);
int miles=0;int mileage;
while(lines.hasNext()) {
String line = lines.nextLine();
String[] arrSplit = line.split(" ");
mileage = Integer.parseInt(arrSplit[1]);
if(arrSplit[0].equalsIgnoreCase("coach"))
miles += mileage;
else if(arrSplit[0].equalsIgnoreCase("firstclass"))
miles += 2*mileage;
else
miles += 0;
}
System.out.println("Frequent flier miles: "+ miles);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.