Ask Java Expert


Home >> Java

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

Java, Programming

  • Category:- Java
  • Reference No.:- M92397351
  • Price:- $10

Priced at Now at $10, Verified Solution

Have any Question?


Related Questions in Java

Chatbotscreate a small networked chat application that is

Chatbots Create a small, networked chat application that is populated by bots. Introduction On an old server park, filled with applications from the early days of the internet, a few servers still run one of the earliest ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Project descriptionwrite a java program to traverse a

Project Description: Write a java program to traverse a directory structure (DirWalker.java) of csv files that contain csv files with customer info. A simple sample in provided in with the sample code but you MUST will r ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As