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
};
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.
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):
You also need a workflow built in (new order, fulfilled, update, closed and paid)...
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.
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:
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.