|
6 | 6 | import java.rmi.RemoteException; |
7 | 7 | import java.io.*; |
8 | 8 |
|
9 | | -public class FlightResourceManager extends AbstractRMHashMapManager implements IFlightResourceManager |
| 9 | +public abstract class FlightResourceManager extends AbstractRMHashMapManager implements IFlightResourceManager |
10 | 10 | { |
11 | 11 | // Create a new flight, or add seats to existing flight |
12 | 12 | // NOTE: if flightPrice <= 0 and the flight already exists, it maintains its current price |
13 | 13 | private String m_name = ""; |
| 14 | + protected ICustomerResourceManager customerRM; |
| 15 | + |
14 | 16 | public FlightResourceManager(String p_name) { |
15 | 17 | m_name = p_name; |
16 | 18 | } |
@@ -68,4 +70,53 @@ public String getName() throws RemoteException |
68 | 70 | { |
69 | 71 | return m_name; |
70 | 72 | } |
| 73 | + |
| 74 | + public boolean reserveItem(int xid, int customerID, String key, String location) throws RemoteException |
| 75 | + { |
| 76 | + Trace.info("RM::reserveItem(" + xid + ", customer=" + customerID + ", " + key + ", " + location + ") called" ); |
| 77 | + // Read customer object if it exists (and read lock it) |
| 78 | + Customer customer = null; |
| 79 | + try { |
| 80 | + customer = getCustomer(xid, customerID); |
| 81 | + if (customer == null) |
| 82 | + { |
| 83 | + Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--customer doesn't exist"); |
| 84 | + return false; |
| 85 | + } |
| 86 | + } catch (RemoteException e) { |
| 87 | + Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--customer doesn't exist--remote exception"); |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + // Check if the item is available |
| 92 | + ReservableItem item = (ReservableItem)readData(xid, key); |
| 93 | + if (item == null) |
| 94 | + { |
| 95 | + Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--item doesn't exist"); |
| 96 | + return false; |
| 97 | + } |
| 98 | + else if (item.getCount() == 0) |
| 99 | + { |
| 100 | + Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--No more items"); |
| 101 | + return false; |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + customer.reserve(key, location, item.getPrice()); |
| 106 | + writeData(xid, customer.getKey(), customer); |
| 107 | + |
| 108 | + // Decrease the number of available items in the storage |
| 109 | + item.setCount(item.getCount() - 1); |
| 110 | + item.setReserved(item.getReserved() + 1); |
| 111 | + writeData(xid, item.getKey(), item); |
| 112 | + |
| 113 | + Trace.info("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") succeeded"); |
| 114 | + return true; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + public Customer getCustomer(int xid, int customerID) throws RemoteException |
| 119 | + { |
| 120 | + return customerRM.getCustomer(xid, customerID); |
| 121 | + } |
71 | 122 | } |
0 commit comments