Home > Software design >  i want to do "if a person book a seat the available seats must be depreciate by one". how
i want to do "if a person book a seat the available seats must be depreciate by one". how

Time:11-01

THE PROCESS IS "I a person book a seat the available seats must be depreciate by one seat. The number of seats available are 35. I think I have to use array for this but I don't have an idea how to use it.

The below code (available_seats) that's where i want to show the available seats.

      try {

        String route_number = rnB.toString();
        String depature_on = doB.toString();
        String depature_time = dtB.toString();
        String time_taken = timeB.toString();
        String available_seats = asB.toString();
        String price = pB.toString();
        String custname = name.toString();
        String custcontact = contact.toString();
        
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/eurobusdb?useSSL=false","root","Abc123");
        
        String SQL_INSERT_BOOKING = "INSERT INTO booking (Depature_Country_And_City , Destination_Country_And_City, Depature_On, Depature_Time, Estimated_Time_Taken, Available_Seats, Price, Status, route, name, contact_number) VALUES ( ?,?,?,?,?,?,?,?,?,?,?);";
        
        PreparedStatement preparedStatement = connection.prepareStatement(SQL_INSERT_BOOKING);
              System.out.println(preparedStatement);
        

        preparedStatement.setString(1, String.valueOf(combo1.getSelectedItem()));
        preparedStatement.setString(2, String.valueOf(combo2.getSelectedItem()));
        preparedStatement.setString(3, doB.getText());
        preparedStatement.setString(4, dtB.getText());
        preparedStatement.setString(5, timeB.getText());
        preparedStatement.setString(6, asb.getText());
        preparedStatement.setString(7, pB.getText());
        preparedStatement.setString(8, "Booked");
        preparedStatement.setString(9, rnB.getText());
        preparedStatement.setString(10, name.getText());
        preparedStatement.setString(11, contact.getText());
        preparedStatement.executeUpdate();
        
        System.out.println(preparedStatement);
          }
              
              
              
    } catch (Exception e) {
    }

CodePudding user response:

From what you have posted, I feel you need to hardcode a value of number of seats as n i.e.

int n = 35;

From then on create a method to book seat and when that method is called from the front end decrement the value by one.

return_type bookSeat(someParameters...){
 // Some Function Code
  n--;
}

If you want to have a specific thing like seat numbers and if that seat is booked or not types then create a boolan array something like

boolean arr[n 1] // denoting one based indexing

and initially set flag values as true or false denoting booked or not and then when the seat is booked then check if the seat is already booked or not. If suppose i need to check by 23rd seat is booked or not we could check

if(seatArr[23] == true){
//Some function or output
}
else{
//Something else
}
  • Related