Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

MarketManagerRole

Matthew Pohlmann edited this page Jan 1, 2014 · 1 revision

####Description A MarketManager runs the market and is the point of contact for any consumers. He assigns orders to MarketEmployees and handles the cash flow.

####Class Signature

public class MarketManagerRole extends Role implements MarketManagerRole {}

####Data

String name;
List<Order> orders;
List<MarketEmployee> employees;
Map<MarketConsumer, Float> consumerBalance;
Map<String, Item> inventory;
List<DeliveryTruck> deliveryTrucks;
enum OrderState {PENDING, PROCESSING, READY, SENT};
enum OrderType {INPERSON, DELIVERY};
private class Order {
	List<Item> items;
	MarketConsumer consumer;
	OrderState state;
	OrderType type;
	float totalPrice;
	int orderID;
	private static int nextOrderID = 0;
	
	public Order(MarketConsumer c, List<Item> i, OrderState s, OrderType t) {
		items = i;
		consumer = c;
		state = s;
		type = t;
		orderID = nextOrderID++;
	}
	
	public CalculateTotalPrice() {
		float runningTotal = 0.0f;
		for (Item i : items) {
			totalCost += i.price;
		}
		totalPrice = runningTotal;
	}
}
enum EmployeeState {AVAILABLE, BUSY}
private class MyEmployee {
	MarketEmployee employee;
	EmployeeState state;
}
public class Item {
	String name;
	float price;
	int quantity;
}

####Scheduler

if Ǝ Order o in orderso.state == OrderState.PENDING and
   Ǝ MarketEmployee e in employeese.state == EmployeeState.AVAILABLE then,
    ProcessOrder(o, e);
    return true;
if Ǝ Order o in orderso.state == OrderState.READY then,
    DispatchOrder(o);
    return true;

####Messages

public void msgHereIsMyOrder(MarketConsumer consumer, List<Item> items) {
	// Add the new order to the list of orders
	orders.add(new Order(consumer, items, OrderState.PENDING, OrderType.INPERSON);
	
    stateChanged();
}
public void msgHereIsMyOrderForDelivery(MarketConsumer consumer, List<Item> items) {
	// Add the new order to the list of orders
	orders.add(new Order(consumer, items, OrderState.PENDING, OrderType.DELIVERY);
	
    stateChanged();
}
public void msgIWouldLikeACar(MarketConsumer consumer) {
	// Add the new order to the list of orders
	carOrders.add(new CarOrder
}
public void msgHereIsMyPayment(MarketConsumer consumer, float amount) {
	// Set the order state to paid
	consumerBalance.get(consumer) -= amount;

    stateChanged();
}
public void msgHereIsMyPayment(Structure structure) {
	// Set the order state to paid
	structureBalance.get(structure) -= amount;
	
	stateChanged();
}
public void msgDeliveryFailed(int deliveryID) {
	// Set the order status to FAILED
	orders.get(deliveryID).state = FAILED;
	
	stateChanged();
}
public void msgHereAreItems(MarketEmployee employee, List<Item> items, int id) {
	// Find the consumer's order in our list
	Order theOrder = null;
	for (Order o : orders) {
		if (o.id == id) {
			theOrder = o;
			break;
		}
	}
	
	// Set his items to the ones the employee returned
	theOrder.items = items;
	
	// The order is ready to be shipped
	theOrder.state = OrderState.READY;
	
	// The employee can process other orders
	MyEmployee myEmployee = null;
	for (MyEmployee e : employees) {
		if (e == employee) {
			myEmployee = e;
		}
	}
	myEmployee.state = EmployeeState.AVAILABLE;

    stateChanged();
}
public void msgHereIsCar(MarketEmployee employee, int id) {
	// Find the consumer's order in our list via id
	Order o = orders.find(id);
	
	o.consumer.msgHereIsYourCar();
}

####Actions
```Java
private void ProcessOrder(Order o, MyEmployee e) {
	// Send the employee a message to retrieve the items
	e.msgRetrieveItems(this, o.items, o.id);
	e.state = EmployeeState.BUSY;
	
	// Mark the order as being processed
	o.state = OrderState.PROCESSING;
	
	// Next time, send the message to the next employee
	currentEmployee++;
	currentEmployee = currentEmployee % employees.size();
}
private void DispatchOrder(Order o) {
	// Depending on the order type, dispatch the order
	if (o.type = OrderType.INPERSON) {
		// The consumer is standing right there, so just give him the items
		o.consumer.msgHereAreYourItems(o.items);
	} else if (o.type == OrderType.DELIVERY) {
		// The consumer wants the items delivered to him
                for (DeliveryTruck t : deliveryTrucks) {
                     if (t.available()) {
                          t.msgDeliveryItemsToPerson(o.items, o.consumer);
                          break;
                     }
                }
	}
	
	// The order has now been sent
	o.state = OrderState.SENT;
	
	// Calculate the price of the order
	o.CalculateTotalPrice();
	
	// The consumer needs to pay for the order
	Float balance = consumerBalance.get(consumer);
	consumerBalance.put(o.consumer, (balance == null ? o.totalPrice : balance + o.totalPrice));
	o.consumer.msgHereIsYourTotal(o.totalPrice);
}

####Utility

public void AddEmployee(MarketEmployee e) {
	employees.add(new MyEmployee(e, EmployeeState.AVAILABLE));
}

Wiki Home | Design Home

Clone this wiki locally