diff --git a/lessons/src/main/java/lessons/lesson06/App.java b/lessons/src/main/java/lessons/lesson06/App.java new file mode 100644 index 0000000..4c6d85c --- /dev/null +++ b/lessons/src/main/java/lessons/lesson06/App.java @@ -0,0 +1,64 @@ +package Lesson6; + +import lombok.Cleanup; + +import java.util.Scanner; + +/** + * author Shohjahon + * version 1.1 + */ + +public class App { + public static void main( String[] args ) { + SeatManager manager = new SeatManager(); + @Cleanup Scanner scanner = new Scanner( System.in ); + int choice; + + do { + System.out.println( "\n=== Airline Booking System ===" ); + System.out.println( "1. View all seats" ); + System.out.println( "2. Reserve a seat" ); + System.out.println( "3. Pay for a seat" ); + System.out.println( "4. Cancel reservation" ); + System.out.println( "5. View reservation by seat number" ); + System.out.println( "6. Exit" ); + System.out.print( "Enter your choice: " ); + + try { + choice = Integer.parseInt( scanner.nextLine() ); + } catch ( NumberFormatException e ) { + System.out.println( "Invalid input! Enter a number 1–6." ); + choice = 0; + } + + switch ( choice ) { + case 1 -> manager.viewAllSeats(); + case 2 -> { + System.out.print( "Enter seat number (e.g., S5): " ); + String seatNumber = scanner.nextLine().trim().toUpperCase(); + System.out.print( "Enter passenger name: " ); + String name = scanner.nextLine().trim(); + manager.reserveSeat( seatNumber, name ); + } + case 3 -> { + System.out.print( "Enter seat number to pay: " ); + String seatNumber = scanner.nextLine().trim().toUpperCase(); + manager.payForSeat( seatNumber ); + } + case 4 -> { + System.out.print( "Enter seat number to cancel: " ); + String seatNumber = scanner.nextLine().trim().toUpperCase(); + manager.cancelReservation( seatNumber ); + } + case 5 -> { + System.out.print( "Enter seat number to view: " ); + String seatNumber = scanner.nextLine().trim().toUpperCase(); + manager.viewReservation( seatNumber ); + } + case 6 -> System.out.println( "Goodbye!" ); + default -> System.out.println( "Invalid option. Try again." ); + } + } while ( choice != 6 ); + } +} diff --git a/lessons/src/main/java/lessons/lesson06/Seat.java b/lessons/src/main/java/lessons/lesson06/Seat.java new file mode 100644 index 0000000..d1927f6 --- /dev/null +++ b/lessons/src/main/java/lessons/lesson06/Seat.java @@ -0,0 +1,65 @@ +package Lesson6; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serial; +import java.io.Serializable; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class Seat implements Serializable { + @Serial + private static final long serialVersionUID = 1L; + + private String seatNumber; + private String seatClass; + private SeatStatus status; + private String passengerName; + private LocalDateTime bookingTime; + private LocalDateTime flightDeparture; + + public void reserve( String name ) { + this.passengerName = name; + this.status = SeatStatus.BOOKED; + this.bookingTime = LocalDateTime.now(); + } + + public void pay() { + this.status = SeatStatus.PAID; + } + + public void cancel() { + this.passengerName = ""; + this.status = SeatStatus.AVAILABLE; + this.bookingTime = null; + } + + public boolean isExpired() { + if ( status == SeatStatus.BOOKED && bookingTime != null ) { + return bookingTime.plusMinutes( 24 ).isBefore( LocalDateTime.now() ); + } + return false; + } + + @Override + public String toString() { + String bookingInfo = (bookingTime != null) + ? bookingTime.format( DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm" ) ) + : "-"; + String flightInfo = (flightDeparture != null) + ? flightDeparture.format( DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm" ) ) + : "-"; + return String.format( "%-5s | %-10s | %-9s | %-15s | %-16s | %-16s", + seatNumber, + seatClass, + status, + passengerName.isEmpty() ? "-" : passengerName, + bookingInfo, + flightInfo ); + } +} diff --git a/lessons/src/main/java/lessons/lesson06/SeatManager.java b/lessons/src/main/java/lessons/lesson06/SeatManager.java new file mode 100644 index 0000000..b41bd09 --- /dev/null +++ b/lessons/src/main/java/lessons/lesson06/SeatManager.java @@ -0,0 +1,142 @@ +package Lesson6; + +import java.io.*; +import java.time.LocalDateTime; +import java.util.*; + +public class SeatManager { + private static final String DATA_FILE = "seats.dat"; + private ArrayList< Seat > seats; + + public SeatManager() { + seats = new ArrayList<>(); + loadSeats(); + } + + private void initializeSeats() { + for ( int i = 1; i <= 30; i++ ) { + String seatClass = (i <= 10) ? "First" : (i <= 20) ? "Business" : "Economy"; + LocalDateTime flightDate = LocalDateTime.now().plusDays( 1 ); + seats.add( new Seat( "S" + i, seatClass, SeatStatus.AVAILABLE, "", null, flightDate ) ); + } + } + + private void resetExpiredBookings() { + for ( Seat seat : seats ) { + if ( seat.isExpired() ) { + seat.cancel(); + } + } + } + + public void viewAllSeats() { + resetExpiredBookings(); + System.out.println( "\nSeat | Class | Status | Passenger | Booked Time | Flight Time" ); + System.out.println( "-------------------------------------------------------------------------------" ); + for ( Seat seat : seats ) { + System.out.println( seat ); + } + } + + public void reserveSeat( String seatNumber, String name ) { + Seat seat = findSeat( seatNumber ); + if ( seat == null ) { + System.out.println( "Seat not found!" ); + return; + } + if ( seat.getStatus() == SeatStatus.PAID ) { + System.out.println( "Seat already paid for by " + seat.getPassengerName() ); + return; + } + if ( seat.getStatus() == SeatStatus.BOOKED && !seat.isExpired() ) { + System.out.println( "Seat already booked by " + seat.getPassengerName() ); + return; + } + + seat.reserve( name ); + saveSeats(); + System.out.println( "Seat " + seatNumber + " successfully reserved for " + name + "." ); + } + + public void payForSeat( String seatNumber ) { + Seat seat = findSeat( seatNumber ); + if ( seat == null ) { + System.out.println( "Seat not found!" ); + return; + } + if ( seat.getStatus() != SeatStatus.BOOKED ) { + System.out.println( "Seat must be booked first before payment." ); + return; + } + seat.pay(); + saveSeats(); + System.out.println( "Seat " + seatNumber + " successfully paid for." ); + } + + public void cancelReservation( String seatNumber ) { + Seat seat = findSeat( seatNumber ); + if ( seat == null ) { + System.out.println( "Seat not found!" ); + return; + } + if ( seat.getStatus() == SeatStatus.AVAILABLE ) { + System.out.println( "Seat is not currently booked." ); + return; + } + seat.cancel(); + saveSeats(); + System.out.println( "Reservation for seat " + seatNumber + " canceled." ); + } + + public void viewReservation( String seatNumber ) { + resetExpiredBookings(); + Seat seat = findSeat( seatNumber ); + if ( seat == null ) { + System.out.println( "Seat not found!" ); + return; + } + + System.out.println( "\n--- Seat Details ---" ); + System.out.println( "Seat Number : " + seat.getSeatNumber() ); + System.out.println( "Class : " + seat.getSeatClass() ); + System.out.println( "Status : " + seat.getStatus() ); + System.out.println( "Passenger : " + (seat.getPassengerName().isEmpty() ? "-" : seat.getPassengerName()) ); + System.out.println( "Booking Time: " + (seat.getBookingTime() != null ? seat.getBookingTime() : "-") ); + System.out.println( "Flight Time : " + seat.getFlightDeparture() ); + } + + private Seat findSeat( String seatNumber ) { + for ( Seat seat : seats ) { + if ( seat.getSeatNumber().equalsIgnoreCase( seatNumber ) ) { + return seat; + } + } + return null; + } + + @SuppressWarnings( "unchecked" ) + private void loadSeats() { + File file = new File( DATA_FILE ); + if ( !file.exists() ) { + initializeSeats(); + saveSeats(); + return; + } + + try ( ObjectInputStream ois = new ObjectInputStream( new FileInputStream( file ) ) ) { + seats = ( ArrayList< Seat > ) ois.readObject(); + System.out.println( "Previous seat data loaded successfully." ); + } catch ( Exception e ) { + System.out.println( "Error loading saved data. Reinitializing seats..." ); + initializeSeats(); + } + } + + private void saveSeats() { + try ( ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream( DATA_FILE ) ) ) { + oos.writeObject( seats ); + } catch ( IOException e ) { + System.out.println( "Error saving seat data: " + e.getMessage() ); + } + } +} diff --git a/lessons/src/main/java/lessons/lesson06/SeatStatus.java b/lessons/src/main/java/lessons/lesson06/SeatStatus.java new file mode 100644 index 0000000..0380ff0 --- /dev/null +++ b/lessons/src/main/java/lessons/lesson06/SeatStatus.java @@ -0,0 +1,7 @@ +package Lesson6; + +public enum SeatStatus { + AVAILABLE, + BOOKED, + PAID +}