Rest 4.1 Design

  • Upload
    jc-l

  • View
    218

  • Download
    0

Embed Size (px)

Citation preview

  • 8/2/2019 Rest 4.1 Design

    1/19

    Waiter Agent

    Messages

    public void msgSitCustomerAtTable (CustomerAgent customer, int tableNum) {

    MyCustomer c = new MyCustomer(customer, tableNum);c.state = CustomerState.NEED_SEATED;

    customers.add(c);

    stateChanged();

    }

    Host sends this to give the waiter a new customer, which the waiter will add to itslist of customers

    public void msgImReadyToOrder(CustomerAgent customer) {

    if ( Customer c in customers c.equals(customer)) {

    c.state = CustomerState.READY_TO_ORDER;

    stateChanged();return;

    }

    }

    Customer sends this when they are ready, which will make the waiter change thecustomers state for that customer

    public void msgIWantToReorder(CustomerAgent customer) {

    if ( Customer c in customers c.equals(customer)) {

    c.state = CustomerState.READY_TO_ORDER;

    stateChanged();

    return;

    }

    }

    public void msgHereIsMyChoice(CustomerAgent customer, String choice, boolean

    reordering) {

    if ( Customer c in customers c.equals(customer)) {

    c.choice = choice;

    if (!reordering) {

    c.state = CustomerState.ORDER_PENDING;

    } else {c.state = CustomerState.REORDERING;

    }

    stateChanged();

    return;

    }

    }

  • 8/2/2019 Rest 4.1 Design

    2/19

    Customer sends this to let the waiter know its choice, which the waiter will keep

    track of for the customer

    public void msgOrderIsReady(int tableNum, Food f) {

    if ( Customer c in customers c.tableNum == tableNum) {

    c.state = CustomerState.ORDER_READY;c.food = f;

    stateChanged();

    return;

    }

    }

    Cook sends this to let the waiter know that food has been preparedpublic void msgDoneEating (CustomerAgent customer) {

    if ( Customer c in customers c.tableNum == tableNum) {

    c.state = CustomerState.IS_DONE;

    stateChanged();return;

    }

    }

    Customer sends this when they are done eatingpublic void msgTheseItemsAreOut (List foodList, order) {

    order.customer.state = CustomerState.NEED_TO_REORDER;

    for ( String s in foodList) {

    order.unavailable.add(s);

    }

    stateChanged();

    return;

    }

    Cook sends this when an order cannot be cooked due to shortagepublic void msgTooLateToChangeOrder(Order order) {

    if ( Customer c in customers order.table == c.tableNum) {

    c.state = Customer.ORDER_PENDING;

    }

    }

    Cook sends this when an order was already being cooked and unable to be changedpublic void msgOrderChanged(Order order) {

    if ( Customer c in customersorder.table == c.tableNum) {

    c.state = Customer.ORDER_PENDING;

    }

    }

    Cook sends this when an order was able to be changed

  • 8/2/2019 Rest 4.1 Design

    3/19

    Scheduling

    if(!customers.isEmpty()) {

    if ( Customer c in customers c.state == CustomerState.ORDER_READY) {

    giveFoodToCustomer(c);return true;

    }

    o Gives food to the customer if an order is readyif ( Customer c in customers c.state == CustomerState.NEED_TO_REORDER) {

    retakeOrder(c);

    return true;

    }

    o Retakes the order for a customer who ordered a short itemif ( Customer c in customers c.state == CustomerState.IS_DONE) {

    clearTable(c);

    return true;

    }

    o Clears the table if a customer is doneif ( Customer c in customers c.state == CustomerState.NEED_SEATED) {

    seatCustomer(c);

    return true;

    }

    o Seats a customer if a customer is ready to be seatedif ( Customer c in customers c.state == CustomerState.ORDER_PENDING) {

    giveOrderToCook(c);

    return true;

    }

    o Gives the an order to the cook if a customer has orderedif ( Customer c in customers c.state == CustomerState.REORDERING) {

    giveReorderToCook(c);

    return true;

    }o Lets the cook now that a customer wants to reorder

    if ( Customer c in customers c.state == CustomerState.READY_TO_ORDER) {

    takeOrder(c);

    return true;

    }

    o Takes an order from a customer if he/she is ready to order

  • 8/2/2019 Rest 4.1 Design

    4/19

    }

    return false;

    Return false when there is nothing to doActions

    private void seatCustomer (MyCustomer customer) {

    DoSeatCustomer(customer);

    customer.state = CustomerState.NO_ACTION;

    customer.cmr.msgFollowMeToTable(this, new Menu());

    stateChanged();

    }

    Seats a customer at a tableprivate void takeOrder(MyCustomer customer) {

    DoTakeOrder(customer);customer.state = CustomerState.NO_ACTION;

    customer.cmr.msgWhatWouldYouLike();

    stateChanged();

    }

    Takes an order from a customerprivate void retakeOrder(MyCustomer customer) {

    DoTakeOrder(customer);

    customer.state = CustomerState.NO_ACTION;

    Menu menu = new Menu();

    menu.remove(order.unavailable);customer.cmr.msgPleaseReorder(menu);

    stateChanged();

    }

    Retakes an order from a customer with a lessened menuprivate void giveOrderToCook(MyCustomer customer) {

    customer.state = CustomerState.NO_ACTION;

    cook.msgHereIsAnOrder(this, customer.tableNum, customer.choice);

    stateChanged();

    }

    The waiter gives the cook a pending orderprivate void giveReorderToCook (MyCustomer customer) {

    customer.state = CustomerState.NO_ACTION;

    cook.msgCanYouChangeOrders(customer, customer.choice);

    stateChanged();

    }

  • 8/2/2019 Rest 4.1 Design

    5/19

    The waiter gives the cook an account of the order to be changed

    private void giveFoodToCustomer(MyCustomer customer) {

    DoGiveFoodToCustomer(customer);

    customer.state = CustomerState.NO_ACTION;

    customer.cmr.msgHereIsYourFood(customer.choice);stateChanged();

    }

    The waiter gives food to the customerprivate void clearTable(MyCustomer customer) {

    DoClearingTable(customer);

    customer.state = CustomerState.NO_ACTION;

    stateChanged();

    }

    Data

    private boolean onBreak

    Whether the waiter is on break or notprivate String name

    Name of the waiterprivate List customers

    All the customers that the waiter is servingprivate HostAgent host;

    The host of the restaurantprivate CookAgent cook;

    The cook of the restaurantHost Agent

    Messages

    public void msgIWantToEat(CustomerAgent customer) {waitlist.add(customer);

    stateChanged();

    }

    Customer sends this message to be added to the wait list for eatingpublic void msgTableIsFree(int tableNum) {

    tables[tableNum].occupied = false;

  • 8/2/2019 Rest 4.1 Design

    6/19

    stateChanged();

    }

    Waiter sends this message to let the host know a table is openpublic void msgIWillLeave(CustomerAgent customer) {

    if ( CustomerAgent c in waitlist c.equals(customer))waitlist.remove(c);

    }

    stateChanged();

    }

    Customer sends this message to tell the host that it will leavepublic void msgIWillStay(CustomerAgent customer) {

    if ( CustomerAgent c in waitlist c.equals(customer))

    c.state = CustomerState.WAITING_FOR_OPENING;

    }

    stateChanged();}

    Customer sends this message to let the host know that it is staying

    Scheduling

    if(!waitList.isEmpty() && !waiters.isEmpty()){

    if ( Table t in tablest.occuplied == false) {

    tellWaiterToSitCustomerAtTable(waiters.get(nextWaiter), waitlist.get(0), t);

    return true;

    }

    o If there is an unoccupied table, have the next waiter sit a customer down atthat table

    else if ( CustomerAgent customer in waitlist customer.state !=

    CustomerState.WAITING_FOR_OPENING)) {

    tellCustomerRestaurantIsFull(waitlist.get(0));

    return true;

    }

    o If the restaurant is full and some customers dont know, then tell thecustomer so

    }return false;

    Returns false when there are no waiters or no waiting customersActions

    private void tellWaiterToSitCustomerAtTable(MyWaiter waiter, CustomerAgent customer,

    int tableNum) {

  • 8/2/2019 Rest 4.1 Design

    7/19

    waiter.wtr.msgSitCustomerAtTable(customer, tableNum);

    tables[tableNum].occupied = true;

    waitlist.remove(customer);

    nextWaiter = (nextWaiter+1)%waiters.size();

    stateChanged();

    } Has a waiter send a customer to a table, revises the waitlist and table list, and

    advances a waiter in the list

    private void tellCustomerRestaurantIsFull(CustomerAgent customer) {

    customer.msgRestaurantCurrentlyFull();

    stateChanged();

    }

    Data

    private List waitlist List of customers waiting for a table

    private List waiters

    List of all waitersint nTables

    Number of tablesprivate Table tables[];

    Array of all the tablesprivate String name;

    Name of the hostCustomer Agent

    Messages

    public void setHungry() {

    events.add(AgentEvent.gotHungry);

    isHungry = true;

    stateChanged();

    }

    The GUI sends this to force the customer to being hungrypublic void setReorder() {

    events.add(AgentEvent.wantToReorder)

    stateChanged();

  • 8/2/2019 Rest 4.1 Design

    8/19

    }

    Forces the customer to want to reorderpublic void msgFollowMeToTable(WaiterAgent waiter, Menu menu) {

    this.menu = menu;

    this.waiter = waiter;events.add(AgentEvent.beingSeated);

    stateChanged();

    }

    Waiter sends this have the customer follow to be seatedpublic void msgDecide() {

    events.add(AgentEvent.decidedChoice);

    stateChanged();

    }

    Message sent to self to let the customer know that its decidedpublic void msgWhatWOuldYouLike() {

    events.add(AgentEvent.waiterToTakeOrder);

    stateChanged()

    }

    Waiter sends this message to take the customers orderpublic void msgPleaseReorder(Menu menu) {

    this.menu = menu;

    events.add(AgentEvent.waiterToTakeOrder);

    stateChanged();

    }

    public void msgHereIsYourFood(String choice) {

    events.add(AgentEvent.foodDelivered);

    stateChanged();

    }

    Waiter sends this when the food is ready and to be deliveredpublic void msgDoneEating() {

    events.add(AgentEvent.doneEating);

    stateChanged();

    }

    Message sent to self to let the customer know that its done eatingpublic void msgGoToCashier(CashierAgent c, Bill b) {

    this.cashier = c;

    this.bill = b;

    events.add(AgentEvent.receivedBill);

  • 8/2/2019 Rest 4.1 Design

    9/19

    stateChanged();

    }

    Message from the waiter to go to the cashierpublic void msgYouStillOweMoneyAndMustWork(int amount) {

    this.moneyOwed = amount;events.add(AgentEvent.owesMoney);

    stateChanged();

    }

    Cashier sends this to let customer know that it did not pay enoughpublic void msgYouCanLeave() {

    events.add(AgentEvents.donePaying);

    stateChanged();

    }

    Cashier sends this to let the customer know it can leavepublic void msgRestaurantCurrentlyFull() {

    events.add(AgentEvents.restaurantFull);

    stateChanged();

    }

    Host sends this to let the customer know the restaurant is fullActions

    private void goingToRestauruant() {

    host.msgIWantToEat(this);

    stateChanged()}

    Goes the restaurant when the customer becomes hungryprivate void makeMenuChoice() {

    timer.delay();

    msgDecided();

    stateChanged();

    }

    Takes time for the customer to make a decisionprivate void callWaiter() {

    waiter.msgImReadyToOrder(this);

    stateChanged();

    }

    Calls the waiter to let it know the customers readyprivate void callWaiterForReorder() {

  • 8/2/2019 Rest 4.1 Design

    10/19

    waiter.msgIWantToReorder(this);

    stateChanged();

    }

    Calls the waiter for a reorderprivate void orderFood() {

    String choice = menu.choice(random);

    waiter.msgHereIsMyChoice(this, choice, reordering);

    stateChanged();

    }

    Customer makes a random choice of foodprivate void eatFood() {

    timer.delay();

    msgDoneEating();

    stateChanged();

    } Has the customer eat food and message itself when finished

    private void tellWaiterDone() {

    waiter.msgDoneEating(this);

    stateChanged();

    }

    Has the customer lets the waiter know its done eatingprivate void leaveRestaurant() {

    guiCustomer.leave();

    waiter.msgDoneEating(this);stateChanged();

    gui.setCustomerEnabled(this);

    }

    Customer leaves the restaurantprivate void becomeHungryInAWhile() {

    timer.delay();

    setHungry();

    }

    Hack to make the customer hungry after a period of timeprivate void giveCashierPayment() {

    int payment=0;

    if (bill.cost > money)

    payment = money;

    else

    payment = bill.cost;

  • 8/2/2019 Rest 4.1 Design

    11/19

    cashier.msgHereIsPayment(Bill bill, payment, this);

    stateChanged();

    }

    Gives the cashier the payment that it can affordprivate void workForMoney() {

    while (money < moneyOwed) {

    timer.delay();

    money++;

    }

    cashier.msgHereIsPayment(Bill bill, moneyOwed, this);

    stateChanged()

    }

    Works until enough money is made to pay the rest of the mealprivate void restaurantFullChoice() {

    if (random(1)==1) {host.msgIWillWait(this);

    } else {

    host.msgIWillLeave(this);

    guiCustomer.leave();

    }

    stateChanged();

    }

    Decides what to do when the restaurant is fullScheduling

    if (events.isEmpty()) {

    return false;

    }

    If there were no events, do nothingAgentEvent event = events.remove(0);

    Pop the first eventif (state == AgentState.DoingNothing) {

    if (event == AgentEvent.gotHungry) {

    goingToRestraunt();

    state = AgentState.WaitingInRestraunt;

    return true;

    }

    }

    If the customer is doing nothing and is hungry, it goes to the restaurantif (state == AgentState.WaitingInRestaurant) {

    if (event == AgentEvent.beingSeated){

    makeMenuChoice();

  • 8/2/2019 Rest 4.1 Design

    12/19

    state = AgentState.SeatedWithMenu;

    return true;

    }

    o If the customer is in the restaurant and being seated, it makes a choiceif (event == AgentEvent.restaurantFull) {

    restaurantFullChoice();state = AgentState.restaurantFull;

    return true;

    }

    o If the customer is in the restaurant and is told its full, it makes a choice}

    if (state == AgentState.SeatedWithMenu) {

    if (event == AgentEvent.decidedChoice){

    callWaiter();

    state = AgentState.WaiterCalled;

    return true;

    }}

    If the customer is seated with a menu and finished deciding, it calls the waiterif (state == AgentState.WaiterCalled) {

    if (event == AgentEvent.waiterToTakeOrder){

    orderFood();

    state = AgentState.WaitingForFood;

    return true;

    }

    }

    If the customer has called the waiter and the waiter is taking the order, it ordersif (state == AgentState.WaitingForFood) {

    if (event == AgentEvent.foodDelivered){

    eatFood();

    state = AgentState.Eating;

    return true;

    }

    o If the customer is waiting for food and it is delivered, it eatsif (event == AgenteEvent.wantToReorder) {

    callWaiterForReorder();

    state = AgentState.WaiterCalledForReorder;

    return true;

    }

    o If the customer is waiting and wants to reorder, it reorders}

    if (state == AgentState.Eating) {

    if (event == AgentEvent.doneEating){

    tellWaiterDone();

    state = AgentState.WaitingForBill;

  • 8/2/2019 Rest 4.1 Design

    13/19

    return true;

    }

    }

    If the customer is eating and finishes, it leaves the restaurantif (state == AgentState.WaitingForBill) {

    if (event == AgentEvent.receivedBill) {giveCashierPayment();

    state = AgentState.PayingForBill;

    return true;

    }

    }

    If the customer is waiting for the bill and gets it, it pays the cashierif (state == AgentState.PayingForBill) {

    if (event == AgentEvent.owesMoney) {

    workForMoney();

    state = AgentState.DoingNothing;

    }}

    If the customer is paying for bill and doesnt have enough money, it needs to workif (state == AgentState.PayingForBill) {

    if (event == AgentEvent.donePaying) {

    leaveRestaurant()

    state = AgentState.DoingNothing;

    return true;

    }

    }

    If the customer is able to pay the bill, it will leave the restaurantData

    private String name

    Name of the customerprivate int hungerLevel

    How hungry the customer is, determines length of mealprivate CashierAgent cashier

    The cashier of the restaurant

    private Bill bill

    The customers billprivate HostAgent host

    The host of the restaurant

  • 8/2/2019 Rest 4.1 Design

    14/19

    private WaiterAgent waiter

    The waiter assigned to the customerRestaurant restaurant

    The restaurantprivate Menu menu

    The menu with the choice for the customerprivate boolean isHungry

    Hack for making the customer hungryprivate AgentState state

    State of the customerList events

    List of events for the customerprivate int money

    Amount of money that the customer hasprivate int moneyOwed

    Amount of money owedprivate boolean reordering

    Whether or not a customer is reorderingCook Agent

    Messages

    public void msgHereIsAnOrder(WaiterAgent waiter, int tableNum, String choice) {

    orders.add(new Order(waiter, tableNum, choice));

    stateChanged();

    }

    Waiter sends this to give the cook an orderpublic void msgCanYouCangeOrders(WaiterAgent waiter, int tableNum, String choice) {

    if ( Order o in orders

    o.choice.matches(choice) {o.changeChoice = choice;

    }

    stateChanged();

    }

    Waiter sends this to ask the cook to change orderspublic void msgOrderFulfilled(FoodOrder fo) {

  • 8/2/2019 Rest 4.1 Design

    15/19

    for ( FoodData fd in inventory FoodData fd2 in fo

    fd.name.matches(fd2.name) {

    fd.amount += fd2.amount;

    }

    foodOrders.remove(fo);

    } Market sends this to let the cook know that an order has been fulfilled

    public void msgUnableToFulfillOrder(FoodOrder fo) {

    fo.market++;

    foodOrders.add(fo);

    }

    Market sends this to let the cook know that an order could not be fulfilledActions

    private void cookOrder(Order order) {if ( FoodData fd in inventory fd.amount != 0 && fd.name.matches(order.name))

    {

    DoCooking(order);

    order.status = status.cooking;

    } else {

    List = new List ();

    for ( FoodData fd in inventory fd.amount == 0) {

    foodList.add(fd.name);

    }

    order.waiter.msgTheseItemsAreOut(foodList, order);

    }

    }

    Cooks an order, if there is no more ingredient, tell the waiterprivate void placeOrder(Order order) {

    DoPlacement(order);

    order.waiter.msgOrderIsReady(order.tableNum.order.food);

    orders.remove(order);

    }

    Places an order, messages the cook about it, and removes the order from its listprivate void changeOrder(Order order) {

    if (order.status == Status.pending) {

    order.choice == order.changeChoice;

    order.waiter.msgOrderChanged(order);

    } else {

    order.waiter.msgTooLateToChangeOrder(order);

    }

  • 8/2/2019 Rest 4.1 Design

    16/19

    }

    private void initialOrderFromMarket() {

    FoodOrder fo = new FoodOrder();

    for ( FoodData fd in inventory fd.amount < sufficientLimit) {

    fo.addItem(fd.food, sufficientLimit - fd.amount);fd.amountOrdered += sufficientLimit - fd.amount;

    }

    markets.get(fo.market).msgHereIsOrder(fo);

    }

    Places an order for food from the favorite selected marketprivate void repeatedOrderFromMarket(FoodOrder fo) {

    markets.get(fo.market).msgHereIsOrder(fo);

    }

    Places an order from other markets, to be called if the first failed

    Scheduling

    if ( FoodData fd in inventoryfd.amountOrdered < lowLimit) {

    initialOrderFromMarket();

    return true;

    }

    If a food item is below the low limit and an order has not been sent, order from themarket

    if (!foodOrders.isEmpty()) {

    repeatedOrderFromMarket(foodOrders.remove(0));

    return true;

    }

    If an order was unable to be fulfilled by one market, try other marketsif ( Order o in orders o.status == Status.done) {

    placeOrder(o);

    return true;

    }

    If there is an order that is done, place itif ( Order o in orders o.changeChoice != o.choice) {

    changeOrder(o);return true;

    }

    If there is an order thats been requested to be changed, change itif ( Order o in orders o.status == Status.pending) {

    cookOrder(o);

    return true;

    }

  • 8/2/2019 Rest 4.1 Design

    17/19

    If there is an order that is pending cook it

    return false;

    Data

    private List orders List of all the orders

    private Map inventory

    Map that holds the amount of food leftprivate String name

    Name of the cookprivate int lowLimit

    The limit of food before needing to order moreprivate int sufficientLimit

    The limit of food when it is unnecessary to order moreprivate list markets

    List of marketsprivate list foodOrders

    List of orders sent back from the marketCashier Agent

    Messages

    private void msgHereIsPayment(Bill bill, int payment, CustomerAgent customer) {

    if ( Bill b in billsb.customer.equals(customer)) {

    b.payment = payment;

    }

    }

    Customer calls this to give a payment to the cashierActions

    private void calculate(MyBill bill) {

    if (bill.payment < bill.cost) {

    bill.customer.msgYouStillOweMoneyAndMustWork(amount);

    bill.cost -= bill.payment;

    } else {

    bill.customer.msgYouCanLeave();

  • 8/2/2019 Rest 4.1 Design

    18/19

    bills.remove(bill);

    }

    stateChanged();

    }

    Cashier calculates a payment and responds to the customerScheduling

    if (!bills.isEmpty()) {

    if ( Bill b in bills b.payment != 0) {

    calculateBill(b);

    return true;

    }

    o Cashier calculates a bill if there is a bill that has a payment}

    Data

    private List bills

    List of bills that the cashier is keeping track ofMarket Agent

    Messages

    public void msgHereIsOrder(FoodOrder fo) {

    pendingOrders.add(fo);stateChanged();

    }

    Cook sends this to order from the marketActions

    private void respondToOrder(FoodOrder fo) {

    if ( FoodData fd in fo fo.amount < inventory.get(fd.name).amount) {

    cook.msgOrderFulfilled(fo);

    for ( FoodData fd in fo) {inventory.get(fd.name).amount -= fo.amount;

    }

    } else {

    cook.msgUnableToFulfillOrder(fo);

    }

    stateChanged();

    }

  • 8/2/2019 Rest 4.1 Design

    19/19

    Check the inventory to see if there is enough food to fulfill the order, and respond

    Scheduling

    if (!pendingOrders.isEmpty()) {

    respondToOrder(pendingOrders.get(0));return true;

    }

    If there is an order, reply to itreturn false;

    Data

    private CookAgent cook

    The cook agent of the restaurantprivate List pendingOrders

    List of orders to be completed for the chefprivate Map inventory

    Map that holds the amount of food left