Using the the code below need to add method that adds a passengers to a queue, r
ID: 3817613 • Letter: U
Question
Using the the code below need to add method that adds a passengers to a queue, removes them from a queue and displays information about the passenger.
int main()
{
char choice;
do
{
System.out.println("Enter A(dd), R(emove), S(can), or Q(uit)>");
//scan the input
switch(toupper(choice))
{
case 'A':
//ADD PASSENGERS TO THE QUEUE
break;
case 'R':
//REMOVE PASSENGER FROM THE QUEUE
break;
case 'S':
//SCAN AND DISPLAY DETAILS OF A PARTICULAR PASSENGER
break;
case 'Q':
//DISPLAY CONTENTS IN QUEUE AND QUIT THE LOOP
break;
default: system.out.prinntln("Invalid choice ---try again ");
}
} while (toupper(choice) ! = ‘Q’);
return (0);
}
the rest of the program:
class Passenger {
public String Name;
public String ticket_type;
public int seat_no;
public Passenger() {
}
public Passenger(String name, String type, int num) {
this.Name = name;
this.ticket_type = type;
this.seat_no = num;
}
public String toString() {
return Name + " " + ticket_type;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
class PassengerData extends Passenger {
public PassengerData(String name, String type, int num) {
super(name, type,num);
}
---------------------------------------------------------------------------------------------------------------------------------------------
}
class CreateQueue<E> {
private LinkedList<E> list = new LinkedList<E>();
public void enqueue(E item) { //SAME AS PUSH IN STACK
list.addLast(item);
}
public E dequeue() { //SAME AS POP IN STACK
return list.poll();
}
public boolean hasItems() { //CHECKS IF THE QUEUE IS EMPTY OR NOT
return !list.isEmpty();
}
public int size() { //DETERMINES THE SIZE OF QUEUE
return list.size();
}
public void addItems(CreateQueue<? extends E> q) { // ADDS ITEMS TO
LINKED LIST
while (q.hasItems())
list.addLast(q.dequeue());
}
Explanation / Answer
package com.example;
import java.util.LinkedList;
import java.util.Scanner;
public class Chegg {
public static void main(String args[]) {
char choice;
Scanner sc = new Scanner(System.in);
LinkedList<Passenger> passengerQueue = new LinkedList<Passenger>();
do {
System.out.println("Enter A(dd), R(emove), S(can), or Q(uit)>");
// scan the input
choice = Character.toUpperCase(sc.next().charAt(0));
switch (choice) {
case 'A':
// ADD PASSENGERS TO THE QUEUE
System.out.println("Enter name:");
String name = sc.next();
System.out.println("Enter ticket type:");
String type = sc.next();
System.out.println("Enter ticket number:");
int num = sc.nextInt();
Passenger passenger = new Passenger(name, type, num);
passengerQueue.addFirst(passenger);
break;
case 'R':
// REMOVE PASSENGER FROM THE QUEUE
passengerQueue.removeFirst();
break;
case 'S':
// SCAN AND DISPLAY DETAILS OF A PARTICULAR PASSENGER
System.out.println("Enter passenger name to get his details:");
String tempName = sc.next();
for (Passenger p : passengerQueue) {
if (p.Name.equalsIgnoreCase(tempName)) {
System.out.println(p.Name + " " + p.ticket_type + " "
+ p.seat_no);
}
}
break;
case 'Q':
// DISPLAY CONTENTS IN QUEUE AND QUIT THE LOOP
for (Passenger p : passengerQueue) {
System.out.println(p.Name + " " + p.ticket_type + " "
+ p.seat_no);
}
break;
default:
System.out.println("Invalid choice ---try again ");
}
} while (choice != 'Q');
sc.close();
}
}
class Passenger {
public String Name;
public String ticket_type;
public int seat_no;
public Passenger(String name, String type, int num) {
this.Name = name;
this.ticket_type = type;
this.seat_no = num;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.