HELP ME IN MY BUS RESERVATION PROJECT I NEED TO HAVE A CONDITONAL STATEMENT IN MT SEATING ARRANGEMENT FOR EXAMPLE IF THE SEAT NUMBER IS ALREADY SELECTED IT WILL POP UP THE SEAT IS ALREADY SELECTED I USE THE COMBO BOX FOR THE SEAT NUMBER

Recommended Answers

All 4 Replies

You’re saying you use a combo box for the seat number. So I assume this project has some sort of front end user interface? Can you please provide the code you have so far so we can see what changes need to be made? It’s pretty much impossible to know what tweaks need to be made without seeing what you have so far.

commented: private void OKActionPerformed(java.awt.event.ActionEvent evt) { String name = NAME.getText(); +0
Here's my code
private void OKActionPerformed(java.awt.event.ActionEvent evt) {      
    String name = NAME.getText();
    Object seatn = seatno.getSelectedItem();
    Object time = TIME.getSelectedItem();
    Object start = Start.getSelectedItem();
    Object Desti =Destination.getSelectedItem();
    Object bs = busno.getSelectedItem();
    Object bust = bustype.getSelectedItem();
    Object price= PRICE.getSelectedItem();
    SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd");
    String date = date_format.format(DATE.getDate());
    DefaultTableModel model = (DefaultTableModel)jTable2.getModel();
    model.addRow(new Object[]{name,start,Desti,bs,
        date + " "+time  ,seatn ,bust,price });     
try
{
    connection();
    Class.forName("com.mysql.jdbc.Driver");
    String s;
    ps=(Statement) con.createStatement();
    s = "INSERT INTO book"+"(Name,Start,Destination,BusNo,Departure,SeatNo,BusType,Price)"  + "VALUES('"+name+"','"+ start+"','"+ Desti+"','"+ bs+"','"+ date+"','"+ seatn+"','"+ bust+"','"+ price+"')";
    ps.executeUpdate(s);
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);}

Are you reading the database to get the current states of the seats before the code above is called? You would need to have a list of the seats that have already been taken. Then after line 4 in your code sample, you could check the user's selection against that list and determine if the seat the user selected is already taken and issue an alert.

Or, you could insert a read of the database after line 4 to determine the same thing. But if you were to gather the list up-front, it would be one call to the database instead of potentially many.

Here's the bit I'm having trouble with:

You are already at the stage of using persistence with actual databases, meaning for a beginner, pretty advanced material, yet you don't know how or where to implement a simple conditional statement ...

Start without a database. look at your 'bus' with it's seats as a data structure.
For instance, you have a bus with 10 rows of 4 seatsh?

private boolean[] seatsTaken = new boolean[10][4];

There you have it. Your 'initial bus'. (I'm just showing you a simple example, without timeframes. The seat is available when the value for that seat is false, otherwise it's occupied.

private boolean checkSeatIsFree(int row, int seat) {
  return seatsTaken[row][seat];
}  

Now, you can easily verify whether or not the seat is free before assigning it.

public void occupySeat(int row, int seat) throws DoubleBookingException {
  if ( checkSeatIsFree(row, seat) ) {
    throw new DoubleBookingException("This seat is already occupied");
  }
  seatsTaken[row][seat] = true;
}  

The same goes for releasing a seat, but here, the check is not needed. After all, the application flow won't really cause any issues if you release a non-occupied seat:

public void releaseSeat(int row, int seat) {
  seatsTaken[row][seat] = false;
} 

This is the basic of the logic surrounding the whole.
Next, you'll create a Seat class, containing a list of occupations, these contain the timeframes, as you need. When you go to check, you verify whether it is currently free.

When all that works, that's when you add the database into the mix. The logic remains the same, but it's a lot easier to develop if you go step by step.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.