Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

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

Assessment database and multithread programmingtasktask 1

Assessment: Database and Multithread Programming Task Task 1: Grade Processing University grading system maintains a database called "GradeProcessing" that contains number of tables to store, retrieve and manipulate stud ...

Can someone please help me with the following java

can someone please help me with the following java question The input is an N by N matrix of nonnegative integers. Each individual row is a decreasing sequence from left to right. Each individual column is a decreasing s ...

Assessment socket programmingtaskwrite a java gui program

Assessment: Socket Programming Task Write a JAVA GUI program that would facilitate text chatting/exchanging between two or multiple computers over the network/internet, using the concept of JAVA socket programming. If yo ...

Assignment - java program using array of objectsobjectives

Assignment - 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 Menu ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

Operating systems assignment -problem 1 sharing the bridgea

Operating Systems Assignment - Problem 1: Sharing the Bridge A new single lane bridge is constructed to connect the North Island of New Zealand to the South Island of New Zealand. Farmers from each island use the bridge ...

Project requirementsfor the problem described in the next

Project requirements For the problem described in the next section, you must do the following: 1. include your student ID at the end of all filenames for all java code files. Three classes have been identified in section ...

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 instructionsin this assessment you will design

Assessment Instructions In this assessment, you will design and code a simple Java application that defines a class, instantiate the class into a number of objects, and prints out the attributes of these objects in a spe ...

Simple order processing systemquestion given the classes

Simple Order Processing System Question: Given the classes Ship (with getter and setter), Speedboat, and SpeedboatTest. Answer the following questions: Refine the whole application (all classes) and create Abstract class ...

  • 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