Question
Java For this assignment you will re-implement the Air Ticket Redeemer, refer to the output screen The Destination class should remain the same as that
Java
For this assignment you will re-implement the Air Ticket Redeemer, refer to the output screen
The Destination class should remain the same as that developed in Assignment 4. This reusability is one of the advantages of encapsulation, i.e., using object-oriented methodology.
In addition, the MileRedeemer class from Assignment 4 can be reused as well. Many methods of MileRedeemer can be invoked directly by your application, such as readDestinations() and the method that contains the redemption algorithm. There should be no part of your application that displays console-type output as you did in Assignment 4.
where the arguments are in the range 0...255 and the higher the number, the lighter the color. Use colors that are not too intense and displeasing to a user, and be sure that the text is completely legible.
The left panel contains a JList so that the user can go back and forth to look at the information of different tickets to different cities. The array of strings returned by the MileRedeemer method getCityNames() can be used to populate the JList. When a city in the JList is selected, its details (i.e., the members of its corresponding Destination object) should be displayed in the corresponding JTextFields. These JTextFields are not editable. To listen for this event, implement the interface javax.swing.event.ListSelectionListener, and provide the method
public void valueChanged(ListSelectionEvent e)
Add a new method to your MileRedeemer class to return the corresponding Destination object for a given city name, e.g.:
public Destination findDestination(String cityName)
The right panel takes in the accumulated miles using a JTextField and a JSpinner. After the "Redeem Tickets" button is clicked, it outputs ticket details in a JTextArea, and the remaining miles in a JTextField. The components for output are not editable.
The Input File
Create a small GUI window to ask for the name of the input destinations file.
How to populate the month Strings for the JSpinner
The spinner's month Strings can be obtained using the following code. (Some logic is included to remove an extra, empty value.)
protected String[] getMonthStrings() { String[] months = new java.text.DateFormatSymbols().getMonths();
int lastIndex = months.length - 1;
if (months[lastIndex] == null || months[lastIndex].length()
String[] monthStrings = new String[lastIndex]; System.arraycopy(months, 0, monthStrings, 0, lastIndex); return monthStrings; }
else
{ return months; } }
Other Hints
Do not use a form or GUI builder in your IDE to build your GUI; you must code it all by hand.
You are allowed to use GridBagLayout, about which more can be found here.
Any error messages or messages printed as a result of caught exceptions should be printed on the Java console.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.util.Scanner; // This is for accepting user input.
import java.io.*; // This is for file input/output.
// Declaration of class MileRedemptionApp
public class MileRedemptionApp {
// main - if file not found, throw an exception public static void main(String[] args) throws FileNotFoundException {
// Create a scanner object for file. Scanner keyScan = new Scanner( System.in ) ; // File paths
// File text = new File("C:/NIU_CSCI_470/archive/destination.txt");
// File text = new File("C:/learnJava/JavaAirlines/destination.txt");
// Prompt user to enter file name. System.out.print("Please enter the name of the file: ");
// Accept the file. String fileName = keyScan.next( );
// Create a new File object. File text = new File(fileName);
// Create another Scanner object fileScan. Scanner fileScan = new Scanner(text);
// Print a border and label that welcomes user to the application.
System.out.println("-------------------------------------------------");
System.out.println("WELCOME TO THE JAVA AIRLINES MILES REDEMPTION APP");
System.out.println("-------------------------------------------------");
System.out.println(" "); // Show user what cities he/she can travel to.
System.out.println("List of destination cities you can travel to: ");
// Create a MileRedeemer object MR. MileRedeemer MR = new MileRedeemer();
// Call method readDestinations, passing it fileScan. MR.readDestinations(fileScan);
// Call method getCityNames and put cities into a String array. String cities[] = MR.getCityNames();
System.out.println(" "); // Print however many cities there are in the array cities.
for (int i = 0; i
Scanner scMiles = new Scanner( System.in );
// Create a new Scanner object scAnswer. Scanner scAnswer = new Scanner( System.in ) ;
// Create a new Scanner object scMonth. Scanner scMonth = new Scanner( System.in );
do {
System.out.println(" ");
// Prompt user for accumulated Frequent Flyer Miles. System.out.print("Please enter your accumulated Frequent Flyer Miles: ");
// Accept miles. int miles = scMiles.nextInt( );
System.out.println(" "); // Prompt user to enter month of departure.
System.out.print("Please enter your month of departure (1-12): ");
// Accept month of departure. int month = scMonth.nextInt( );
System.out.println(" "); // Tell user what he/she can redeem with miles.
System.out.println("Your Frequent Flyer Miles can be used to redeem the following tickets:");
System.out.println(" "); // Call method redeeMiles, passing it miles and month. // Store results in array results. String results[] = MR.redeemMiles(miles, month);
// Print the results of array results. for (int i = 0; i
// Call method redeemMiles, passing it miles and month.
MR.redeemMiles(miles, month);
System.out.println(" "); // Show remaining Frequent Flyer Miles.
System.out.println("Your remaining Frequent Flyer Miles: " + MR.getRemainingMiles());
// Ask user if he/she would like to continue with application. System.out.print("Do you want to continue (y)? ");
// Accept an answer of 'y' or 'n' for yes or no, respectively. answer=scAnswer.next().charAt(0);
// The application will continue until the user decides to stop.
} while (answer != 'n');
// Close fileScan. fileScan.close();
// Close keyScan. keyScan.close();
// Close scAnswer. scAnswer.close();
// Close scMiles. scMiles.close(); // Close scMonth. scMonth.close(); // Print a border and a label that thanks traveler for using the application.
System.out.println("------------------------------------------------------------");
System.out.println("THANK YOU FOR USING THE JAVA AIRLINES MILES REDEMPTION APP!");
System.out.println("------------------------------------------------------------");
} }
import java.util.*;
// Declaration of class MileRedeemer.
public class MileRedeemer { // Create an ArrayList of type Destination called destinationList. ArrayList
/*************************************************************** readDestinations Use: Read and parses the destination data into an array of Destination objects, which should then be sorted by their normal mileage. Parameters: 1. fileScanner - a Scanner object that reads and parses the destination data into an array of Destination objects. Returns: Nothing. Notes: A Scanner object is used as an input parameter here for flexibility and reusability. ***************************************************************/ public void readDestinations(Scanner fileScanner) {
// While there is data in the file, put each city and field on a line, // separating them by the semicolon (";") delimiter. while(fileScanner.hasNextLine()) {
String line = fileScanner.nextLine(); // Put the next line into line.
String[] splitedStr = line.split(";"); // Split fields by semicolon // delimiter.
// Create a destination object dest and put each field in an array element. Destination dest = new Destination (splitedStr[0], splitedStr[1], splitedStr[2], splitedStr[3], splitedStr[4]); //Create a new object.
// Call add passing it dest - Adding it to the list. destinationList.add(dest);
} }
/*************************************************************** getCityNames Use: Loops through the array of Destination objects and creates an array of String objects from the city names Parameters: Takes no parameters. Returns: cities - An array of String objects representing the destination cities. Notes: The cities array can be sorted in ascending order. ***************************************************************/
public String[] getCityNames() {
// Create an array of type Destination called destinationArray. // Convert the ArrayList destinationList to an array. Destination[] destinationArray = (Destination[]) destinationList.toArray(new
// Create a Destination array the size of destinationList. Destination[destinationList.size()]);
// Create a String array, cities of size destinationList. String[] cities = new String[destinationList.size()]; // Loop the contents of the array destinationArray into array cities. for (int i = 0; i
}
// Call Array class method sort, passing it cities. Arrays.sort(cities);
// Return the String array containing destination cities. return cities;
}
/*************************************************************** redeemMiles Use: Returns an array of String objects containing descriptions of redeemed tickets to be printed out by the main program. Also saves the miles remaining after the tickets have been redeemed. Parameters: 1. miles - an int representing the total available miles. 2. month - an int representing the desired month of departure. Returns: results - an array of String objects containing descriptions of redeemed tickets to be printed out by the main program. Notes: This method calls some of the get methods of class Destination. ***************************************************************/
public String[] redeemMiles(int miles, int month) {
// Call the sort method for destinationList, passing it MileageComparator. destinationList.sort(new MileageComparator());
// Set i equal to 0. int i = 0; // Set remainingMiles equal to miles.
remainingMiles = miles; // Create a String array results of size of destinationList.
String[] results = new String[destinationList.size()]; // Advanced for loop - for each destination of list destinationList.
for (Destination eachDestination : destinationList) { // Check the month range for each destination. if (eachDestination.getbeginMonth() = month) { // If cheapMiles are less than remainingMiles,
if (eachDestination.getCheapMiles()
// Put the selected destination in array results (this is for an Economy Class flight). results[i] = "* A trip to " + eachDestination.getCity() + " in Economy Class";
// If remainingMiles are greater than additionalMiles, traveler can upgrade. if (remainingMiles > eachDestination.getAdditionalMile()) { // Subtract additionalMiles and apply them - select destination, upgrading // traveler to First Class. remainingMiles-= eachDestination.getAdditionalMile(); // Put the selected destination in array results (this is for a First Class flight).
results[i] = "* A trip to " + eachDestination.getCity() + " in First Class";
}
}
} // If NormalMiles are less than remainingMiles, else if (eachDestination.getNormalMiles()
// Subtract NormalMiles of the destination from remainingMiles. remainingMiles-= eachDestination.getNormalMiles();
// If remainingMiles are greater than AdditionalMiles, if (remainingMiles > eachDestination.getAdditionalMile()) { // subtract AdditionalMiles from remainingMiles. remainingMiles-= eachDestination.getAdditionalMile();
// Put the selected destination in array results (this is for a First Class flight). results[i] = "* A trip to " + eachDestination.getCity() + " in First Class"; }
} // Increment i. i++; } // Return results. return results;
}
/*************************************************************** getRemainingMiles Use: Returns the saved remaining miles. Parameters: Takes no parameters. Returns: remainingMiles - An int representing the saved remaining miles. Notes: This is a class MileRedeemer getter method. ***************************************************************/
public int getRemainingMiles() { // Return the saved remaining miles. return remainingMiles; }
/*************************************************************** class MileageComparator Use: Object of a class that defines the ordering. Parameters: Takes no parameters, however implements a Comparator of type Destination. Compare has two int parameters, d1 and d2 (Destination objects) - NormalMiles will be determined by subtracting them. Returns: the difference of the normalMiles (normal miles) for Destination objects d1 and d2, (d2.getNormalMiles() - d1.getNormalMiles()). Notes: class MileageComparator has been placed in class MileRedeemer. ***************************************************************/
// class MileageComparator implements a Comparator of type // Destination.
public class MileageComparator implements Comparator
{ // Compare Destination objects d1 and d2. public int compare(Destination d1, Destination d2) {
// Return the difference of the distances between Destination objects // d1 and d2. return (d2.getNormalMiles() - d1.getNormalMiles());
} }
}
public class Destination {
private String city; // From here and below are private instance // variables for class Destination. private int normalMile;
private int cheapMile;
private int additionalMile;
private String months;
private int beginMonth;
private int endMonth;
/*************************************************************** Constructor Use: Initializes a the instance variables of a Destination object. Parameters: 1. City - a String that represents where a traveler can go to. 2. normalMile - a String that represents the normal miles for an economy class ticket. 3. cheapMile - a String that represents the "Fly Cheap" miles needed for an economy class ticket. 4. additionalMile - a String that represents the additional miles needed to upgrade to first/business class. 5. months - a String representing the months of departure when the "Fly Cheap" mileage can be used. Returns: Does not apply - constructors have NO return type. Notes: Constructor initializes Destination instance variables and splits a range of months of departure, storing them into two array elements. ***************************************************************/
public Destination(String City, String normalMile, String cheapMile, String additionalMile, String months) {
this.city = City; // Initialize City
this.normalMile = Integer.parseInt(normalMile); // Initialize noramMile
this.cheapMile = Integer.parseInt(cheapMile); // Initialize cheapMile
this.additionalMile = Integer.parseInt(additionalMile); // Initialize additionalMile
this.months = months;
// Create a String array in which to store the month range. String[] monthRange = new String[2];
// Split month range into two months by the "-" delimiter. monthRange = months.split("-");
// Store the first month of a departure month range in beginMonth, having parsed it. beginMonth = Integer.parseInt(monthRange[0]);
// Store the ending month of a departure month range in endMonth, having parsed it. endMonth = Integer.parseInt(monthRange[1]);
}
/*************************************************************** getCity Use: Accessor method that returns the destination city. Parameters: Takes no parameters. Returns: A String city - the destination city. Notes: This is a class Destination getter method. ***************************************************************/
public String getCity() {
// Return the destination city. return city; }
/*************************************************************** getNormalMiles Use: Accessor method that returns the normal miles for an economy class ticket. Parameters: Takes no parameters. Returns: An int normalMile - the normal miles for an economy class ticket. Notes: This is a class Destination getter method. ***************************************************************/
public int getNormalMiles() { // Return the normal miles for an economy class ticket. return normalMile; }
/*************************************************************** getCheapMiles Use: Accessor method that returns the cheap miles for an economy class ticket. Parameters: Takes no parameters. Returns: An int cheapMile - the cheap miles for an economy class ticket. Notes: This is a class Destination getter method. ***************************************************************/
public int getCheapMiles() { // Return the normal miles for an economy class ticket. return cheapMile; }
/*************************************************************** getAdditionalMile Use: Accessor method that returns the additional miles needed to upgrade to business/first class. Parameters: Takes no parameters. Returns: An int additionalMile - the additional miles needed to upgrade to business/first class. Notes: This is a class Destination getter method. ***************************************************************/
public int getAdditionalMile() { // Return the additional miles needed to upgrade to first class. return additionalMile; }
/*************************************************************** getbeginMonth Use: Accessor method that returns the first month of a departure month range. Parameters: Takes no parameters. Returns: An int beginMonth - the first month of a departure month range. Notes: This is a class Destination getter method. **************************************************************/
public int getbeginMonth() { // Return the first month of a departure month range. return beginMonth; }
/*************************************************************** getendMonth Use: Accessor method that returns the ending month of a departure month range. Parameters: Takes no parameters. Returns: An int endMonth - the ending month of a departure month range. Notes: This is a class Destination getter method. **************************************************************/
public int getendMonth() { // Return the ending month of a departure month range. return endMonth; }
}
Mile Redemption App Destinations Redeem Miles Berlin Hong Kong Hyderabad Sidney Paris New York Tokyo Vienna Washington, D.C Enter your miles elect the month of departure Jan Redeem miles Normal miles50000 Supersaver miles 30000 upgrade cost 20000 Supersaver DatesMay June Your remaining miles
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started