Hello I have to write a Java program for this problem Can an

Hello, I have to write a Java program for this problem. Can anyone here help me I need some guidance on how to proceed (Use of interface is mandatory) ? Thank you

An aircraft has 200 seats; each row has 4 seats with an isle in the middle. In other words, two seats on the left side of the airplane a space and then two seats on the right side of the airplane. There are 50 rows. Seat reservation is problematic because multiple users attempt to reserve seats at the same time and they are not aware of one another. Obvious conflicts can occur, for example, two users attempting to reserve the same seat at the same time. For this question you will implement three threads that will attempt seat reservations. These threads will represent brokers who provide this service to customers. Two threads will be automated producer functions that randomly attempt seat reservations. The third producer will not be automated; instead it is a GUI Swing form that you will use to make manual reservations, as the third broker. Each producer has an ID number: 1, 2, and 3. These ID numbers will be recorded with the seat reservation so that we can know which producer did what. The GUI displays the airplane seats together with the producer ID number and if the seat is available. To do this you have two choices: choice 1- draw a white box for each seat. An empty white box means the seat is available. A successfully reserved seat will have the producer\'s ID number written within the box. Choice 2-print a string of underline characters (IE. for each empty seat. Overwrite the underline with the producer\'s ID number to indicate that it is taken (in other words, not a graphic based solution). Use the row and column number method to identify which seat you are interested in. To summarize, this swing GUI displays the reservation form and the airplane seating availability \"diagram\"

Solution


package airbooking;

public class broker extends Thread{
private int seats[][]=new int[50][4];
private int b;
public broker(int b)
{
for(int i=0;i<50;i++)
for(int j=0;j<4;j++)
this.seats[i][j]=0;
this.b=b;
this.start();
}
  
  
private boolean isValid(int i , int j)
{
boolean ret = true;
if(i==j && j==1)
ret=false;
return ret;
}
  
public boolean bookSeat(int i , int j)
{
boolean ret = true;
if(isValid(i,j) && i<50 && j<4)
this.seats[i][j]=b;
else
ret=false;
return ret;
}

public int[][] getSeats() {
return seats;
}
  
public String getArrangement()
{
String ret=\"\";
for(int i=0;i<50;i++)
{
ret=ret+\"\\t\";
for(int j=0;j<4;j++)
{
if(this.seats[i][j]!=0)
ret=ret+seats[i][j]+\"\\t\";
else
ret=ret+\"_\\t\";
}
ret=ret+\"\ \";
}
return ret;
}

public void setSeats(int[][] seats) {
this.seats = seats;
}
  
public void Run()
{
System.out.println(\"Broker \"+b+\"is currently booking!\");
}
}

-----------------------------------------------------------------------------------------------------

package airbooking;

public class BookingGUI extends javax.swing.JFrame {

/** Creates new form BookingGUI */
public BookingGUI() {
b1 = new broker(1);
b2 = new broker(2);
b3 = new broker(3);
for(int i=0;i<50;i++)
for(int j=0;j<4;j++)
this.seats[i][j]=0;
initComponents();
display.setText(b3.getArrangement());

}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings(\"unchecked\")
// <editor-fold defaultstate=\"collapsed\" desc=\"Generated Code\">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
display = new javax.swing.JTextArea();
r = new javax.swing.JTextField();
c = new javax.swing.JTextField();
book = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText(\"Airplane Reservation System\");

display.setColumns(20);
display.setRows(5);
jScrollPane1.setViewportView(display);

book.setText(\"Book\");
book.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bookActionPerformed(evt);
}
});

jLabel2.setText(\"Row\");

jLabel3.setText(\"Column\");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(178, 178, 178)
.addComponent(jLabel1))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 489, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(138, 138, 138)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(r, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(c, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(11, 11, 11)
.addComponent(jLabel2)
.addGap(39, 39, 39)
.addComponent(jLabel3)))
.addGap(18, 18, 18)
.addComponent(book)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 522, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
.addComponent(book)
.addGap(27, 27, 27))
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(r, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(c, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())))
);

pack();
}// </editor-fold>

private void bookActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
this.bookTicket(Integer.parseInt(r.getText()), Integer.parseInt(c.getText()), b3);
this.bookTicket((int)(Math.random()*1000)%50, (int)(Math.random()*1000)%4, b1);
this.bookTicket((int)(Math.random()*1000)%50, (int)(Math.random()*1000)%4, b2);
this.display.setText(this.b2.getArrangement());
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate=\"collapsed\" desc=\" Look and feel setting code (optional) \">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if (\"Nimbus\".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(BookingGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(BookingGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(BookingGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(BookingGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new BookingGUI().setVisible(true);
}
});
}
private void bookTicket(int r,int c,broker b)
{
b.setSeats(this.seats);
b.bookSeat(r, c);
this.seats=b.getSeats();
}
// Variables declaration - do not modify
private javax.swing.JButton book;
private javax.swing.JTextField c;
private javax.swing.JTextArea display;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField r;
// End of variables declaration

private broker b1,b2,b3;
private int seats[][]=new int[50][4];
}

Hello, I have to write a Java program for this problem. Can anyone here help me I need some guidance on how to proceed (Use of interface is mandatory) ? Thank y
Hello, I have to write a Java program for this problem. Can anyone here help me I need some guidance on how to proceed (Use of interface is mandatory) ? Thank y
Hello, I have to write a Java program for this problem. Can anyone here help me I need some guidance on how to proceed (Use of interface is mandatory) ? Thank y
Hello, I have to write a Java program for this problem. Can anyone here help me I need some guidance on how to proceed (Use of interface is mandatory) ? Thank y
Hello, I have to write a Java program for this problem. Can anyone here help me I need some guidance on how to proceed (Use of interface is mandatory) ? Thank y

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site