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

Admin Cindy

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

Admin Cindy

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

import java.sql.

Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.util.Scanner;

// Abstract base class for common functionality


abstract class User {
protected int userId;
protected String username;
protected String password;

public User(int userId, String username, String password) {


this.userId = userId;
this.username = username;
this.password = password;
}

public abstract void handleActions();


}

// Admin class
class Admin extends User {
public Admin(int userId, String username, String password) {
super(userId, username, password); // Call the parent constructor
}

@Override
public void handleActions() {
while (true) {
System.out.println("Welcome, Admin!");
System.out.println("1. Add a new flight");
System.out.println("2. View all flights");
System.out.println("3. Log out");
System.out.print("Choose an option: ");
int choice = AerowayBookingSystem.scanner.nextInt();
AerowayBookingSystem.scanner.nextLine(); // consume newline

switch (choice) {
case 1 -> AerowayBookingSystem.addFlight();
case 2 -> AerowayBookingSystem.viewFlights();
case 3 -> {
System.out.println("Logging out...");
return;
}
default -> System.out.println("Invalid choice.");
}
}
}
}

class RegularUser extends User {


public RegularUser(int userId, String username, String password) {
super(userId, username, password); // Call the parent constructor with the
correct parameters
}
@Override
public void handleActions() {
while (true) {
System.out.println("Welcome, User!");
System.out.println("1. View Flights");
System.out.println("2. Book a Flight");
System.out.println("3. View My Bookings");
System.out.println("4. Log out");
System.out.print("Choose an option: ");
int choice = AerowayBookingSystem.scanner.nextInt();
AerowayBookingSystem.scanner.nextLine(); // consume newline

switch (choice) {
case 1 -> AerowayBookingSystem.viewFlights();
case 2 -> AerowayBookingSystem.bookFlight(userId);
case 3 -> AerowayBookingSystem.viewBookings(userId);
case 4 -> {
System.out.println("Logging out...");
return;
}
default -> System.out.println("Invalid choice.");
}
}
}
}

// Main class implementing the booking system


// AerowayBookingSystem.java
public class AerowayBookingSystem {
public static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {


while (true) {
System.out.println(" AERO-WAY AIRLINE RESERVATION");
System.out.println(" --------------------------------");
System.out.println(" WHERE EVERY JOURNEY TAKES A FLIGHT!");
System.out.println(" Welcome to Aeroway!!!");
System.out.println("========================================");
System.out.println("1. Register");
System.out.println("2. Login");
System.out.println("3. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // consume newline

switch (choice) {
case 1 -> register();
case 2 -> login();
case 3 -> {
System.out.println("Exiting system...");
System.exit(0);
}
default -> System.out.println("Invalid choice. Please try again.");
}
}
}

private static void register() {


System.out.print("Enter username: ");
String username = scanner.nextLine();
System.out.print("Enter password: ");
String password = scanner.nextLine();
System.out.print("Are you an admin? (yes/no): ");
boolean isAdmin = scanner.nextLine().equalsIgnoreCase("yes");

try (Connection conn = DatabaseManager.connect()) {


String query = "INSERT INTO users (username, password, isAdmin) VALUES
(?, ?, ?)";
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1, username);
stmt.setString(2, password);
stmt.setBoolean(3, isAdmin);
stmt.executeUpdate();
System.out.println("Registration successful.");
} catch (SQLException e) {
System.out.println("Error: " + e.getMessage());
}
}

private static void login() {


System.out.print("Enter username: ");
String username = scanner.nextLine();
System.out.print("Enter password: ");
String password = scanner.nextLine();

try (Connection conn = DatabaseManager.connect()) {


String query = "SELECT * FROM users WHERE username = ? AND password
= ?";
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1, username);
stmt.setString(2, password);
ResultSet rs = stmt.executeQuery();

if (rs.next()) {
int userId = rs.getInt("id");
boolean isAdmin = rs.getBoolean("isAdmin");

User user;
if (isAdmin) {
user = new Admin(userId, username, password); // Admin
constructor
} else {
user = new RegularUser(userId, username, password); //
RegularUser constructor
}
user.handleActions();
} else {
System.out.println("Invalid credentials.");
}
} catch (SQLException e) {
System.out.println("Error: " + e.getMessage());
}
}

// Admin-specific and regular user methods remain unchanged


public static void addFlight() {
System.out.print("Enter flight name: ");
String flightName = scanner.nextLine();
System.out.print("Enter departure: ");
String departure = scanner.nextLine();
System.out.print("Enter destination: ");
String destination = scanner.nextLine();
System.out.print("Enter date (YYYY-MM-DD): ");
Date date = Date.valueOf(scanner.nextLine());
System.out.print("Enter time (HH:MM:SS): ");
Time time = Time.valueOf(scanner.nextLine());
System.out.print("Enter status (Scheduled, Delayed, Cancelled, Completed):
");
String status = scanner.nextLine();
System.out.print("Enter flight cost: ");
double cost = scanner.nextDouble();
scanner.nextLine(); // Consume newline

try (Connection conn = DatabaseManager.connect()) {


String query = "INSERT INTO flights (flight_name, departure,
destination, date, time, status, cost) VALUES (?, ?, ?, ?, ?, ?, ?)";
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1, flightName);
stmt.setString(2, departure);
stmt.setString(3, destination);
stmt.setDate(4, date);
stmt.setTime(5, time);
stmt.setString(6, status);
stmt.setDouble(7, cost); // Set the flight cost
stmt.executeUpdate();
System.out.println("Flight added successfully.");
} catch (SQLException e) {
System.out.println("Error: " + e.getMessage());
}
}

public static void viewFlights() {


System.out.println(" AERO-WAY AIRLINE RESERVATION");
System.out.println(" --------------------------------");
System.out.println(" WHERE EVERY JOURNEY TAKES A FLIGHT!");
System.out.println(" ~~Available Flights~~");
System.out.println("========================================");
try (Connection conn = DatabaseManager.connect()) {
String query = "SELECT * FROM flights";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);

System.out.println("Available Flights:");
while (rs.next()) {
System.out.println("Flight ID: " + rs.getInt("flight_id"));
System.out.println("Flight Name: " + rs.getString("flight_name"));
System.out.println("Departure: " + rs.getString("departure"));
System.out.println("Destination: " + rs.getString("destination"));
System.out.println("Date: " + rs.getDate("date"));
System.out.println("Time: " + rs.getTime("time"));
System.out.println("Status: " + rs.getString("status"));
System.out.println("Cost: $" + rs.getDouble("cost"));
System.out.println("---------------------------------");
}
} catch (SQLException e) {
System.out.println("Error: " + e.getMessage());
}
}

public static void bookFlight(int userId) {


System.out.print("Enter the Flight ID you want to book: ");
String flightIdInput = scanner.nextLine(); // Accept input as string

// Validate if the input is an integer


int flightId;
try {
flightId = Integer.parseInt(flightIdInput); // Try parsing the input to
an integer
} catch (NumberFormatException e) {
System.out.println("Invalid Flight ID. Please enter a numeric value.");
return; // Exit the method
}

// Generate a unique ticket number


String ticketNumber = "TKT" + System.currentTimeMillis() + "_" + userId;

try (Connection conn = DatabaseManager.connect()) {


// Retrieve the flight cost before booking
String flightQuery = "SELECT cost, flight_name, departure, destination
FROM flights WHERE flight_id = ?";
PreparedStatement flightStmt = conn.prepareStatement(flightQuery);
flightStmt.setInt(1, flightId);
ResultSet flightRs = flightStmt.executeQuery();

if (flightRs.next()) {
double flightCost = flightRs.getDouble("cost");
String flightName = flightRs.getString("flight_name");
String departure = flightRs.getString("departure");
String destination = flightRs.getString("destination");

// Book the flight


String query = "INSERT INTO bookings (user_id, flight_id,
ticket_number) VALUES (?, ?, ?)";
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setInt(1, userId);
stmt.setInt(2, flightId);
stmt.setString(3, ticketNumber);
stmt.executeUpdate();
System.out.println("Booking successful!");
printBookingReceipt(flightName, departure, destination, flightCost,
ticketNumber);
} else {
System.out.println("Flight not found.");
}
} catch (SQLException e) {
System.out.println("Error: " + e.getMessage());
}
}

// ASCII Art Box for booking receipt


public static void printBookingReceipt(String flightName, String departure,
String destination, double cost, String ticketNumber) {
System.out.println("==========================================");
System.out.println(" *** BOOKING RECEIPT ***");
System.out.println("==========================================");
System.out.println("Ticket Number: " + ticketNumber);
System.out.println("Flight: " + flightName);
System.out.println("Departure: " + departure);
System.out.println("Destination: " + destination);
System.out.println("Cost: $" + cost);
System.out.println("==========================================");
System.out.println(" Thank you for booking with us!");
System.out.println("==========================================");
}

public static void viewBookings(int userId) {


System.out.println("==========================================");
System.out.println(" *** MY BOOKINGS ***");
System.out.println("==========================================");
try (Connection conn = DatabaseManager.connect()) {
String query = "SELECT b.ticket_number, f.flight_name, f.departure,
f.destination, f.date, f.time, f.status, f.cost " +
"FROM bookings b " +
"JOIN flights f ON b.flight_id = f.flight_id " +
"WHERE b.user_id = ?";
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setInt(1, userId);
ResultSet rs = stmt.executeQuery();

while (rs.next()) {
System.out.println("Ticket Number: " +
rs.getString("ticket_number"));
System.out.println("Flight: " + rs.getString("flight_name"));
System.out.println("Departure: " + rs.getString("departure"));
System.out.println("Destination: " + rs.getString("destination"));
System.out.println("Date: " + rs.getDate("date"));
System.out.println("Time: " + rs.getTime("time"));
System.out.println("Status: " + rs.getString("status"));
System.out.println("Cost: $" + rs.getDouble("cost"));
System.out.println("---------------------------------");
}
} catch (SQLException e) {
System.out.println("Error: " + e.getMessage());
}
}
}

You might also like