Assign Airline Seats to Passengers using Java


 

Assign Airline Seats to Passengers using Java

Here we are going to create an application through which we can assign seats to passengers in an airplane.

Here we are going to create an application through which we can assign seats to passengers in an airplane.

Assign Airline Seats to Passengers using Java

Here we are going to create an application through which we can assign seats to passengers in an airplane. For this purpose, we have created setter and getter methods and stored the some seat numbers into the ArrayList. Then we have allowed the user to enter seat of their choice to reserve. If the seat is available, your seat will get reserved and 'X' mark will get displayed on the assigned seat. In case if the seat is already assigned or not available, a message will get displayed and user can enter another seat no. This will continue until all seats are filled or until the user signals that the program should end.

Here is the code:

import java.util.*;

class Passenger {
	public int no;
	public String seatA;
	public String seatB;
	public String seatC;
	public String seatD;

	public Passenger() {
	}

	public Passenger(int no, String seatA, String seatB, String seatC,
			String seatD) {
		this.no = no;
		this.seatA = seatA;
		this.seatB = seatB;
		this.seatC = seatC;
		this.seatD = seatD;
	}

	public int getNo() {
		return no;
	}

	public void setNo(int no) {
		this.no = no;
	}

	public String getSeatA() {
		return seatA;
	}

	public void setSeatA(String seatA) {
		this.seatA = seatA;
	}

	public String getSeatB() {
		return seatB;
	}

	public void setSeatB(String seatB) {
		this.seatB = seatB;
	}

	public String getSeatC() {
		return seatC;
	}

	public void setSeatC(String seatC) {
		this.seatC = seatC;
	}

	public String getSeatD() {
		return seatD;
	}

	public void setSeatD(String seatD) {
		this.seatD = seatD;
	}
}

public class AssignPassenger {
	public static void main(String[] args) throws Exception {
		List list = new ArrayList();
		list.add(new Passenger(1, "A", "B", "C", "D"));
		list.add(new Passenger(2, "A", "B", "C", "D"));
		list.add(new Passenger(3, "A", "B", "C", "D"));
		list.add(new Passenger(4, "A", "B", "C", "D"));
		list.add(new Passenger(5, "A", "B", "C", "D"));
		list.add(new Passenger(6, "A", "B", "C", "D"));
		list.add(new Passenger(7, "A", "B", "C", "D"));
		for (Passenger s : list) {
			System.out.println(s.getNo() + "  " + s.getSeatA() + "  "
					+ s.getSeatB() + "  " + s.getSeatC() + "  " + s.getSeatD());
		}
		for (int i = 1; i <= 5; i++) {
		System.out.print("Enter seat no: ");
		Scanner input = new Scanner(System.in);
		int num = input.nextInt();
		String sn = input.next();
		for (Passenger s : list) {
		if (num == s.getNo()) {
		if (sn.equals(s.getSeatA())) {
		s.setSeatA("X");
		System.out.println("Seat "+num+""+sn+" is Reserved");				
	        } else if (sn.equals(s.getSeatB())) {
		s.setSeatB("X");
		System.out.println("Seat "+num+""+sn+" is Reserved");	
		} else if (sn.equals(s.getSeatC())) {
		s.setSeatC("X");
		System.out.println("Seat "+num+""+sn+" is Reserved");	
		} else if (sn.equals(s.getSeatD())) {
		s.setSeatD("X");
		System.out.println("Seat "+num+""+sn+" is Reserved");	
		} else {
		System.out.println("Not Available");
		}
		}
		System.out.println(s.getNo()+"  "+s.getSeatA()+"  "
		+s.getSeatB()+ "  "+s.getSeatC()+"  "+ s.getSeatD());
		}
		}
	}
}

Output:

1  A  B  C  D
2  A  B  C  D
3  A  B  C  D
4  A  B  C  D
5  A  B  C  D
6  A  B  C  D
7  A  B  C  D
Enter seat no: 1 A
Seat 1A is reserved
1  X  B  C  D
2  A  B  C  D
3  A  B  C  D
4  A  B  C  D
5  A  B  C  D
6  A  B  C  D
7  A  B  C  D

Ads