Example #1
0
  // Create a new flight, or add seats to existing flight
  //  NOTE: if flightPrice <= 0 and the flight already exists, it maintains its current price
  public boolean addFlight(int id, int flightNum, int flightSeats, int flightPrice)
      throws RemoteException, InvalidTransactionException, TransactionAbortedException {
    Trace.info(
        "RM::addFlight("
            + id
            + ", "
            + flightNum
            + ", $"
            + flightPrice
            + ", "
            + flightSeats
            + ") called");
    Flight curObj = (Flight) readData(id, Flight.getKey(flightNum));
    if (curObj == null) {
      // doesn't exist...add it
      Flight newObj = new Flight(flightNum, flightSeats, flightPrice);

      writeData(id, newObj.getKey(), newObj);
      String key = newObj.getKey();
      if (readDataFromLog(id, key, id) == null) {
        Flight logObj = (Flight) newObj.clone();
        logObj.setCount(-1);
        logObj.type = 0;
        writeDataToLog(id, key, logObj);
      }
      Trace.info(
          "RM::addFlight("
              + id
              + ") created new flight "
              + flightNum
              + ", seats="
              + flightSeats
              + ", price=$"
              + flightPrice);
    } else {
      // add seats to existing flight and update the price...
      Flight logObj = (Flight) curObj.clone();
      if (readDataFromLog(id, curObj.getKey(), id) == null)
        writeDataToLog(id, curObj.getKey(), logObj);

      curObj.setCount(curObj.getCount() + flightSeats);
      if (flightPrice > 0) {
        curObj.setPrice(flightPrice);
      } // if
      writeData(id, curObj.getKey(), curObj);
      Trace.info(
          "RM::addFlight("
              + id
              + ") modified existing flight "
              + flightNum
              + ", seats="
              + curObj.getCount()
              + ", price=$"
              + flightPrice);
    } // else
    return (true);
  }