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 sect.
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