0% found this document useful (0 votes)
30 views2 pages

Advanced Programming: Lab 7 Hands On

This Java program defines a Flight class with methods to book tickets, return tickets, and check for valid ticket IDs. The Flight class tracks the number of seats and valid ticket IDs. The main method creates a flight with 5 seats and 5 ticket IDs, books 3 tickets, tries returning an invalid ticket which throws an error, then books the remaining 2 tickets to test the seat counting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views2 pages

Advanced Programming: Lab 7 Hands On

This Java program defines a Flight class with methods to book tickets, return tickets, and check for valid ticket IDs. The Flight class tracks the number of seats and valid ticket IDs. The main method creates a flight with 5 seats and 5 ticket IDs, books 3 tickets, tries returning an invalid ticket which throws an error, then books the remaining 2 tickets to test the seat counting.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

ADVANCED PROGRAMMING

Lab 7 Hands On

Name: Hossam ElDin Mohamed


ID:18P4607
Package Assignment7;

import java.util.Random;

public class flight {


private int NumSeats;
private String ValidTicketIDs[];
Random r = new Random();
public flight (int NumSeats,String ValidTicketIDs[]) {
this.NumSeats=NumSeats;
this.ValidTicketIDs= ValidTicketIDs;
}
public String BookTicket () {
try {
NumSeats--;
}
catch (Exception e) {
System.out.println(e.getMessage());
}
return ValidTicketIDs[r.nextInt(5)];
}
void ReturnTicket(String TicketID) throws Exception {
try {
for (int i=0;i<ValidTicketIDs.length;i++) {
if (TicketID.equals(ValidTicketIDs[i])) {
NumSeats++;
}
else {
throw new Exception("Error Ticket Is InValid");
}
}
}
catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
String Tickets[]={"A","B","C","D","E"};
flight flight1=new flight(5,Tickets);
System.out.println(flight1.BookTicket());
System.out.println(flight1.BookTicket());
System.out.println(flight1.BookTicket());
flight1.ReturnTicket("z");
System.out.println(flight1.BookTicket());
System.out.println(flight1.BookTicket());
System.out.println(flight1.BookTicket());
}
}

You might also like