-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmovieTicket.java
More file actions
99 lines (82 loc) · 3.59 KB
/
movieTicket.java
File metadata and controls
99 lines (82 loc) · 3.59 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
package movieTicket;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class movieTicket extends JFrame implements ActionListener {
private List<Queue<String>> ticketQueues;
private int currentQueueIndex;
private JButton bookButton;
private JButton processButton;
private JButton showBookedButton;
public movieTicket(int numQueues, int numCustomers) {
ticketQueues = new ArrayList<>();
currentQueueIndex = 0;
initializeQueues(numQueues);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Movie Ticket Booking System");
setSize(300, 200);
setLocationRelativeTo(null);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(3, 1));
bookButton = new JButton("Book a Ticket");
bookButton.addActionListener(this);
processButton = new JButton("Process Next Ticket");
processButton.addActionListener(this);
showBookedButton = new JButton("Show Booked Tickets");
showBookedButton.addActionListener(this);
mainPanel.add(bookButton);
mainPanel.add(processButton);
mainPanel.add(showBookedButton);
add(mainPanel);
}
private void initializeQueues(int numQueues) {
for (int i = 0; i < numQueues; i++) {
ticketQueues.add(new LinkedList<>());
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == bookButton) {
String customerName = JOptionPane.showInputDialog(this, "Enter the name of the customer:");
Queue<String> currentQueue = ticketQueues.get(currentQueueIndex);
currentQueue.offer(customerName);
JOptionPane.showMessageDialog(this, "Ticket booked successfully for " + customerName);
currentQueueIndex++;
if (currentQueueIndex >= ticketQueues.size()) {
currentQueueIndex = 0;
}
} else if (e.getSource() == processButton) {
Queue<String> currentQueue = ticketQueues.get(currentQueueIndex);
if (currentQueue.isEmpty()) {
currentQueueIndex++;
if (currentQueueIndex >= ticketQueues.size()) {
currentQueueIndex = 0;
}
currentQueue = ticketQueues.get(currentQueueIndex);
}
String customerName = currentQueue.poll();
JOptionPane.showMessageDialog(this, "Ticket processed for " + customerName);
} else if (e.getSource() == showBookedButton) {
StringBuilder bookedTickets = new StringBuilder();
for (Queue<String> queue : ticketQueues) {
for (String customer : queue) {
bookedTickets.append(customer).append("\n");
}
}
JOptionPane.showMessageDialog(this, "Booked Tickets:\n" + bookedTickets.toString());
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
int numQueues = Integer.parseInt(JOptionPane.showInputDialog("Enter the number of ticket queues:"));
int numCustomers = Integer.parseInt(JOptionPane.showInputDialog("Enter the number of customers per queue:"));
movieTicket gui = new movieTicket(numQueues, numCustomers);
gui.setVisible(true);
});
}
}