C/structures programming
Expert: Zlatko - 1/26/2011
Questionhi mr zlatko..
first of all, i would like to thank you for helping me score
full mark in my previous c programming assignment..
now i m in the 2nd part of programming which is data structure, and my current assignment is to create a breakfast billing system where the output as below:
Welcome to HiFi’s Restaurant
1 Bacon and Egg $3.45
1 Muffin $2.20
1 Coffee $1.50
Tax 5% $0.35
Amount Due $7.50
so i need some references such as website or related question to do this assignment. i hope u can advise me on this.tq.
vanan
AnswerHello Vanan
I'm glad I could help you last time. This time, it is not clear to me what you are having trouble with. I can guess that you need to store the customer's order in some sort of data structure, and then go through the data structure, print out each item in the order, and calculate the total. If that is correct, then the simplest way to do it is to create a struct that holds the elements of an order. For example:
struct order
{
int quantity; /* the amount of muffins, or coffees bought */
char product[32]; /* the description of the item */
float cost; /* the cost of one item */
};
Then you have the problem of storing the order in some data structure. It could be stored in an array of order elements, or it could be stored in a linked list. If you decide to store it in a linked list, the simplest way is to expand the order structure to include elements for the links. For example:
struct order
{
int quantity; /* the amount of muffins, or coffees bought */
char product[32]; /* the description of the item */
float cost; /* the cost of one item */
struct order* pNext; /* for a linked list, a pointer to the next item */
};
My design so far is not a very good one. It is poor because there is a lot of repeated information. If customer A orders a coffee, and customer B orders a coffee, you have two orders with coffee and it is easy to make inconsistencies. In one order it might be spelled koffee, and cost $1.40 and in another order the server may have input Coffee, and priced it at $1.60, when the real price is $1.50. A better design would have a "master" container of menu items and the customer's bill references the master menu items. For example, the master menu may be on a linked list using this structure:
struct menuItem
{
char product[32];
float cost;
struct menuItem* pNext; /* pointer to the next menu item */
};
or, the master menu items could be in an array, instead of a linked list, since they are fixed for the duration of the program run.
Then orders items would reference the master menu items with a structure like this:
struct order
{
int quantity; /* the amount of muffins, or coffees bought */
struct menuItem* pItem; /* a pointer to the coffee or muffin menu item */
struct order* pNext; /* the next element in the order */
}
In this design, the master menu items are constant for the duration of the program, while customer orders are created and destroyed as customers arrive and leave.
Of course, once you decide on the data structures, you will need to write code to handle them, so we are only on the first part of your project.
The second design is a little better than the first one, but it still has a problem. Notice that the orders and menu items are set to be held on a linked list by having the pNext pointer directly inside them. We can say that the order items "know" that they are being stored on a linked list. The advantage is that it is a simple design, but it is not the best design. However, before I tell you how to improve this second design, I want to know if my explanation is making sense to you. Also, I want to see that you are reading my answer. Many times I put a lot of effort into an answer and people don't even read it.
Please ask me more follow-up questions if my explanation is not clear, and if I have completely misunderstood your question, then you will need to make it clearer.
Best regards
Zlatko