可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
};
回答1:
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
}
]
};
回答2:
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).
回答3:
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.
回答4:
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));
回答5:
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)!
回答6:
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.
回答7:
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?
回答8:
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)...
回答9:
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.