Design a system that can process upgrade requests and


Detail :

JAVA
Suppose you work for a major airline and are given the job of writing the algorithm for processing upgrades into first class on various flights. Any frequent flyer can request an upgrade for his or her up-coming flight using this online system. Frequent flyers have different priorities, which are determined first by frequent flyer status (which can be, in order, silver, gold, platinum, and super) and then, if there are ties, by length of time in the waiting list. In addition, at any time prior to the flight, a frequent flyer can cancel his or her upgrade request (for instance, if he or she wants to take a different flight), using a confirmation code they got when he or she made his or her upgrade request. When it is time to determine upgrades for a flight that is about to depart, the gate agents inform the system of the number, k, of seats available in first class, and it needs to match those seats with the k highest-priority passengers on the waiting list.

Design a system that can process upgrade requests and cancellations in O(log n) time and can determine the k highest-priority flyers on the waiting list in O(k log n) time, where n is the number of frequent flyers on the waiting list.

1. Write a program that interactively allows the user to choose one of the following options
tiny_mce_markergt; PassengerList
1. Request upgrade
2. Cancel upgrade
3. Print upgrade list
Make a choice (1-3):
Choosing 1 or 2 should further ask for a passenger name and frequent flyer status.Take appropriate action (ie add or remove them from the list).
Choosing 3 should prompt user to enter a number for k (the number of available seats in first class) and prints the list of upgrade passengers
Here is the code :

import java.io.*;
import java.util.*;
class FlightResCanc {
final int MAX_SEATS = 20;
Scanner input = new Scanner(System.in);
public FlightResCanc() {
}
public void main(String[] args) {
Console gc = new Console();
boolean completed = false;
String[] seats = new String[MAX_SEATS]; // the passenger list
SeatIntialize(seats);
do {
Menu();
int choice = GetMenu(); // choice off of the main menu
switch (choice) {
case 1:
Req_Upgrade(seats);
break;
case 2:
Cancel(seats);
break;
case 3:
Print(seats);
break;
case 4:
completed = true;
break;
}
} while (!completed);
}
//CONSOLE
public class Console {
private BufferedReader in;
private String integerReprompt = "Invalid integer. Try again: ";
private String DPrompt = "Invalid double. Try again: ";
private String CPrompt = "Invalid character. Try again: ";
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
}
public String ReadS() {
String s = null;
s = in.readLine();
return s;
}
//Console gc = new Console();
public char ReadC() {
char c = 0;
String s = in.readLine();
if (s.length() == 1) {
c = s.charAt(0);
boolean valid = true;
} else {
System.out.print(CPrompt);
}
return c;
}
int readInt() {
int i = 0;
boolean valid = false;
i = Integer.parseInt(in.readLine());
valid = true;
System.out.print(integerReprompt);
return i;
}
public double readDouble() {
double d = 0.0;
boolean valid = true;
d = Double.parseDouble(in.readLine());
valid = true;
valid = false;
System.out.print(DPrompt);
return d;
}
public void pause() {
System.out.print("Press enter to continue...");
in.readLine();
}
public void setIntegerReprompt(String prompt) {
integerReprompt = prompt;
}
public void Charprompt (String prompt) {
CPrompt = prompt;
}
public void Doubleprompt(String prompt) {
DPrompt = prompt;
}
}
//END CONSOLE
void Menu() {
System.out.println("n Menun");
System.out.println("1. Request Upgrade");
System.out.println("2. Cancel Upgrade");
System.out.println("3. Print seating chart");
System.out.println("4. Quit");
System.out.println();
}
/**
* Get the user's choice off the main menu
*/
int GetMenu() {
int choice; // choice entered
boolean valid = false; // is choice valid?
do {
System.out.print("===> ");
choice = gc.readInt();
if (1 <= choice && choice <= 4) {
valid = true;
} else {
System.out.println("Invalid choice.");
}
} while (!valid);
return choice;
}
/**
* Initialize each element of seats to the empty string
*/
void SeatIntialize(String[] seats) {
for (int i = 0; i < seats.length; i++) {
seats[i] = "";
}
}
/**
* Make a Upgrade
*/
void Req_Upgrade(String[] seats) {
int seatIndex = findEmptySeat(seats); // index of first empty seat
if (seatIndex == seats.length) {
System.out.println("All seats are full. Sorry.");
} else {
String name = getPassengerName(); // passenger's name
seats[seatIndex] = name;
System.out.println(name + " has been assigned seat #" + (seatIndex+1));
}
}
/**
* Cancel a upgrade
*/
void Cancel(String[] seats) {
int seatIndex = CancelSeat(); // index of seat to cancel reservation for
if (isEmpty(seats, seatIndex)) {
System.out.println("Seat #" + (seatIndex+1) + " has not been Req_Upgraded for anyone");
} else {
seats[seatIndex] = "";
System.out.println("Seat #" + (seatIndex+1) + " is now available");
}
}
/**
* Print the seating chart
*/
void Print(String[] seats) {
System.out.println("nSeating Chartn");
for (int i = 0; i < seats.length; i++) {
System.out.println((i+1) + ". " + seats[i]);
}
}
/**
* Find the index of the first empty seat on the plane.
* If there are no empty seats, return seats.length
*/
int findEmptySeat(String[] seats) {
for (int i = 0; i < seats.length; i++) {
if (isEmpty(seats, i)) {
return i;
}
}
return seats.length;
}
boolean isEmpty(String[] seats, int seatIndex) {
return seats[seatIndex].equals("");
}
String getPassengerName() {
System.out.print("Enter the passenger's name: ");
return Console.ReadS();
}
int CancelSeat() {
boolean valid = false; // is the seat number valid?
int seat; // seat number to cancel
do {
System.out.print("Enter the seat to cancel: ");
seat = Console.readInt();
if (1 <= seat && seat <= MAX_SEATS) {
valid = true;
} else {
System.out.println("Invalid seat number");
}
} while (!valid);
return seat-1;
}
}

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Design a system that can process upgrade requests and
Reference No:- TGS02384180

Now Priced at $10 (50% Discount)

Recommended (98%)

Rated (4.3/5)