-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTheaterSystem.java
More file actions
133 lines (110 loc) · 4.1 KB
/
TheaterSystem.java
File metadata and controls
133 lines (110 loc) · 4.1 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package ArtistTheaterSystem;
import java.util.HashSet;
import java.util.Set;
public class TheaterSystem {
public class NoSeatAvailableException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
public NoSeatAvailableException(String message) {
super(message);
}
}
private HashSet<Integer> purchasedSeats = new HashSet<>();
public class Ticket {
private String date;
private String time;
private int numberOfSeats;
private int[] seatNumbers;
private boolean[] seatAvailability;
public Ticket(String date, String time) {
this.setDate(date);
this.setTime(time);
this.numberOfSeats = 30; // Predetermined
initializeSeats();
}
private void initializeSeats() {
seatNumbers = new int[numberOfSeats];
seatAvailability = new boolean[numberOfSeats];
for (int i = 0; i < numberOfSeats; i++) {
seatNumbers[i] = i + 1; // Seat numbers start from 1
seatAvailability[i] = true; // All seats are available at first
}
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
private Ticket ticket;
public TheaterSystem(String date, String time) {
this.ticket = new Ticket(date, time);
}
public boolean checkseat(int seatNum) {
boolean isSeatAvailable = false;
for (Integer seat : purchasedSeats) {
if (seat == seatNum) {
isSeatAvailable = true;
break; // Found a match, no need to continue checking
}
}
return isSeatAvailable;
}
public boolean isTheaterFull() {
// Check if all seats have been purchased
for (int i = 1; i <= ticket.numberOfSeats; i++) {
if (purchasedSeats.contains(i)) {
System.out.print((i));
return false; // At least one seat is available
}
}
return true; // All seats have been purchased
}
public Set<Integer> getPurchasedSeats() {
return purchasedSeats;
}
public void requestseat(int seatNumber) throws NoSeatAvailableException {
purchasedSeats.add(seatNumber);
}
public boolean isArtistTheaterFull() {
return purchasedSeats.size() >= 30;
}
public void buyTicket(int seatNumber) throws NoSeatAvailableException {
if (seatNumber < 1 || seatNumber > ticket.numberOfSeats) {
throw new NoSeatAvailableException("Invalid Seat Number");
}
int index = seatNumber - 1;
if (!ticket.seatAvailability[index]) {
throw new NoSeatAvailableException("No seats available for the requested ticket! Choose a different show.");
}
ticket.seatAvailability[index] = false;
}
public void returnTicket(int seatNumber) {
if (seatNumber < 1 || seatNumber > ticket.numberOfSeats) {
System.out.println("Invalid Seat Number");
return;
}
purchasedSeats.remove(seatNumber);
int index = seatNumber - 1;
ticket.seatAvailability[index] = true;
System.out.println("Ticket returned for seat number " + seatNumber); //debugging
}
public static void main(String[] args) {
TheaterSystem theater = new TheaterSystem("April 20", "1:00 PM");
try {
theater.buyTicket(1);
theater.buyTicket(1); // This should throw an exception
} catch (NoSeatAvailableException e) {
System.out.println(e.getMessage());
}
}
}