Skip to content

Commit 52f8142

Browse files
Neil DEVASNeil DEVAS
authored andcommitted
created resource manager interfaces and classes for car room and flight
1 parent b63f8c5 commit 52f8142

12 files changed

+1025
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package Server.Common;
2+
3+
import Server.Interface.*;
4+
5+
import java.util.*;
6+
import java.rmi.RemoteException;
7+
import java.io.*;
8+
9+
public abstract class AbstractRMHashMapManager
10+
{
11+
protected RMHashMap m_data = new RMHashMap();
12+
13+
protected RMItem readData(int xid, String key)
14+
{
15+
synchronized(m_data) {
16+
RMItem item = m_data.get(key);
17+
if (item != null) {
18+
return (RMItem)item.clone();
19+
}
20+
return null;
21+
}
22+
}
23+
24+
// Writes a data item
25+
protected void writeData(int xid, String key, RMItem value)
26+
{
27+
synchronized(m_data) {
28+
m_data.put(key, value);
29+
}
30+
}
31+
32+
// Remove the item out of storage
33+
protected void removeData(int xid, String key)
34+
{
35+
synchronized(m_data) {
36+
m_data.remove(key);
37+
}
38+
}
39+
40+
protected boolean deleteItem(int xid, String key)
41+
{
42+
Trace.info("RM::deleteItem(" + xid + ", " + key + ") called");
43+
ReservableItem curObj = (ReservableItem)readData(xid, key);
44+
// Check if there is such an item in the storage
45+
if (curObj == null)
46+
{
47+
Trace.warn("RM::deleteItem(" + xid + ", " + key + ") failed--item doesn't exist");
48+
return false;
49+
}
50+
else
51+
{
52+
if (curObj.getReserved() == 0)
53+
{
54+
removeData(xid, curObj.getKey());
55+
Trace.info("RM::deleteItem(" + xid + ", " + key + ") item deleted");
56+
return true;
57+
}
58+
else
59+
{
60+
Trace.info("RM::deleteItem(" + xid + ", " + key + ") item can't be deleted because some customers have reserved it");
61+
return false;
62+
}
63+
}
64+
}
65+
66+
// Query the number of available seats/rooms/cars
67+
protected int queryNum(int xid, String key)
68+
{
69+
Trace.info("RM::queryNum(" + xid + ", " + key + ") called");
70+
ReservableItem curObj = (ReservableItem)readData(xid, key);
71+
int value = 0;
72+
if (curObj != null)
73+
{
74+
value = curObj.getCount();
75+
}
76+
Trace.info("RM::queryNum(" + xid + ", " + key + ") returns count=" + value);
77+
return value;
78+
}
79+
80+
// Query the price of an item
81+
protected int queryPrice(int xid, String key)
82+
{
83+
Trace.info("RM::queryPrice(" + xid + ", " + key + ") called");
84+
ReservableItem curObj = (ReservableItem)readData(xid, key);
85+
int value = 0;
86+
if (curObj != null)
87+
{
88+
value = curObj.getPrice();
89+
}
90+
Trace.info("RM::queryPrice(" + xid + ", " + key + ") returns cost=$" + value);
91+
return value;
92+
}
93+
94+
// Reserve an item
95+
protected boolean reserveItem(int xid, int customerID, String key, String location)
96+
{
97+
Trace.info("RM::reserveItem(" + xid + ", customer=" + customerID + ", " + key + ", " + location + ") called" );
98+
// Read customer object if it exists (and read lock it)
99+
Customer customer = (Customer)readData(xid, Customer.getKey(customerID));
100+
if (customer == null)
101+
{
102+
Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--customer doesn't exist");
103+
return false;
104+
}
105+
106+
// Check if the item is available
107+
ReservableItem item = (ReservableItem)readData(xid, key);
108+
if (item == null)
109+
{
110+
Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--item doesn't exist");
111+
return false;
112+
}
113+
else if (item.getCount() == 0)
114+
{
115+
Trace.warn("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") failed--No more items");
116+
return false;
117+
}
118+
else
119+
{
120+
customer.reserve(key, location, item.getPrice());
121+
writeData(xid, customer.getKey(), customer);
122+
123+
// Decrease the number of available items in the storage
124+
item.setCount(item.getCount() - 1);
125+
item.setReserved(item.getReserved() + 1);
126+
writeData(xid, item.getKey(), item);
127+
128+
Trace.info("RM::reserveItem(" + xid + ", " + customerID + ", " + key + ", " + location + ") succeeded");
129+
return true;
130+
}
131+
}
132+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package Server.Common;
2+
3+
import Server.Interface.*;
4+
5+
import java.util.*;
6+
import java.rmi.RemoteException;
7+
import java.io.*;
8+
9+
public class CarResourceManager extends AbstractRMHashMapManager implements ICarResourceManager
10+
{
11+
// Create a new car location or add cars to an existing location
12+
// NOTE: if price <= 0 and the location already exists, it maintains its current price
13+
public boolean addCars(int xid, String location, int count, int price) throws RemoteException
14+
{
15+
Trace.info("RM::addCars(" + xid + ", " + location + ", " + count + ", $" + price + ") called");
16+
Car curObj = (Car)readData(xid, Car.getKey(location));
17+
if (curObj == null)
18+
{
19+
// Car location doesn't exist yet, add it
20+
Car newObj = new Car(location, count, price);
21+
writeData(xid, newObj.getKey(), newObj);
22+
Trace.info("RM::addCars(" + xid + ") created new location " + location + ", count=" + count + ", price=$" + price);
23+
}
24+
else
25+
{
26+
// Add count to existing car location and update price if greater than zero
27+
curObj.setCount(curObj.getCount() + count);
28+
if (price > 0)
29+
{
30+
curObj.setPrice(price);
31+
}
32+
writeData(xid, curObj.getKey(), curObj);
33+
Trace.info("RM::addCars(" + xid + ") modified existing location " + location + ", count=" + curObj.getCount() + ", price=$" + price);
34+
}
35+
return true;
36+
}
37+
38+
// Adds car reservation to this customer
39+
public boolean reserveCar(int xid, int customerID, String location) throws RemoteException
40+
{
41+
return reserveItem(xid, customerID, Car.getKey(location), location);
42+
}
43+
44+
// Delete cars at a location
45+
public boolean deleteCars(int xid, String location) throws RemoteException
46+
{
47+
return deleteItem(xid, Car.getKey(location));
48+
}
49+
// Returns the number of cars available at a location
50+
public int queryCars(int xid, String location) throws RemoteException
51+
{
52+
return queryNum(xid, Car.getKey(location));
53+
}
54+
55+
// Returns price of cars at this location
56+
public int queryCarsPrice(int xid, String location) throws RemoteException
57+
{
58+
return queryPrice(xid, Car.getKey(location));
59+
}
60+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package Server.Common;
2+
3+
import Server.Interface.*;
4+
5+
import java.util.*;
6+
import java.rmi.RemoteException;
7+
import java.io.*;
8+
9+
public class FlightResourceManager extends AbstractRMHashMapManager implements IFlightResourceManager
10+
{
11+
// Create a new flight, or add seats to existing flight
12+
// NOTE: if flightPrice <= 0 and the flight already exists, it maintains its current price
13+
public boolean addFlight(int xid, int flightNum, int flightSeats, int flightPrice) throws RemoteException
14+
{
15+
Trace.info("RM::addFlight(" + xid + ", " + flightNum + ", " + flightSeats + ", $" + flightPrice + ") called");
16+
Flight curObj = (Flight)readData(xid, Flight.getKey(flightNum));
17+
if (curObj == null)
18+
{
19+
// Doesn't exist yet, add it
20+
Flight newObj = new Flight(flightNum, flightSeats, flightPrice);
21+
writeData(xid, newObj.getKey(), newObj);
22+
Trace.info("RM::addFlight(" + xid + ") created new flight " + flightNum + ", seats=" + flightSeats + ", price=$" + flightPrice);
23+
}
24+
else
25+
{
26+
// Add seats to existing flight and update the price if greater than zero
27+
curObj.setCount(curObj.getCount() + flightSeats);
28+
if (flightPrice > 0)
29+
{
30+
curObj.setPrice(flightPrice);
31+
}
32+
writeData(xid, curObj.getKey(), curObj);
33+
Trace.info("RM::addFlight(" + xid + ") modified existing flight " + flightNum + ", seats=" + curObj.getCount() + ", price=$" + flightPrice);
34+
}
35+
return true;
36+
}
37+
38+
// Deletes flight
39+
public boolean deleteFlight(int xid, int flightNum) throws RemoteException
40+
{
41+
return deleteItem(xid, Flight.getKey(flightNum));
42+
}
43+
44+
// Adds flight reservation to this customer
45+
public boolean reserveFlight(int xid, int customerID, int flightNum) throws RemoteException
46+
{
47+
return reserveItem(xid, customerID, Flight.getKey(flightNum), String.valueOf(flightNum));
48+
}
49+
50+
// Returns the number of empty seats in this flight
51+
public int queryFlight(int xid, int flightNum) throws RemoteException
52+
{
53+
return queryNum(xid, Flight.getKey(flightNum));
54+
}
55+
56+
// Returns price of a seat in this flight
57+
public int queryFlightPrice(int xid, int flightNum) throws RemoteException
58+
{
59+
return queryPrice(xid, Flight.getKey(flightNum));
60+
}
61+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package Server.Common;
2+
3+
import Server.Interface.*;
4+
5+
import java.util.*;
6+
import java.rmi.RemoteException;
7+
import java.io.*;
8+
9+
public abstract class MiddlewareResourceManager implements IResourceManager
10+
{
11+
private CarResourceManager carRM;
12+
private FlightResourceManager flightRM;
13+
private RoomResourceManager roomRM;
14+
15+
// Create a new flight, or add seats to existing flight
16+
// NOTE: if flightPrice <= 0 and the flight already exists, it maintains its current price
17+
public boolean addFlight(int xid, int flightNum, int flightSeats, int flightPrice) throws RemoteException
18+
{
19+
flightRM.addFlight(xid, flightNum, flightSeats, flightPrice);
20+
};
21+
22+
// Deletes flight
23+
public boolean deleteFlight(int xid, int flightNum) throws RemoteException
24+
{
25+
flightRM.deleteFlight(xid, flightNum);
26+
}
27+
28+
// Returns the number of empty seats in this flight
29+
public int queryFlight(int xid, int flightNum) throws RemoteException
30+
{
31+
flightRM.queryFlight(xid, flightNum);
32+
};
33+
34+
// Returns price of a seat in this flight
35+
public int queryFlightPrice(int xid, int flightNum) throws RemoteException
36+
{
37+
return flightRM.queryFlightPrice(xid, flightNum);
38+
}
39+
// Adds flight reservation to this customer
40+
public boolean reserveFlight(int xid, int customerID, int flightNum) throws RemoteException
41+
{
42+
return flightRM.reserveFlight(xid, customerID, flightNum);
43+
};
44+
45+
// Adds car reservation to this customer
46+
public boolean reserveCar(int xid, int customerID, String location) throws RemoteException
47+
{
48+
return carRM.reserveCar(xid, customerID, location);
49+
};
50+
51+
// Create a new car location or add cars to an existing location
52+
// NOTE: if price <= 0 and the location already exists, it maintains its current price
53+
public boolean addCars(int xid, String location, int count, int price) throws RemoteException
54+
{
55+
return carRM.addCars(xid, location, count, price);
56+
}
57+
58+
// Delete cars at a location
59+
public boolean deleteCars(int xid, String location) throws RemoteException
60+
{
61+
return carRM.deleteItem(xid, location);
62+
}
63+
64+
// Returns the number of cars available at a location
65+
public int queryCars(int xid, String location) throws RemoteException
66+
{
67+
return carRM.queryCars(xid, location);
68+
}
69+
70+
// Returns price of cars at this location
71+
public int queryCarsPrice(int xid, String location) throws RemoteException
72+
{
73+
return carRM.queryPrice(xid, location);
74+
}
75+
76+
// Adds room reservation to this customer
77+
public boolean reserveRoom(int xid, int customerID, String location) throws RemoteException
78+
{
79+
return roomRM.reserveRoom(xid, customerID, location);
80+
}
81+
82+
// Create a new room location or add rooms to an existing location
83+
// NOTE: if price <= 0 and the room location already exists, it maintains its current price
84+
public boolean addRooms(int xid, String location, int count, int price) throws RemoteException
85+
{
86+
return roomRM.addRooms(xid, location, count, price);
87+
}
88+
89+
// Returns the amount of rooms available at a location
90+
public int queryRooms(int xid, String location) throws RemoteException
91+
{
92+
return roomRM.queryRooms(xid, location);
93+
}
94+
95+
// Returns room price at this location
96+
public int queryRoomsPrice(int xid, String location) throws RemoteException
97+
{
98+
return roomRM.queryRoomsPrice(xid, location);
99+
}
100+
101+
// Delete rooms at a location
102+
public boolean deleteRooms(int xid, String location) throws RemoteException
103+
{
104+
return roomRM.deleteRooms(xid, location);
105+
}
106+
107+
}

0 commit comments

Comments
 (0)