views:

144

answers:

9

What would be the best way to structure orders for a restaurant (available languages are php and javascript)? Since there are multiple tables (the ones you keep things on...), I thought of using objects in javascript. But I am quite new to javascript and absolutely new to OOP, so I'm not sure whether this is the best solution, and whether my design is actually ok. Here is what I have come up with:

var order = {
    id: 0,
    table: 0,
    number_of_items: 0,
    item: {
        name: "",
        quantity: 0,
        unit_price: 0
    },
    total: 0
};
+3  A: 

Well, orders should be independent of tables. Really you'd have a table object that consisted of seat/ customer objects and an order object that consisted of one or more items (with items having name, quantity, price or whatever else belongs to an item).

As an additional complication, an order could either belong to a seat (one person) or a table (e.g., shared appetizer). Supporting that relationship would allow you to do different things when trying to compile the bill for the table (like if you had to provide split checks).

Tom
+1 - Good point about splitting the bill. Your answer is actually more complete in that it should help bring about a better answer, than mine. :)
James Black
I agree with what tom said above. Also who are you writing this for? what are their policies for spiting and discounting items. I worked on a Point of Sale system which was customized heavily for certain customers because they couldn't agree on a single work flow. Discounts was the biggest headache for us, because of how different franchise's required a discount to be reported.
Chad
+1 for treating each item ordered as a separate order. This offers great flexibility. Say what if two unrelated customers share a table? This is perfectly allowed in some countries.
Chetan Sastry
Jazaakallaahu khairun for your input. The point you've made is practical, but it really doesn't apply in this case because one table can only have one order per time.
Mussnoon
+2  A: 

You don't need the number_of_items as you can get that from the item array, so you will want to have an array of items.

var order = {
    id: 0,
    table: 0,
    items: []
};

Ideally you may want to have another class for item and just put a list of them in your items array.

It would look like:

items = [{name: "", quantity: 0, unit_price: 0}, {...}, {...}]

You can get the total by looping through the array and do the math.

Unless the math is overly complex I tend to prefer to not have derivable values stored in the object, but that is just what I do.

James Black
A: 

How about this?

 order = {
     id: <unique identifier>,
     table: <table number>,
     items : <array of objects as declared below> 
 }

 item = {
    item_id: <id of the item in the menu>
    instructions : <text>
 }

Notes:

  • number of items = length of items array.
  • unit price belongs to menu object.
  • total can be calculated any time.
  • Don't forget the instructions (cooking, serving)!
Chetan Sastry
I think your design is lacking a very important component - the quantity of an ordered item. Treating multiple servings of the same item as individual items would add to the complexity I think. Because on a restaurant check, you usually want one item to show only once - with the quantity next to it. Treating them individually is more of a supermarket/grocery store thing. Also, in this specific scenario, there aren't any instructions required. Jazaakallaahu khairun for your input.
Mussnoon
A: 

There are multiple tables, and each table can -over the course of a service- have several orders.

I would think the basic component/object of your system would be the cover itself, consisting of something along the lines of:

order_id, the table, the course (there'll be a minimum of one course), the chosen dish and its cost, the time of ordering (this may not seem important, but it usually is to a -good- maitre d'hotel or service manager) and the drinks.

David Thomas
+1  A: 

I think you can do away with some redundant elements in your structure:

var order = {
    id: 445221,
    table: 42,
    items: [
        {
            name: "Steak",
            quantity: 1,
            unit_price: 15
        },
        {
            name: "Beer",
            quantity: 1,
            unit_price: 3
        }
    ]
};
Ates Goral
I knew those were redundant. But I initially planned to use two arrays for ordered items - "items" and "prices" - instead of an object, and thought keeping a "number_of_items" would just make it easier to traverse through the items. But I guess that's contrary to good programming principles. I prefer to keep "item" singular because, after all, someone really can just order one item. And when using an object, a singular name makes more sense. Jazaakallaahu khairun for your input.
Mussnoon
A: 

In addition to the other answers, what about special preparation directions? Do you want separate Burger items (Burger-medium rare, Burger-well done), or do you want a separate table with the cooking directions linked to that item?

Your answer really depends on your audience. Is it for the screen in the kitchen, for the waitress entering the order, the cashier, the health inspector, or all of the above?

John at CashCommons
A: 

It really depends how you will write your program. If you will not use AJAX, you dont need any javascript structure. I do not use the syntax you use (I prefer building classes in functions) but, item should be plural and must be an array. Remember this, although it seem nice to have some OO stuff, if it adds to complexity it should be avoided. Although we use AJAX in our applications, we do not have Javascript representation of objects, we use only PHP classes and Javascript deals with XML data it gets. Only classes we have in JS are widgets.

Cem Kalyoncu
Why should item be plural...
Mussnoon
i mean there should be more than one item.
Cem Kalyoncu
Thanks, but realistically speaking, someone can just order one item - like just one drink, or one fried rice, or one serving of fried chicken, and so on. So looking at item as always being plural isn't correct in this case, in my opinion. Jazaakallaahu khairun for your input.
Mussnoon
+1  A: 

Here is one way.

function item(name, price)
{
    this.name = name;
    this.price = price;
}

function order(id, table)
{
    this.id = id;
    this.table = table;
    this.items = [];
}

order.prototype.countItems = function()
{
    return this.items.length;
}

order.prototype.getTotal = function()
{
    var total = 0.0;
    for(var i = 0;i < this.items.length; i++)
    {
        total += this.items[i].price;
    }
    return total;
}

var myorder = new order(1234, 12);

myorder.items.push(new item("coke", 1.25));
postfuturist
A: 

I think an identification of objects needs to take place prior to any coding (resist the urge to code). Some objects include (some already identified):

  • table
  • waiter
  • item (pre-defined list of standard menu items along with ability for daily specials and special order)
  • order - grouping of items - could be more than one order per table, a client can have multiple waiters and tables if they move from bar to table, each of those transitions could be handled by paying or transferring the items

You also need a workflow built in (new order, fulfilled, update, closed and paid)...

meade