Code
Code
View
all flights");
import java.util.Scanner; @Override
System.out.println("3. Log
out"); public void handleActions() {
System.out.println("3. View
public User(int userId, String
switch (choice) { My Bookings");
username, String password) {
case 1 -> System.out.println("4. Log
this.userId = userId;
AerowayBookingSystem.addFlight( out");
this.username = username; );
System.out.print("Choose an
this.password = password; case 2 -> option: ");
AerowayBookingSystem.viewFlight
} int choice =
s();
AerowayBookingSystem.scanner.ne
case 3 -> { xtInt();
public abstract void
handleActions(); AerowayBookingSystem.scanner.ne
System.out.println("Logging
out..."); xtLine(); // consume newline
}
return;
} switch (choice) {
// Admin class
default -> case 1 ->
class Admin extends User {
System.out.println("Invalid AerowayBookingSystem.viewFlight
public Admin(int userId, String choice."); s();
username, String password) {
} case 2 ->
super(userId, username, AerowayBookingSystem.bookFligh
password); // Call the parent } t(userId);
constructor
} case 3 ->
} AerowayBookingSystem.viewBook
} ings(userId);
// RegularUser class case 4 -> {
@Override
class RegularUser extends User {
public void handleActions() { System.out.println("Logging
public RegularUser(int userId,
out...");
while (true) { String username, String password) {
return;
super(userId, username,
System.out.println("Welcome, password); // Call the parent }
Admin!"); constructor
default ->
System.out.println("1. Add a } System.out.println("Invalid
new flight"); choice.");
} switch (choice) { PreparedStatement stmt =
conn.prepareStatement(query);
} case 1 -> register();
stmt.setString(1, username);
} case 2 -> login();
stmt.setString(2, password);
} case 3 -> {
stmt.setString(3, email); //
Save the email to the database
System.out.println("Exiting
// Main class implementing the system..."); stmt.setBoolean(4,
booking system isAdmin);
System.exit(0);
public class stmt.executeUpdate();
AerowayBookingSystem { }
System.out.println("------------------- }
--------------");
// Retrieve flight details for }
} receipt generation
} catch (SQLException e) {
} catch (SQLException e) { query = "SELECT
f.flight_name, f.departure, System.out.println("Error: "
System.out.println("Error: " f.destination, f.date, f.time, f.cost + e.getMessage());
+ e.getMessage()); FROM flights f WHERE f.flight_id
}
= ?";
}
}
stmt =
}
conn.prepareStatement(query);
public static void bookFlight(int
stmt.setInt(1, flightId); public static boolean
userId) {
processPayment(double amount) {
ResultSet rs =
System.out.print("Enter the
stmt.executeQuery(); System.out.print("Select a
Flight ID you want to book: ");
payment method:");
int flightId =
Integer.parseInt(scanner.nextLine()); System.out.print("1. Credit
if (rs.next()) {
Card");
String ticketNumber = "TKT" String flightName =
+ System.currentTimeMillis(); // System.out.print("2. Cash");
rs.getString("flight_name");
Generate unique ticket number
System.out.print("Choose an
String departure =
option: ");
rs.getString("departure");
try (Connection conn = int choice = scanner.nextInt();
String destination =
DatabaseManager.connect()) { rs.getString("destination"); scanner.nextLine(); // Consume
// Insert booking into the newline
Date date =
bookings table rs.getDate("date");
String query = "INSERT Time time = boolean paymentSuccess =
INTO bookings (ticket_number, rs.getTime("time"); false;
user_id, flight_id, status,
booking_date) VALUES double cost =
(?, ?, ?, ?, ?)"; rs.getDouble("cost");
switch (choice) {
PreparedStatement stmt =
conn.prepareStatement(query); case 1:
// Process Payment
stmt.setString(1, paymentSuccess =
ticketNumber); boolean paymentSuccess processCreditCardPayment(amount)
= processPayment(cost); ;
stmt.setInt(2, userId);
if (paymentSuccess) { break;
stmt.setInt(3, flightId);
// Print Booking case 2:
stmt.setString(4, "Booked"); Receipt after successful payment
paymentSuccess =
stmt.setTimestamp(5, new processCashPayment(amount);
Timestamp(System.currentTimeMill printBookingReceipt(flightName,
is())); departure, destination, cost, break;
ticketNumber);
stmt.executeUpdate(); default:
} else {
return true; destination, double cost, String
System.out.println("Invalid choice. ticketNumber) {
Please select a valid payment }
method.");
} System.out.println("===========
break; ==========================
=====");
}
public static boolean
System.out.println(" *
processCreditCardPayment(double
BOOKING RECEIPT *");
amount) {
return paymentSuccess;
System.out.print("Enter your
System.out.println("===========
} credit card number: ");
==========================
String cardNumber = =====");
scanner.nextLine();
public static boolean System.out.println("Ticket
processCashPayment(double System.out.print("Enter the Number: " + ticketNumber);
amount) { expiration date (MM/YY): ");
System.out.println("Flight: " +
System.out.print("Enter the String expiryDate = flightName);
amount of cash you are paying: "); scanner.nextLine();
System.out.println("Departure:
double cashPaid = System.out.print("Enter the " + departure);
scanner.nextDouble(); CVV: ");
System.out.println("Cost: $" +
cost);
if (cashPaid < amount) { // For simplicity, just simulate
success if the card number is valid System.out.println("Status:
(dummy check) Booked");
System.out.print("Insufficient
amount. Payment failed."); if (cardNumber.length() == 16
&& expiryDate.length() == 5 && System.out.println("===========
return false; cvv.length() == 3) { ==========================
=====");
} else { System.out.print("Credit
card payment of $" + amount + " System.out.println(" Thank
double change = cashPaid -
has been processed successfully."); you for booking with us!");
amount;
return true;
System.out.printf("Payment
System.out.println("===========
of $%.2f received. ", cashPaid); } else { ==========================
if (change > 0) { =====");
System.out.print("Invalid
credit card details. Payment }
System.out.printf("Your
failed.");
change is $%.2f.\n", change);
return false;
} else {
}
System.out.println("Thank you for }
the exact payment.");
System.out.println("Error: "
+ e.getMessage());
// Check if there are any
bookings }
if (!rs.next()) { }
System.out.println("You }
don't have any booked flights yet.
Please Book a Flight First.");
} else {
do {
System.out.println("Ticket Number:
" + rs.getString("ticket_number"));
System.out.println("Flight: " +
rs.getString("flight_name"));