// 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); }
// Create a new flight, or add seats to existing flight. // Note: if flightPrice <= 0 and the flight already exists, it maintains // its current price. @Override public boolean addFlight(int id, int flightNumber, int numSeats, int flightPrice) { Trace.info( "RM::addFlight(" + id + ", " + flightNumber + ", $" + flightPrice + ", " + numSeats + ") called."); Flight curObj = (Flight) readData(id, Flight.getKey(flightNumber)); synchronized (syncLock) { if (curObj == null) { // Doesn't exist; add it. Flight newObj = new Flight(flightNumber, numSeats, flightPrice); writeData(id, newObj.getKey(), newObj); Trace.info( "RM::addFlight(" + id + ", " + flightNumber + ", $" + flightPrice + ", " + numSeats + ") OK."); } else { // Add seats to existing flight and update the price. curObj.setCount(curObj.getCount() + numSeats); if (flightPrice > 0) { curObj.setPrice(flightPrice); } writeData(id, curObj.getKey(), curObj); Trace.info( "RM::addFlight(" + id + ", " + flightNumber + ", $" + flightPrice + ", " + numSeats + ") OK: " + "seats = " + curObj.getCount() + ", price = $" + flightPrice); } return (true); } }