SlideShare a Scribd company logo
public class Passenger {
public static enum Section {
First, Economy
}
private String name;
private Section section;
private String confirmationCode;
/**
* Constructor
*
* @param name
* @param section
* @param confirmationCode
*/
public Passenger(String name, Section section, String confirmationCode) {
this.name = name;
this.section = section;
this.confirmationCode = confirmationCode;
}
/**
* Copy Constructor that produces a deep copy of the argument
*
* @param
*/
public Passenger(Passenger p) {
this.name = p.name;
this.section = p.section;
this.confirmationCode = p.confirmationCode;
}
/**
* Returns a string with confirmation code
*/
public static String generateCode() {
String alphabet = "abcdefghijklmnopqrstuvwxyz";
String[] letters = new String[6];
String code = "";
for (int i = 0; i < 6; i++) {
letters[i] = "" + alphabet.charAt((int) Math.floor(Math.random() * 26));
code += letters[i];
}
return code.toUpperCase();
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the section
*/
public Section getSection() {
return section;
}
/**
* @param section the section to set
*/
public void setSection(Section section) {
this.section = section;
}
/**
* @return the confirmationCode
*/
public String getConfirmationCode() {
return confirmationCode;
}
/**
* @param confirmationCode the confirmationCode to set
*/
public void setConfirmationCode(String confirmationCode) {
this.confirmationCode = confirmationCode;
}
@Override
public String toString() {
return " Passenger name: " + getName() +
" Class: " + getSection() +
" Confirmation Code: " + getConfirmationCode();
}
}
import java.util.Scanner;
public class Manifest {
public static void printBP(Passenger p) {
System.out.print(String.format(" %30s", " ").replaceAll(" ", "-"));
System.out.print(p);
System.out.print(String.format(" %30s", " ").replaceAll(" ", "-"));
}
public static void main(String[] args) {
final int FIRST_CAPACITY = 2;
final int ECO_CAPACITY = 4;
// Scanner to get user input
Scanner in = new Scanner(System.in);
// Create passenger array
Passenger[] pList = new Passenger[6];
int reservationCount = 0;
// Variables to track whether a section is full
int firstSeats = 0;
int ecoSeats = 0;
char ch = ' ';
do {
// Get pax name
System.out.print("Enter the passenger's full name: ");
String name = in.nextLine();
// Get section
int section = -1;
while (true) {
System.out.println("Select a section 0-First, 1-Economy: ");
section = in.nextInt();
// Check if selected section is full
if (section == 0) {
if (firstSeats == FIRST_CAPACITY) {
System.out.println("First class seats are full. Do you wan to reserve a seat in the
economy section[Y/N]: ");
char reserve = in.next().charAt(0);
//If user does not want to make the reservation
//finish booking without a reservation
if(Character.toUpperCase(reserve) == 'N')
break;
section = 1; //Change section to economy if user selects Y
ecoSeats += 1;
} else
firstSeats += 1;
} else if (section == 1) {
if (ecoSeats == ECO_CAPACITY) {
System.out.println("Economy class seats are full. Do you wan to reserve a seat in
the first section[Y/N]: ");
char reserve = in.next().charAt(0);
//If user does not want to make the reservation
//finish booking without a reservation
if(Character.toUpperCase(reserve) == 'N')
break;
section = 0; //Change section to economy if user selects Y
firstSeats += 1;
} else
ecoSeats += 1;
} else {
System.out.println("Invalid section. Please try again.");
continue;
}
//Make reservation
// Generate code
String code = Passenger.generateCode();
pList[reservationCount] = new Passenger(name, Passenger.Section.values()[section],
code);
printBP(pList[reservationCount]); //Print BP
reservationCount += 1;
break;
}
if(reservationCount == 6)
break;
else {
System.out.println(" Do you want to make another reservation[Y/N]: ");
ch = in.next().charAt(0);
}
//Clear keyboard buffer
in.nextLine();
} while (Character.toUpperCase(ch) == 'Y');
// Close scanner
in.close();
//Print manifest
System.out.println("MANIFEST");
System.out.printf(" %-20s %-20s %-10s", "Confirmation Code", "Passenger",
"Class");
System.out.print(String.format(" %52s", " ").replaceAll(" ", "-"));
for (Passenger pax : pList) {
if(pax != null)
System.out.printf(" %-20s %-20s %-10s", pax.getConfirmationCode(),
pax.getName(), pax.getSection());
}
}
}
SAMPLE OUTPUT:
Enter the passenger's full name: Passenger 1
Select a section 0-First, 1-Economy:
1
------------------------------
Passenger name: Passenger 1
Class: Economy
Confirmation Code: UZIBJN
------------------------------
Do you want to make another reservation[Y/N]:
y
Enter the passenger's full name: Passenger 2
Select a section 0-First, 1-Economy:
0
------------------------------
Passenger name: Passenger 2
Class: First
Confirmation Code: IRHYRQ
------------------------------
Do you want to make another reservation[Y/N]:
y
Enter the passenger's full name: Passenger 3
Select a section 0-First, 1-Economy:
0
------------------------------
Passenger name: Passenger 3
Class: First
Confirmation Code: AAGOYD
------------------------------
Do you want to make another reservation[Y/N]:
y
Enter the passenger's full name: Passenger 4
Select a section 0-First, 1-Economy:
0
First class seats are full. Do you wan to reserve a seat in the economy section[Y/N]:
n
Do you want to make another reservation[Y/N]:
y
Enter the passenger's full name: Passenger 5
Select a section 0-First, 1-Economy:
0
First class seats are full. Do you wan to reserve a seat in the economy section[Y/N]:
y
------------------------------
Passenger name: Passenger 5
Class: Economy
Confirmation Code: DFFWQN
------------------------------
Do you want to make another reservation[Y/N]:
n
MANIFEST
Confirmation Code Passenger Class
----------------------------------------------------
UZIBJN Passenger 1 Economy
IRHYRQ Passenger 2 First
AAGOYD Passenger 3 First
DFFWQN Passenger 5 Economy
Solution
public class Passenger {
public static enum Section {
First, Economy
}
private String name;
private Section section;
private String confirmationCode;
/**
* Constructor
*
* @param name
* @param section
* @param confirmationCode
*/
public Passenger(String name, Section section, String confirmationCode) {
this.name = name;
this.section = section;
this.confirmationCode = confirmationCode;
}
/**
* Copy Constructor that produces a deep copy of the argument
*
* @param
*/
public Passenger(Passenger p) {
this.name = p.name;
this.section = p.section;
this.confirmationCode = p.confirmationCode;
}
/**
* Returns a string with confirmation code
*/
public static String generateCode() {
String alphabet = "abcdefghijklmnopqrstuvwxyz";
String[] letters = new String[6];
String code = "";
for (int i = 0; i < 6; i++) {
letters[i] = "" + alphabet.charAt((int) Math.floor(Math.random() * 26));
code += letters[i];
}
return code.toUpperCase();
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the section
*/
public Section getSection() {
return section;
}
/**
* @param section the section to set
*/
public void setSection(Section section) {
this.section = section;
}
/**
* @return the confirmationCode
*/
public String getConfirmationCode() {
return confirmationCode;
}
/**
* @param confirmationCode the confirmationCode to set
*/
public void setConfirmationCode(String confirmationCode) {
this.confirmationCode = confirmationCode;
}
@Override
public String toString() {
return " Passenger name: " + getName() +
" Class: " + getSection() +
" Confirmation Code: " + getConfirmationCode();
}
}
import java.util.Scanner;
public class Manifest {
public static void printBP(Passenger p) {
System.out.print(String.format(" %30s", " ").replaceAll(" ", "-"));
System.out.print(p);
System.out.print(String.format(" %30s", " ").replaceAll(" ", "-"));
}
public static void main(String[] args) {
final int FIRST_CAPACITY = 2;
final int ECO_CAPACITY = 4;
// Scanner to get user input
Scanner in = new Scanner(System.in);
// Create passenger array
Passenger[] pList = new Passenger[6];
int reservationCount = 0;
// Variables to track whether a section is full
int firstSeats = 0;
int ecoSeats = 0;
char ch = ' ';
do {
// Get pax name
System.out.print("Enter the passenger's full name: ");
String name = in.nextLine();
// Get section
int section = -1;
while (true) {
System.out.println("Select a section 0-First, 1-Economy: ");
section = in.nextInt();
// Check if selected section is full
if (section == 0) {
if (firstSeats == FIRST_CAPACITY) {
System.out.println("First class seats are full. Do you wan to reserve a seat in the
economy section[Y/N]: ");
char reserve = in.next().charAt(0);
//If user does not want to make the reservation
//finish booking without a reservation
if(Character.toUpperCase(reserve) == 'N')
break;
section = 1; //Change section to economy if user selects Y
ecoSeats += 1;
} else
firstSeats += 1;
} else if (section == 1) {
if (ecoSeats == ECO_CAPACITY) {
System.out.println("Economy class seats are full. Do you wan to reserve a seat in
the first section[Y/N]: ");
char reserve = in.next().charAt(0);
//If user does not want to make the reservation
//finish booking without a reservation
if(Character.toUpperCase(reserve) == 'N')
break;
section = 0; //Change section to economy if user selects Y
firstSeats += 1;
} else
ecoSeats += 1;
} else {
System.out.println("Invalid section. Please try again.");
continue;
}
//Make reservation
// Generate code
String code = Passenger.generateCode();
pList[reservationCount] = new Passenger(name, Passenger.Section.values()[section],
code);
printBP(pList[reservationCount]); //Print BP
reservationCount += 1;
break;
}
if(reservationCount == 6)
break;
else {
System.out.println(" Do you want to make another reservation[Y/N]: ");
ch = in.next().charAt(0);
}
//Clear keyboard buffer
in.nextLine();
} while (Character.toUpperCase(ch) == 'Y');
// Close scanner
in.close();
//Print manifest
System.out.println("MANIFEST");
System.out.printf(" %-20s %-20s %-10s", "Confirmation Code", "Passenger",
"Class");
System.out.print(String.format(" %52s", " ").replaceAll(" ", "-"));
for (Passenger pax : pList) {
if(pax != null)
System.out.printf(" %-20s %-20s %-10s", pax.getConfirmationCode(),
pax.getName(), pax.getSection());
}
}
}
SAMPLE OUTPUT:
Enter the passenger's full name: Passenger 1
Select a section 0-First, 1-Economy:
1
------------------------------
Passenger name: Passenger 1
Class: Economy
Confirmation Code: UZIBJN
------------------------------
Do you want to make another reservation[Y/N]:
y
Enter the passenger's full name: Passenger 2
Select a section 0-First, 1-Economy:
0
------------------------------
Passenger name: Passenger 2
Class: First
Confirmation Code: IRHYRQ
------------------------------
Do you want to make another reservation[Y/N]:
y
Enter the passenger's full name: Passenger 3
Select a section 0-First, 1-Economy:
0
------------------------------
Passenger name: Passenger 3
Class: First
Confirmation Code: AAGOYD
------------------------------
Do you want to make another reservation[Y/N]:
y
Enter the passenger's full name: Passenger 4
Select a section 0-First, 1-Economy:
0
First class seats are full. Do you wan to reserve a seat in the economy section[Y/N]:
n
Do you want to make another reservation[Y/N]:
y
Enter the passenger's full name: Passenger 5
Select a section 0-First, 1-Economy:
0
First class seats are full. Do you wan to reserve a seat in the economy section[Y/N]:
y
------------------------------
Passenger name: Passenger 5
Class: Economy
Confirmation Code: DFFWQN
------------------------------
Do you want to make another reservation[Y/N]:
n
MANIFEST
Confirmation Code Passenger Class
----------------------------------------------------
UZIBJN Passenger 1 Economy
IRHYRQ Passenger 2 First
AAGOYD Passenger 3 First
DFFWQN Passenger 5 Economy

More Related Content

PDF
I want to write this program in java.Write a simple airline ticket.pdf
PDF
Complete the following- The int planTrip(int numPassengers- String tri.pdf
PDF
package reservation; import java.util.; For Scanner Class .pdf
DOCX
You work for an airline, a small airline, so small you have only one.docx
TXT
Code
PDF
import java-util-ArrayList- public class Bus { private String na.pdf
PDF
Create a Flight class that uses the Plane and Time class. This class.pdf
PDF
In this assignment you will practice creating classes and enumeratio.pdf
I want to write this program in java.Write a simple airline ticket.pdf
Complete the following- The int planTrip(int numPassengers- String tri.pdf
package reservation; import java.util.; For Scanner Class .pdf
You work for an airline, a small airline, so small you have only one.docx
Code
import java-util-ArrayList- public class Bus { private String na.pdf
Create a Flight class that uses the Plane and Time class. This class.pdf
In this assignment you will practice creating classes and enumeratio.pdf

More from annesmkt (20)

PDF
#include iostream #include fstream #include cstdlib #.pdf
PDF
six lone pairs (each F has three lone pairs) nit.pdf
PDF
silver cyanide i think coz HCN will be formed whi.pdf
PDF
Rh (Rhodium) is the answer. Solution .pdf
PDF
please rate me.... .pdf
PDF
No. The Mo is in +3 oxidation state and thus its .pdf
PDF
In the automata theory, a nondeterministic finite.pdf
PDF
Formaldehyde is an organic compound with the form.pdf
PDF
d. the molecular orbital diagram for O2. .pdf
PDF
Water molecules do not participate in acid base neutralization react.pdf
PDF
The Mouse and the track ball uses pointing and dragging tasks.The ac.pdf
PDF
The employer must estimate the total cost of the benefits promised a.pdf
PDF
slopeSolutionslope.pdf
PDF
Plz send the clear image to me...so that i can solve it..Solutio.pdf
PDF
Prior to 2010, the United States banned HIV-positive individuals fro.pdf
PDF
option (e) is correct.the feature that distinguishes fluorescence .pdf
PDF
NADP is the source of electrons for the light reactionsThe electro.pdf
PDF
import java.util.Scanner;public class Bottle {    private stat.pdf
PDF
In previous year, 2 birthdays were on tuesday but this year theres.pdf
PDF
Homology is defined as structures which are derived from a common an.pdf
#include iostream #include fstream #include cstdlib #.pdf
six lone pairs (each F has three lone pairs) nit.pdf
silver cyanide i think coz HCN will be formed whi.pdf
Rh (Rhodium) is the answer. Solution .pdf
please rate me.... .pdf
No. The Mo is in +3 oxidation state and thus its .pdf
In the automata theory, a nondeterministic finite.pdf
Formaldehyde is an organic compound with the form.pdf
d. the molecular orbital diagram for O2. .pdf
Water molecules do not participate in acid base neutralization react.pdf
The Mouse and the track ball uses pointing and dragging tasks.The ac.pdf
The employer must estimate the total cost of the benefits promised a.pdf
slopeSolutionslope.pdf
Plz send the clear image to me...so that i can solve it..Solutio.pdf
Prior to 2010, the United States banned HIV-positive individuals fro.pdf
option (e) is correct.the feature that distinguishes fluorescence .pdf
NADP is the source of electrons for the light reactionsThe electro.pdf
import java.util.Scanner;public class Bottle {    private stat.pdf
In previous year, 2 birthdays were on tuesday but this year theres.pdf
Homology is defined as structures which are derived from a common an.pdf

Recently uploaded (20)

PPTX
Cell Structure & Organelles in detailed.
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Institutional Correction lecture only . . .
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
master seminar digital applications in india
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
RMMM.pdf make it easy to upload and study
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Computing-Curriculum for Schools in Ghana
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Classroom Observation Tools for Teachers
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
Cell Structure & Organelles in detailed.
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Institutional Correction lecture only . . .
Anesthesia in Laparoscopic Surgery in India
102 student loan defaulters named and shamed – Is someone you know on the list?
202450812 BayCHI UCSC-SV 20250812 v17.pptx
master seminar digital applications in india
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Chinmaya Tiranga quiz Grand Finale.pdf
RMMM.pdf make it easy to upload and study
GDM (1) (1).pptx small presentation for students
Computing-Curriculum for Schools in Ghana
Microbial disease of the cardiovascular and lymphatic systems
Supply Chain Operations Speaking Notes -ICLT Program
Classroom Observation Tools for Teachers
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Final Presentation General Medicine 03-08-2024.pptx
Final Presentation General Medicine 03-08-2024.pptx

public class Passenger {    public static enum Section {        .pdf

  • 1. public class Passenger { public static enum Section { First, Economy } private String name; private Section section; private String confirmationCode; /** * Constructor * * @param name * @param section * @param confirmationCode */ public Passenger(String name, Section section, String confirmationCode) { this.name = name; this.section = section; this.confirmationCode = confirmationCode; } /** * Copy Constructor that produces a deep copy of the argument * * @param */ public Passenger(Passenger p) { this.name = p.name; this.section = p.section; this.confirmationCode = p.confirmationCode; } /** * Returns a string with confirmation code */ public static String generateCode() { String alphabet = "abcdefghijklmnopqrstuvwxyz";
  • 2. String[] letters = new String[6]; String code = ""; for (int i = 0; i < 6; i++) { letters[i] = "" + alphabet.charAt((int) Math.floor(Math.random() * 26)); code += letters[i]; } return code.toUpperCase(); } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the section */ public Section getSection() { return section; } /** * @param section the section to set */ public void setSection(Section section) { this.section = section; } /** * @return the confirmationCode */ public String getConfirmationCode() {
  • 3. return confirmationCode; } /** * @param confirmationCode the confirmationCode to set */ public void setConfirmationCode(String confirmationCode) { this.confirmationCode = confirmationCode; } @Override public String toString() { return " Passenger name: " + getName() + " Class: " + getSection() + " Confirmation Code: " + getConfirmationCode(); } } import java.util.Scanner; public class Manifest { public static void printBP(Passenger p) { System.out.print(String.format(" %30s", " ").replaceAll(" ", "-")); System.out.print(p); System.out.print(String.format(" %30s", " ").replaceAll(" ", "-")); } public static void main(String[] args) { final int FIRST_CAPACITY = 2; final int ECO_CAPACITY = 4; // Scanner to get user input Scanner in = new Scanner(System.in); // Create passenger array Passenger[] pList = new Passenger[6]; int reservationCount = 0; // Variables to track whether a section is full int firstSeats = 0; int ecoSeats = 0; char ch = ' ';
  • 4. do { // Get pax name System.out.print("Enter the passenger's full name: "); String name = in.nextLine(); // Get section int section = -1; while (true) { System.out.println("Select a section 0-First, 1-Economy: "); section = in.nextInt(); // Check if selected section is full if (section == 0) { if (firstSeats == FIRST_CAPACITY) { System.out.println("First class seats are full. Do you wan to reserve a seat in the economy section[Y/N]: "); char reserve = in.next().charAt(0); //If user does not want to make the reservation //finish booking without a reservation if(Character.toUpperCase(reserve) == 'N') break; section = 1; //Change section to economy if user selects Y ecoSeats += 1; } else firstSeats += 1; } else if (section == 1) { if (ecoSeats == ECO_CAPACITY) { System.out.println("Economy class seats are full. Do you wan to reserve a seat in the first section[Y/N]: "); char reserve = in.next().charAt(0); //If user does not want to make the reservation //finish booking without a reservation if(Character.toUpperCase(reserve) == 'N') break;
  • 5. section = 0; //Change section to economy if user selects Y firstSeats += 1; } else ecoSeats += 1; } else { System.out.println("Invalid section. Please try again."); continue; } //Make reservation // Generate code String code = Passenger.generateCode(); pList[reservationCount] = new Passenger(name, Passenger.Section.values()[section], code); printBP(pList[reservationCount]); //Print BP reservationCount += 1; break; } if(reservationCount == 6) break; else { System.out.println(" Do you want to make another reservation[Y/N]: "); ch = in.next().charAt(0); } //Clear keyboard buffer in.nextLine(); } while (Character.toUpperCase(ch) == 'Y'); // Close scanner in.close(); //Print manifest System.out.println("MANIFEST"); System.out.printf(" %-20s %-20s %-10s", "Confirmation Code", "Passenger", "Class"); System.out.print(String.format(" %52s", " ").replaceAll(" ", "-"));
  • 6. for (Passenger pax : pList) { if(pax != null) System.out.printf(" %-20s %-20s %-10s", pax.getConfirmationCode(), pax.getName(), pax.getSection()); } } } SAMPLE OUTPUT: Enter the passenger's full name: Passenger 1 Select a section 0-First, 1-Economy: 1 ------------------------------ Passenger name: Passenger 1 Class: Economy Confirmation Code: UZIBJN ------------------------------ Do you want to make another reservation[Y/N]: y Enter the passenger's full name: Passenger 2 Select a section 0-First, 1-Economy: 0 ------------------------------ Passenger name: Passenger 2 Class: First Confirmation Code: IRHYRQ ------------------------------ Do you want to make another reservation[Y/N]: y Enter the passenger's full name: Passenger 3 Select a section 0-First, 1-Economy: 0 ------------------------------ Passenger name: Passenger 3 Class: First Confirmation Code: AAGOYD ------------------------------
  • 7. Do you want to make another reservation[Y/N]: y Enter the passenger's full name: Passenger 4 Select a section 0-First, 1-Economy: 0 First class seats are full. Do you wan to reserve a seat in the economy section[Y/N]: n Do you want to make another reservation[Y/N]: y Enter the passenger's full name: Passenger 5 Select a section 0-First, 1-Economy: 0 First class seats are full. Do you wan to reserve a seat in the economy section[Y/N]: y ------------------------------ Passenger name: Passenger 5 Class: Economy Confirmation Code: DFFWQN ------------------------------ Do you want to make another reservation[Y/N]: n MANIFEST Confirmation Code Passenger Class ---------------------------------------------------- UZIBJN Passenger 1 Economy IRHYRQ Passenger 2 First AAGOYD Passenger 3 First DFFWQN Passenger 5 Economy Solution public class Passenger { public static enum Section { First, Economy }
  • 8. private String name; private Section section; private String confirmationCode; /** * Constructor * * @param name * @param section * @param confirmationCode */ public Passenger(String name, Section section, String confirmationCode) { this.name = name; this.section = section; this.confirmationCode = confirmationCode; } /** * Copy Constructor that produces a deep copy of the argument * * @param */ public Passenger(Passenger p) { this.name = p.name; this.section = p.section; this.confirmationCode = p.confirmationCode; } /** * Returns a string with confirmation code */ public static String generateCode() { String alphabet = "abcdefghijklmnopqrstuvwxyz"; String[] letters = new String[6]; String code = ""; for (int i = 0; i < 6; i++) { letters[i] = "" + alphabet.charAt((int) Math.floor(Math.random() * 26)); code += letters[i]; }
  • 9. return code.toUpperCase(); } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the section */ public Section getSection() { return section; } /** * @param section the section to set */ public void setSection(Section section) { this.section = section; } /** * @return the confirmationCode */ public String getConfirmationCode() { return confirmationCode; } /** * @param confirmationCode the confirmationCode to set */ public void setConfirmationCode(String confirmationCode) {
  • 10. this.confirmationCode = confirmationCode; } @Override public String toString() { return " Passenger name: " + getName() + " Class: " + getSection() + " Confirmation Code: " + getConfirmationCode(); } } import java.util.Scanner; public class Manifest { public static void printBP(Passenger p) { System.out.print(String.format(" %30s", " ").replaceAll(" ", "-")); System.out.print(p); System.out.print(String.format(" %30s", " ").replaceAll(" ", "-")); } public static void main(String[] args) { final int FIRST_CAPACITY = 2; final int ECO_CAPACITY = 4; // Scanner to get user input Scanner in = new Scanner(System.in); // Create passenger array Passenger[] pList = new Passenger[6]; int reservationCount = 0; // Variables to track whether a section is full int firstSeats = 0; int ecoSeats = 0; char ch = ' '; do { // Get pax name System.out.print("Enter the passenger's full name: "); String name = in.nextLine(); // Get section int section = -1;
  • 11. while (true) { System.out.println("Select a section 0-First, 1-Economy: "); section = in.nextInt(); // Check if selected section is full if (section == 0) { if (firstSeats == FIRST_CAPACITY) { System.out.println("First class seats are full. Do you wan to reserve a seat in the economy section[Y/N]: "); char reserve = in.next().charAt(0); //If user does not want to make the reservation //finish booking without a reservation if(Character.toUpperCase(reserve) == 'N') break; section = 1; //Change section to economy if user selects Y ecoSeats += 1; } else firstSeats += 1; } else if (section == 1) { if (ecoSeats == ECO_CAPACITY) { System.out.println("Economy class seats are full. Do you wan to reserve a seat in the first section[Y/N]: "); char reserve = in.next().charAt(0); //If user does not want to make the reservation //finish booking without a reservation if(Character.toUpperCase(reserve) == 'N') break; section = 0; //Change section to economy if user selects Y firstSeats += 1; } else ecoSeats += 1; } else { System.out.println("Invalid section. Please try again.");
  • 12. continue; } //Make reservation // Generate code String code = Passenger.generateCode(); pList[reservationCount] = new Passenger(name, Passenger.Section.values()[section], code); printBP(pList[reservationCount]); //Print BP reservationCount += 1; break; } if(reservationCount == 6) break; else { System.out.println(" Do you want to make another reservation[Y/N]: "); ch = in.next().charAt(0); } //Clear keyboard buffer in.nextLine(); } while (Character.toUpperCase(ch) == 'Y'); // Close scanner in.close(); //Print manifest System.out.println("MANIFEST"); System.out.printf(" %-20s %-20s %-10s", "Confirmation Code", "Passenger", "Class"); System.out.print(String.format(" %52s", " ").replaceAll(" ", "-")); for (Passenger pax : pList) { if(pax != null) System.out.printf(" %-20s %-20s %-10s", pax.getConfirmationCode(), pax.getName(), pax.getSection()); } }
  • 13. } SAMPLE OUTPUT: Enter the passenger's full name: Passenger 1 Select a section 0-First, 1-Economy: 1 ------------------------------ Passenger name: Passenger 1 Class: Economy Confirmation Code: UZIBJN ------------------------------ Do you want to make another reservation[Y/N]: y Enter the passenger's full name: Passenger 2 Select a section 0-First, 1-Economy: 0 ------------------------------ Passenger name: Passenger 2 Class: First Confirmation Code: IRHYRQ ------------------------------ Do you want to make another reservation[Y/N]: y Enter the passenger's full name: Passenger 3 Select a section 0-First, 1-Economy: 0 ------------------------------ Passenger name: Passenger 3 Class: First Confirmation Code: AAGOYD ------------------------------ Do you want to make another reservation[Y/N]: y Enter the passenger's full name: Passenger 4 Select a section 0-First, 1-Economy: 0 First class seats are full. Do you wan to reserve a seat in the economy section[Y/N]:
  • 14. n Do you want to make another reservation[Y/N]: y Enter the passenger's full name: Passenger 5 Select a section 0-First, 1-Economy: 0 First class seats are full. Do you wan to reserve a seat in the economy section[Y/N]: y ------------------------------ Passenger name: Passenger 5 Class: Economy Confirmation Code: DFFWQN ------------------------------ Do you want to make another reservation[Y/N]: n MANIFEST Confirmation Code Passenger Class ---------------------------------------------------- UZIBJN Passenger 1 Economy IRHYRQ Passenger 2 First AAGOYD Passenger 3 First DFFWQN Passenger 5 Economy