Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions lessons/src/main/java/lessons/lesson07/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package Lesson7;

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 );
}
}
65 changes: 65 additions & 0 deletions lessons/src/main/java/lessons/lesson07/Seat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package Lesson7;

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 );
}
}
142 changes: 142 additions & 0 deletions lessons/src/main/java/lessons/lesson07/SeatManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package Lesson7;

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() );
}
}
}
7 changes: 7 additions & 0 deletions lessons/src/main/java/lessons/lesson07/SeatStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package Lesson7;

public enum SeatStatus {
AVAILABLE,
BOOKED,
PAID
}