Algorithm for processing upgrades into first class

Assignment Help JAVA Programming
Reference no: EM131588637

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;
}
}

Reference no: EM131588637

Questions Cloud

Discuss effectiveness of rehabilitation : Define the authors note that studies have shown both little and much hope for the resurgence correctional rehabilitation
Challenges of the current healthcare system : Demographic information and vital health statistics about the country.Challenges of the current healthcare system and issues being faced.
Distributed-cloud computing-virtualization : Briefly answer the following questions on virtualization levels. Highlight the key points and identify the distinctions in different approaches - Instruction.
Shares of stock where options on the stock are traded : There are many different options (calls and puts) that may be purchased on shares of stock where options on the stock are traded.
Algorithm for processing upgrades into first class : 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
Design and management of an optical network element : An article that describes a deployment or a design of an Optical Access Network, and/or explains the design and management of an optical network element.
Discuss chronically truant to and absent from school : While her mental health has always been questionable, she was never formally diagnosed with any DSM IV TR mental disorder
What to advise your staff members : Therefore, you must reach your own conclusions as to what to advise your staff members, and what actions, if any, are to be taken by the firm
The us patients bill of rights : Conduct some independent research into the U.S. Patient's Bill of Rights. Which right (or rights) do you feel is the most important and why?

Reviews

Write a Review

JAVA Programming Questions & Answers

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd