[go: up one dir, main page]

0% found this document useful (0 votes)
17 views6 pages

Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

Code

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

import java.sql.*; System.out.println("2.

View
all flights");
import java.util.Scanner; @Override
System.out.println("3. Log
out"); public void handleActions() {

// Abstract base class for common System.out.print("Choose an while (true) {


functionality option: ");
abstract class User { int choice = System.out.println("Welcome,
AerowayBookingSystem.scanner.ne User!");
protected int userId;
xtInt();
System.out.println("1. View
protected String username; Flights");
protected String password; AerowayBookingSystem.scanner.ne
System.out.println("2. Book
xtLine(); // consume newline
a Flight");

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 { }

public static final Scanner default -> System.out.println("Registration


scanner = new Scanner(System.in); System.out.println("Invalid choice. successful.");
Please try again.");
} catch (SQLException e) {
}
public static void main(String[] System.out.println("Error: "
args) { } + e.getMessage());
while (true) { } }
System.out.println(" }
AERO-WAY AIRLINE
RESERVATION"); private static void register() { private static void login() {

System.out.println(" System.out.print("Enter System.out.print("Enter


--------------------------------"); username: "); username: ");

System.out.println(" String username = String username =


WHERE EVERY JOURNEY scanner.nextLine(); scanner.nextLine();
TAKES A FLIGHT!");
System.out.print("Enter System.out.print("Enter
System.out.println(" password: "); password: ");
Welcome to Aeroway!!!");
String password = String password =
scanner.nextLine(); scanner.nextLine();
System.out.println("===========
System.out.print("Enter your
==========================
email: ");
===");
try (Connection conn =
String email = DatabaseManager.connect()) {
System.out.println("1.
scanner.nextLine();
Register");
String query = "SELECT *
System.out.print("Are you an FROM users WHERE username = ?
System.out.println("2.
admin? (yes/no): "); AND password = ?";
Login");
boolean isAdmin = PreparedStatement stmt =
System.out.println("3.
scanner.nextLine().equalsIgnoreCas conn.prepareStatement(query);
Exit");
e("yes");
System.out.print("Choose an stmt.setString(1, username);
option: ");
stmt.setString(2, password);
try (Connection conn =
int choice =
DatabaseManager.connect()) { ResultSet rs =
scanner.nextInt();
stmt.executeQuery();
String query = "INSERT
scanner.nextLine(); //
INTO users (username, password,
consume newline
email, isAdmin) VALUES
(?, ?, ?, ?)"; if (rs.next()) {
int userId = System.out.print("Enter date public static void viewFlights() {
rs.getInt("id"); (YYYY-MM-DD): ");
System.out.println("
boolean isAdmin = Date date = AERO-WAY AIRLINE
rs.getBoolean("isAdmin"); Date.valueOf(scanner.nextLine()); RESERVATION");

System.out.print("Enter time System.out.println("


(HH:MM:SS): "); --------------------------------");
User user;
Time time = System.out.println(" WHERE
if (isAdmin) { Time.valueOf(scanner.nextLine()); EVERY JOURNEY TAKES A
FLIGHT!");
user = new System.out.print("Enter flight
Admin(userId, username, cost: "); System.out.println("
password); // Admin constructor Available Flights");
double cost =
} else { scanner.nextDouble();
System.out.println("===========
user = new scanner.nextLine(); // ==========================
RegularUser(userId, username, Consume newline ===");
password); // RegularUser
constructor try (Connection conn =
DatabaseManager.connect()) {
} try (Connection conn =
DatabaseManager.connect()) { String query = "SELECT *
user.handleActions();
FROM flights";
String query = "INSERT
} else { INTO flights (flight_name, Statement stmt =
departure, destination, date, time, conn.createStatement();
cost) VALUES (?, ?, ?, ?, ?, ?)";
System.out.println("Invalid
ResultSet rs =
credentials."); PreparedStatement stmt = stmt.executeQuery(query);
conn.prepareStatement(query);
}
stmt.setString(1,
} catch (SQLException e) {
flightName);
System.out.println("Error: " System.out.println("Available
stmt.setString(2, departure); Flights:");
+ e.getMessage());
stmt.setString(3, while (rs.next()) {
}
destination);
} System.out.println("Flight
stmt.setDate(4, date); ID: " + rs.getInt("flight_id"));
public static void addFlight() {
stmt.setTime(5, time); System.out.println("Flight
System.out.print("Enter flight Name: " +
stmt.setDouble(6, cost); //
name: "); rs.getString("flight_name"));
Set the flight cost
String flightName =
stmt.executeUpdate();
scanner.nextLine(); System.out.println("Departure: " +
System.out.println("Flight rs.getString("departure"));
System.out.print("Enter
added successfully.");
departure: ");
} catch (SQLException e) { System.out.println("Destination: " +
String departure =
rs.getString("destination"));
scanner.nextLine(); System.out.println("Error: "
+ e.getMessage()); System.out.println("Date:
System.out.print("Enter
" + rs.getDate("date"));
destination: "); }
System.out.println("Time:
String destination = } " + rs.getTime("time"));
scanner.nextLine();
System.out.println("Cost: System.out.print("Booking
$" + rs.getDouble("cost")); successful! Your ticket number is: " System.out.println("Payment failed.
+ ticketNumber); Booking not completed.");

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: ");

scanner.nextLine(); // Consume String cvv = System.out.println("Destination: " +


newline scanner.nextLine(); destination);

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.");

} public static void viewBookings(int


public static void userId) {
System.out.println("Cash printBookingReceipt(String
payment of $" + amount + " has flightName, String departure, String
been processed successfully."); System.out.println("===========
==========================
====="); System.out.println("Departure: " +
rs.getString("departure"));
System.out.println(" *
MY BOOKINGS *");
System.out.println("Destination: " +
rs.getString("destination"));
System.out.println("===========
==========================
====="); System.out.println("Date: " +
String.valueOf(rs.getDate("date")));

try (Connection conn = System.out.println("Time: " +


DatabaseManager.connect()) { String.valueOf(rs.getTime("time")))
;
// Adjusted query to select
columns based on the uploaded
image System.out.println("Status: " +
rs.getString("status"));
String query = "SELECT
b.ticket_number, f.flight_name,
f.departure, f.destination, f.date, System.out.println("Booking Date: "
f.time, b.status, b.booking_date " + +
String.valueOf(rs.getDate("booking
"FROM bookings b
_date"))); // Added booking_date
JOIN flights f ON b.flight_id =
f.flight_id WHERE b.user_id = ?";
System.out.println("-------------------
PreparedStatement stmt =
--------------");
conn.prepareStatement(query);
} while (rs.next());
stmt.setInt(1, userId);
}
ResultSet rs =
stmt.executeQuery(); } catch (SQLException e) {

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 {

// If there are bookings,


print them

do {

System.out.println("Ticket Number:
" + rs.getString("ticket_number"));

System.out.println("Flight: " +
rs.getString("flight_name"));

You might also like