Write a console-based Java application to help an airline customer find good way
ID: 3856280 • Letter: W
Question
Write a console-based Java application to help an airline customer find good ways of redeeming his/her accumulated mileage into airline tickets. Assume all trips depart from O'Hare (ORD). Different destinations need different mileage for redeeming tickets. If the person doesn't travel in a busy season, he or she may take advantage of the “Fly Cheap” program that can attain a ticket with fewer accumulated miles.
A list of destination cities and related ticket redemption information should be read from a file, which is formatted as one city per line, with five fields separated by a semicolon ( ; ) as shown below destination; normal mileage needed for an economy class ticket (this information is proportional to the actual travel distance); “Fly Cheap” mileage needed for an economy class ticket; additional mileage needed for upgrading to first class; and the months of departure when the “Fly Cheap” mileage can be used.
For example:
Berlin;17250;12750;5250;3-5
Hong Kong;30000;20250;17500;2-4
Hyderabad;30000;20000;15000;2-4
Sidney;50000;30000;20000;5-6
Paris;20000;15000;10000;2-4
New York;10000;5000;8000;10-11
Tokyo;29000;16500;5350;2-5
Vienna;17500;12500;5000;2-4
Washington, D.C.;9000;7500;2000;10-12
Your application program should prompt the user to enter the name of the file at the keyboard. Once entered, it should be read using a Scanner object named keyScan and assigned to a String variable. Then, open another Scanner object named fileScan to read the destination lines from it. To read from an input file, you will need:
import java.io.*;
Your algorithm should 1) try to get tickets that travel the farthest, 2) use “Fly Cheap” mileage whenever possible, 3) try to display as many different tickets as possible given the information (at most, one ticket is needed for a single destination), and 4) use the remaining mileage for upgrade, if possible (try to upgrade the longest trip first).
Your program should first print out a list of the cities for the user’s information. It then loops for input and prints out tickets that can be redeemed. Accurate sample program output can be found at the end of this handout on page 4. It is recommended that you set up your output so that it looks EXACTLY like the sample provided.
Sample Accurate Program Output
Please enter the name of the file: destinations.txt
-------------------------------------------------
WELCOME TO THE JAVA AIRLINES MILES REDEMPTION APP
-------------------------------------------------
List of destination cities you can travel to:
Berlin
Hong Kong
Hyderabad
New York
Paris
Sidney
Tokyo
Vienna
Washington, D.C.
---------------------------------------------------
Please enter your accumulated Frequent Flyer Miles: 49600
Please enter your month of departure (1-12): 4
Your Frequent Flyer Miles can be used to redeem the following tickets:
* A trip to Hong Kong in Economy Class
* A trip to Hyderabad in Economy Class
* A trip to Washington, D.C., in Economy Class
Your remaining Frequent Flyer Miles: 350
Do you want to continue (y/n)? n
-----------------------------------------------------------
THANK YOU FOR USING THE JAVA AIRLINES MILES REDEMPTION APP!
-----------------------------------------------------------
Explanation / Answer
Given below is the code for teh question with output. It uses ArrayList and Hashmap to store data. Please rate the answer if it helped. Thank you.
Ticket.java
public class Ticket {
private int type; //1- economy 2- business
private String destination;
private int mileage;
public Ticket(int type, String destination, int mileage)
{
this.type = type;
this.destination = destination;
this.mileage = mileage;
}
public void set(int type, int mileage)
{
this.type = type;
this.mileage = mileage;
}
public int getType()
{
return type;
}
public int getMileage()
{
return mileage;
}
public String getDestination()
{
return destination;
}
public String toString()
{
String str = "* A trip to " + destination + " in ";
if(type == 1)
str += "Economy class";
else
str += "First class";
return str;
}
}
RedemptionInfo.java
public class RedemptionInfo {
private String destination;
private int normalMileage;
private int flycheapMileage;
private int upgradeMileage;
private int startMonth;
private int endMonth;
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
public int getNormalMileage() {
return normalMileage;
}
public void setNormalMileage(int normalMileage) {
this.normalMileage = normalMileage;
}
public int getFlycheapMileage() {
return flycheapMileage;
}
public void setFlycheapMileage(int flycheapMileage) {
this.flycheapMileage = flycheapMileage;
}
public int getUpgradeMileage() {
return upgradeMileage;
}
public void setUpgradeMileage(int upgradeMileage) {
this.upgradeMileage = upgradeMileage;
}
public int getStartMonth() {
return startMonth;
}
public void setStartMonth(int startMonth) {
this.startMonth = startMonth;
}
public int getEndMonth() {
return endMonth;
}
public void setEndMonth(int endMonth) {
this.endMonth = endMonth;
}
public boolean isMonthIncluded(int month)
{
if(month >= startMonth && month <= endMonth)
return true;
else
return false;
}
}
RedeemerApp.java
import java.io.File;
import java.io.FileNotFoundException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class RedeemerApp {
//sort the list on fly cheap mileage
private static void sort(ArrayList<RedemptionInfo> list)
{
int minIdx ;
for(int i = 0; i < list.size(); i++)
{
minIdx = i;
for( int j = 0; j < list.size(); j++)
{
if(list.get(j).getFlycheapMileage() < list.get(minIdx).getFlycheapMileage())
minIdx = j;
}
if(minIdx != i)
{
RedemptionInfo temp = list.get(i);
list.set(i, list.get(minIdx));
list.set(minIdx, temp);
}
}
}
private static void menu(Scanner keyScan,ArrayList<RedemptionInfo> list, HashMap<String, RedemptionInfo> map, ArrayList<String> dest)
{
System.out.println("-------------------------------------------------");
System.out.println("WELCOME TO THE JAVA AIRLINES MILES REDEMPTION APP");
System.out.println("-------------------------------------------------");
System.out.println("List of destination cities you can travel to:");
for(String s : dest)
System.out.println(s);
System.out.println("-------------------------------------------------");
String ans;
int accumulated, month;
while(true)
{
System.out.print("Please enter your accumulated Frequent Flyer Miles: ");
accumulated = keyScan.nextInt();
System.out.print("Enter your month of departure (1-12): ");
month = keyScan.nextInt();
ArrayList<Ticket> tickets = new ArrayList<Ticket>(); //a list of tickets generated
//iterate through each redemption info and see if a ticket can be generated for it
for(int i = 0; i < list.size(); i++)
{
RedemptionInfo info = list.get(i);
Ticket t = null;
//is the month a flycheap month for this destination
if(info.isMonthIncluded(month))
{
//do we have enough flyer miles for fly cheap miles
if(accumulated >= info.getFlycheapMileage())
{
t = new Ticket(1, info.getDestination(),info.getFlycheapMileage());
accumulated -= info.getFlycheapMileage();
tickets.add(t);
}
}
else //not in flycheap month
{
if(accumulated >= info.getNormalMileage())
{
t = new Ticket(1, info.getDestination(), info.getNormalMileage());
accumulated -= info.getNormalMileage();
tickets.add(t);
}
}
}
if(accumulated > 0)
{
//now try to upgrade the tickets if possible
for(Ticket t : tickets)
{
RedemptionInfo info = map.get(t.getDestination());
if(accumulated >= info.getUpgradeMileage())
{
t.set(2, t.getMileage() + info.getUpgradeMileage());
accumulated -= info.getUpgradeMileage();
}
}
}
//now print out the tickets
System.out.println("Your Frequent Flyer Miles can be used to redeem the following tickets:");
for(Ticket t : tickets)
System.out.println(t);
System.out.println("Your remaining Frequent Flyer Miles: " + accumulated);
System.out.print("Do you want to continue (y/n) ? ");
ans = keyScan.next();
if(!ans.equals("y"))
break;
}
}
public static void main(String[] args) {
ArrayList<RedemptionInfo> redeemInfo = new ArrayList<RedemptionInfo>();
HashMap<String, RedemptionInfo> map = new HashMap<String, RedemptionInfo>();
ArrayList<String> destinations = new ArrayList<String>();
Scanner keyScan = new Scanner(System.in);
String filename;
System.out.print("Enter input file name containing redemption info: ");
filename = keyScan.nextLine().trim();
try {
Scanner fileScan = new Scanner(new File(filename));
while(fileScan.hasNextLine())
{
String line = fileScan.nextLine();
Scanner lineScan = new Scanner(line);
lineScan.useDelimiter(";");
RedemptionInfo info = new RedemptionInfo();
info.setDestination(lineScan.next());
info.setNormalMileage(lineScan.nextInt());
info.setFlycheapMileage(lineScan.nextInt());
info.setUpgradeMileage(lineScan.nextInt());
String months = lineScan.next();
int idx = months.indexOf('-');
String sm = months.substring(0, idx);
String em = months.substring(idx+1);
info.setStartMonth(Integer.parseInt(sm));
info.setEndMonth(Integer.parseInt(em));
redeemInfo.add(info);
destinations.add(info.getDestination());
map.put(info.getDestination(), info);
}
fileScan.close();
sort(redeemInfo); // sort the list so we can take the ones which are farthest
menu(keyScan, redeemInfo, map, destinations);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
input file: redeem.txt
Berlin;17250;12750;5250;3-5
Hong Kong;30000;20250;17500;2-4
Hyderabad;30000;20000;15000;2-4
Sidney;50000;30000;20000;5-6
Paris;20000;15000;10000;2-4
New York;10000;5000;8000;10-11
Tokyo;29000;16500;5350;2-5
Vienna;17500;12500;5000;2-4
Washington, D.C.;9000;7500;2000;10-12
output
Enter input file name containing redemption info: redeem.txt
-------------------------------------------------
WELCOME TO THE JAVA AIRLINES MILES REDEMPTION APP
-------------------------------------------------
List of destination cities you can travel to:
Berlin
Hong Kong
Hyderabad
Sidney
Paris
New York
Tokyo
Vienna
Washington, D.C.
-------------------------------------------------
Please enter your accumulated Frequent Flyer Miles: 49600
Enter your month of departure (1-12): 4
Your Frequent Flyer Miles can be used to redeem the following tickets:
* A trip to Hong Kong in Economy class
* A trip to Hyderabad in Economy class
* A trip to Washington, D.C. in Economy class
Your remaining Frequent Flyer Miles: 350
Do you want to continue (y/n) ? y
Please enter your accumulated Frequent Flyer Miles: 67000
Enter your month of departure (1-12): 4
Your Frequent Flyer Miles can be used to redeem the following tickets:
* A trip to Hong Kong in Economy class
* A trip to Hyderabad in Economy class
* A trip to Paris in Economy class
* A trip to Washington, D.C. in First class
Your remaining Frequent Flyer Miles: 750
Do you want to continue (y/n) ? n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.