-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOOP-prob-4.java
More file actions
59 lines (54 loc) · 2.02 KB
/
OOP-prob-4.java
File metadata and controls
59 lines (54 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class MovieTicket {
// Private fields for the movie name, seat number, and booking status
private String movieName;
private String seatNumber;
private boolean isBooked;
// Constructor to initialize the movie name and seat number, booking status is set to false by default
public MovieTicket(String movieName, String seatNumber)
{
this.movieName = movieName;
this.seatNumber = seatNumber;
this.isBooked = false; // Default status is not booked
}
// Method to book the ticket if it's not already booked
public void bookTicket()
{
if (!isBooked)
{
isBooked = true;
System.out.println("Ticket booked for " + movieName + " at seat " + seatNumber + ".");
} else
{
System.out.println("Seat " + seatNumber + " is already booked.");
}
}
// Method to cancel the ticket if it is currently booked
public void cancelTicket()
{
if (isBooked)
{
isBooked = false;
System.out.println("Booking canceled for seat " + seatNumber + ".");
} else
{
System.out.println("Seat " + seatNumber + " is not booked yet.");
}
}
// Method to display ticket information and current booking status
public void displayTicketInfo()
{
System.out.println("Movie: " + movieName);
System.out.println("Seat Number: " + seatNumber);
System.out.println("Booking Status: " + (isBooked ? "Booked" : "Available"));
}
// Main method to test the MovieTicket class
public static void main(String[] args)
{
MovieTicket ticket1 = new MovieTicket("Avengers: Endgame", "A10"); // Create a new movie ticket
ticket1.displayTicketInfo(); // Before booking
ticket1.bookTicket(); // Book the ticket
ticket1.displayTicketInfo(); // After booking
ticket1.cancelTicket(); // Cancel the ticket
ticket1.displayTicketInfo(); // After cancellation
}
}