import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class BankGUI extends JFrame implements ActionListener {
??private String[] NAME = new String[100]; // Resource names
??private int[][] Max = new int[100][100]; // Maximum demand matrix
??private int[][] Allocation = new int[100][100]; // Allocated resource matrix
??private int[][] Need = new int[100][100]; // Remaining resource matrix
??private int[] Available = new int[100]; // Available resource matrix
??private int[] Request = new int[100]; // Request resource vector
??private int[] Work = new int[100]; // Stores available resources in the system
??private int[] Finish = new int[100]; // Marks whether the system has enough resources to allocate to each process
??private int[] Security = new int[100]; // Stores the safety sequence
??private int M = 100; // Maximum number of processes
??private int N = 100; // Maximum number of resources
??private int currentProcess; // Current process number
??private JTextField[] txtAvailable;
??private JTextField[] txtMax;
??private JTextField[] txtAllocation;
??private JTextField[] txtNeed;
??private JTextField[] txtRequest;
??private JButton btnRequest;
??private JTextArea txtOutput;
??public BankGUI() {
????setTitle("Banker's Algorithm");
????setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
????setSize(800, 600);
????setLocationRelativeTo(null);
????JPanel panel = new JPanel();
????panel.setLayout(new GridBagLayout());
????GridBagConstraints constraints = new GridBagConstraints();
????constraints.insets = new Insets(5, 5, 5, 5);
????// Available resources panel
????JPanel pnlAvailable = new JPanel();
????pnlAvailable.setBorder(BorderFactory.createTitledBorder("Available Resources"));
????pnlAvailable.setLayout(new GridLayout(1, N));
????txtAvailable = new JTextField[N];
????for (int i = 0; i < N; i++) {
??????txtAvailable[i] = new JTextField(3);
??????pnlAvailable.add(txtAvailable[i]);
????}
????constraints.gridx = 0;
????constraints.gridy = 0;
????constraints.gridwidth = 2;
????panel.add(pnlAvailable, constraints);
????// Process details panel
????JPanel pnlProcess = new JPanel();
????pnlProcess.setLayout(new GridBagLayout());
????pnlProcess.setBorder(BorderFactory.createTitledBorder("Process Details"));
????JPanel pnlLabels = new JPanel(new GridLayout(1, N));
????for (int i = 0; i < N; i++) {
??????JLabel lblResource = new JLabel("Resource " + i);
??????pnlLabels.add(lblResource);
????}
????JPanel pnlMax = new JPanel(new GridLayout(M, N));
????txtMax = new JTextField[M * N];
????for (int i = 0; i < M * N; i++) {
??????txtMax[i] = new JTextField(3);
??????pnlMax.add(txtMax[i]);
????}
????JPanel pnlAllocation = new JPanel(new GridLayout(M, N));
????txtAllocation = new JTextField[M * N];
????for (int i = 0; i < M * N; i++) {
??????txtAllocation[i] = new JTextField(3);
??????pnlAllocation.add(txtAllocation[i]);
????}
????JPanel pnlNeed = new JPanel(new GridLayout(M, N));
????txtNeed = new JTextField[M * N];
????for (int i = 0; i < M * N; i++) {
??????txtNeed[i] = new JTextField(3);
??????pnlNeed.add(txtNeed[i]);
????}
????constraints.gridx = 0;
????constraints.gridy = 0;
????constraints.gridwidth = 1;
????pnlProcess.add(pnlLabels, constraints);
????constraints.gridx = 1;
????constraints.gridy = 0;
????pnlProcess.add(pnlMax, constraints);
????constraints.gridx = 2;
????constraints.gridy = 0;
????pnlProcess.add(pnlAllocation, constraints);
????constraints.gridx = 3;
????constraints.gridy = 0;
????pnlProcess.add(pnlNeed, constraints);
????constraints.gridx = 0;
????constraints.gridy = 1;
????constraints.gridwidth = 4;
????panel.add(pnlProcess, constraints);
????// Resource request panel
????JPanel pnlRequest = new JPanel();
????pnlRequest.setBorder(BorderFactory.createTitledBorder("Resource Request"));
????pnlRequest.setLayout(new GridBagLayout());
????GridBagConstraints gbc = new GridBagConstraints();
????gbc.insets = new Insets(5, 5, 5, 5);
????JLabel lblProcess = new JLabel("Process Number:");
????gbc.gridx = 0;
????gbc.gridy = 0;
????pnlRequest.add(lblProcess, gbc);
????JComboBox cmbProcess = new JComboBox<>();
????for (int i = 0; i < M; i++) {
??????cmbProcess.addItem("P" + i);
????}
????cmbProcess.addActionListener(e -> currentProcess = cmbProcess.getSelectedIndex());
????gbc.gridx = 1;
????gbc.gridy = 0;
????pnlRequest.add(cmbProcess, gbc);
????JPanel pnlResource = new JPanel(new GridLayout(1, N));
????for (int i = 0; i < N; i++) {
??????JTextField txtResource = new JTextField(3);
??????txtRequest[i] = txtResource;
??????pnlResource.add(txtResource);
????}
????gbc.gridx = 0;
????gbc.gridy = 1;
????gbc.gridwidth = 2;
????pnlRequest.add(pnlResource, gbc);
????btnRequest = new JButton("Request");
????btnRequest.addActionListener(this);
????gbc.gridx = 0;
????gbc.gridy = 2;
????gbc.gridwidth = 2;
????pnlRequest.add(btnRequest, gbc);
????constraints.gridx = 0;
????constraints.gridy = 2;
????constraints.gridwidth = 2;
????panel.add(pnlRequest, constraints);
????// Output area
????txtOutput = new JTextArea(10, 60);
????txtOutput.setEditable(false);
????JScrollPane scrollPane = new JScrollPane(txtOutput);
????constraints.gridx = 0;
????constraints.gridy = 3;
????constraints.gridwidth = 4;
????panel.add(scrollPane, constraints);
????add(panel);
??}
??public void init() {
????int i, j, m, n;
????int number;
????boolean flag;
????String name;
????int[] temp = new int[100];
????// Input system resources and their initial quantities
????n = N;
????for (i = 0; i < n; i++) {
??????name = JOptionPane.showInputDialog("Resource " + i + " Name:");
??????NAME[i] = name;
??????number = Integer.parseInt(JOptionPane.showInputDialog("Resource " + name + " Initial Quantity:"));
??????Available[i] = number;
????}
????// Input number of processes and the maximum demand matrix
????m = M;
????System.out.println("Enter the maximum demand matrix [Max]:");
????do {
??????flag = false;
??????for (i = 0; i < M; i++) {
????????for (j = 0; j < N; j++) {
??????????Max[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Max[" + i + "][" + j + "]:"));
??????????if (Max[i][j] > Available[j]) {
????????????flag = true;
??????????}
????????}
??????}
??????if (flag) {
????????JOptionPane.showMessageDialog(null, "The maximum demand exceeds the maximum quantity of resources in the system. Please enter again!");
??????}
????} while (flag);
????// Input the allocated resources for each process and calculate the remaining needed resources
????do {
??????flag = false;
??????for (i = 0; i < M; i++) {
????????for (j = 0; j < N; j++) {
??????????Allocation[i][j] = Integer.parseInt(JOptionPane.showInputDialog("Allocation[" + i + "][" + j + "]:"));
??????????if (Allocation[i][j] > Max[i][j]) {
????????????flag = true;
??????????}
??????????Need[i][j] = Max[i][j] - Allocation[i][j];
??????????temp[j] += Allocation[i][j];
????????}
??????}
??????if (flag) {
????????JOptionPane.showMessageDialog(null, "The allocated resources exceed the maximum quantity. Please enter again!");
??????}
????} while (flag);
????// Calculate the available resources in the system
????for (j = 0; j < N; j++) {
??????Available[j] = Available[j] - temp[j];
????}
??}
??public void showData() {
????txtOutput.append("System Available Resources [Available]:\n");
????for (int i = 0; i < N; i++) {
??????txtOutput.append(NAME[i] + "?");
????}
????txtOutput.append("\n");
????for (int j = 0; j < N; j++) {
??????txtOutput.append(Available[j] + "?");
????}
????txtOutput.append("\n");
????txtOutput.append("Current Resource Allocation:\n");
????txtOutput.append("?????Max?? ??Allocation???Need\n");
????txtOutput.append("Process???");
????for (int j = 0; j < 3; j++) {
??????for (int i = 0; i < N; i++) {
????????txtOutput.append(NAME[i] + "?");
??????}
??????txtOutput.append("???");
????}
????txtOutput.append("\n");
????for (int i = 0; i < M; i++) {
??????txtOutput.append("P" + i + "??");
??????for (int j = 0; j < N; j++) {
????????txtOutput.append(Max[i][j] + "?");
??????}
??????txtOutput.append("???");
??????for (int j = 0; j < N; j++) {
????????txtOutput.append(Allocation[i][j] + "?");
??????}
??????txtOutput.append("???");
??????for (int j = 0; j < N; j++) {
????????txtOutput.append(Need[i][j] + "?");
??????}
??????txtOutput.append("\n");
????}
??}
??public void test(int i) {
????for (int j = 0; j < N; j++) {
??????Available[j] = Available[j] - Request[j];
??????Allocation[i][j] = Allocation[i][j] + Request[j];
??????Need[i][j] = Need[i][j] - Request[j];
????}
??}
??public void retest(int i) {
????for (int j = 0; j < N; j++) {
??????Available[j] = Available[j] + Request[j];
??????Allocation[i][j] = Allocation[i][j] - Request[j];
??????Need[i][j] = Need[i][j] + Request[j];
????}
??}
??public boolean isSafe() {
????int[] work = new int[N];
????int[] finish = new int[M];
????int[] security = new int[M];
????int k = 0;
????int count = 0;
????int i, j;
????for (i = 0; i < N; i++) {
??????work[i] = Available[i];
????}
????for (i = 0; i < M; i++) {
??????finish[i] = 0;
????}
????while (count < M) {
??????for (i = 0; i < M; i++) {
????????if (finish[i] == 0) {
??????????for (j = 0; j < N; j++) {
????????????if (Need[i][j] > work[j]) {
??????????????break;
????????????}
??????????}
??????????if (j == N) {
????????????for (int m = 0; m < N; m++) {
??????????????work[m] = work[m] + Allocation[i][m];
????????????}
????????????security[k++] = i;
????????????finish[i] = 1;
????????????count++;
??????????}
????????}
??????}
????}
????if (count == M) {
??????txtOutput.append("System is in a safe state.\n");
??????txtOutput.append("Safe sequence is: ");
??????for (i = 0; i < M - 1; i++) {
????????txtOutput.append("P" + security[i] + " -> ");
??????}
??????txtOutput.append("P" + security[M - 1] + "\n");
??????return true;
????} else {
??????txtOutput.append("System is in an unsafe state.\n");
??????return false;
????}
??}
??@Override
??public void actionPerformed(ActionEvent e) {
????if (e.getSource() == btnRequest) {
??????for (int i = 0; i < N; i++) {
????????Request[i] = Integer.parseInt(txtRequest[i].getText());
??????}
??????if (isSafe()) {
????????test(currentProcess);
????????if (isSafe()) {
??????????txtOutput.append("Request can be granted!\n");
??????????txtOutput.append("New Resource Allocation:\n");
??????????txtOutput.append("?????Max?? ??Allocation???Need\n");
??????????txtOutput.append("Process???");
??????????for (int j = 0; j < 3; j++) {
????????????for (int i = 0; i < N; i++) {
??????????????txtOutput.append(NAME[i] + "?");
????????????}
????????????txtOutput.append("???");
??????????}
??????????txtOutput.append("\n");
??????????txtOutput.append("P" + currentProcess + "??");
??????????for (int j = 0; j < N; j++) {
????????????txtOutput.append(Max[currentProcess][j] + "?");
??????????}
??????????txtOutput.append("???");
??????????for (int j = 0; j < N; j++) {
????????????txtOutput.append(Allocation[currentProcess][j] + "?");
??????????}
??????????txtOutput.append("???");
??????????for (int j = 0; j < N; j++) {
????????????txtOutput.append(Need[currentProcess][j] + "?");
??????????}
??????????txtOutput.append("\n");
????????} else {
??????????retest(currentProcess);
??????????txtOutput.append("Request cannot be granted! It will cause an unsafe state.\n");
????????}
??????} else {
????????txtOutput.append("Request cannot be granted! It will cause an unsafe state.\n");
??????}
??????txtOutput.append("\n");
????}
??}
??public static void main(String[] args) {
????SwingUtilities.invokeLater(() -> {
??????BankGUI gui = new BankGUI();
??????gui.init();
??????gui.showData();
??????gui.setVisible(true);
????});
??}
}
標(biāo)簽: