hey guys, its been a while since i did some c++ programming and i thought i'll get back into it. I set myself a task to make a restaurant/bar type billing system. In otherwords you can make orders at a table, and save them in the computer. Maybe later print them out etc.
Here is my approach.
I consider this problem as 2 classes.
Class Order:
attributes:
orderId
orderName
etc...
getter setter functions bla bla
Class Table
attributes.
tableId
Order myOrders.<<<<<<<<<<<<<this is the tricky part for me
I understand that in order to create a system which can add/remove orders from a table, i will have to pop/add elements from time to time(maybe there was a mistake, wrong input etc...)
So, i first thought, an array, but then remembered that popping an element from an array is time/power consuming.
So, i'm thinking of doing a linked list. I remember working on those in university a while back in C (struct, pointers to next prev, head,tail)
I understand the theory, but could someone please give me a heads up on how to do this in c++?
When should i instantiate the list? in the table constructor? how to do get/set methods for it? i don't want full answers, but pointers to the right direction would be perfect. If i get full answers, i won't learn anything, will i? :)
my plans are afterwards to use Qt to create a GUI interface. I've had experience with Qt before, but that's another story.
Oh and do you think i should use the vector library? or should i create the list myself?
I would definitely look at the STL container classes. It sounds like the list class would work well for your idea. They're easy to use too! So, on this site, look under reference->STL Containers.
Also, the STL Containers are templated meaning that they will work with any class as long as certain criteria are met -- such as comparison methods, etc. It should be fairly easy to understand, but if you get stuck, always post back!
OH MY GOD... in c++ we don't have to define the linked list ourselves? all the operations are already there :) I forgot this pleasure. I remember doing this all manually in C with structs. woop woop :D
I wanted to come back and say, i managed to do it.
i now have:
Table:
addOrder
removeOrder(index)
printOrders
//it took me some time to understand how to use the iterator. but all in all it was a piece of cake
Order:
getOrderId etc....
i'm now thinking of first creating a distributed system this way, and only later going for the design.
This way, if ever the bar/restaurant has multiple computers, they can all work together.
Also, i reckon storing the files to a database would be a better idea. If for example there is a power outage, this way, all the files are still stored. i reckon mySQL? i could also do it as a text file and remove/add entries into it. But i believe that would take forever right?
About the db part, i see it as this:
void addOrder(order o_index){
//i dunno the sql syntax through c++, but something like this
insert into orderTable
(
Select name,price,index
from products
where index=o_index
)
}
void removeOrder(o_id)
remove o_id from orderTable
}
or something like that.
Is this approach the good one? or is it better to do it in another way? i am open to all suggestions
I use MySQL and it's pretty powerful (especially because of the price!) I think for a distributed system like this a database is defiinitely the way to go. Make sure you study up on security especially if you want to web-enable your system (SQL Injection, etc.)
I work in the credit card industry and security is one of the most important issues we deal with everyday. So let me say it again: If you plan on putting this on the Web, study the security end of things very well!!!
That said, don't think it's too hard. It's just more to learn!
Thanks for the advice, ill go read up on sql and c++. about security and that stuff, i had a course on it last semester :) i'll read into it. Thanks for all your advice
alright, i looked it up, and i know the general idea.
I need to be running a mysql server.
I need to have a driver to the server.
I need to configure my ide to link the libraries.
Please advice on how to continue :)
Step 1.
I'm lost :)
Step 2.
no matter how much i search on google, i keep failing.
i have the code to do the connection to the database, but for now i get the following error.
cannot find library mysql.h (i havent done the linking).
I use codeblocks, im running on windows.
a step by step setup for the mysql would be great.
Documentation:
http://dev.mysql.com/doc/
Setups for MySQL on Windows:
http://learn.iis.net/page.aspx/610/walkthrough---set-up-mysql-51-for-php-applications/
http://www.topsourcecode.com/index.php/mysql-tutorials/51-step-by-step-install-mysql-to-windows-tutorial.html
Cheat Sheet:
http://www.addedbytes.com/cheat-sheets/mysql-cheat-sheet/
i figured out how to do everything before reading your reply, thank you anyway, i will keep that cheat sheet. Very usefull indeed.
I seem to be doing something wrong here: in the constructor for my Table class, i want to create a new table in the db, that way for each table(wooden thing with legs we eat of) there is a table(from the db). Easy to print out etc.
Now, here is my code, and i seem to be missing something trivial, i'm sure
1 2 3 4 5 6 7 8 9 10 11
Table::Table()
{
MYSQL *connection, mysql;
MYSQL_RES *result;
MYSQL_ROW row;
int query_state;
connection = mysql_real_connect(&mysql,"localhost","root","asdf","bar",0,0,0);
mysql_query(connection,"CREATE TABLE example(id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name VARCHAR(30), age INT)");
//ctor
}
everytime i run this and instantiate new Table(). i get bar.exe has stopped working
I'm not sure why you are getting this error; however, creating tables is usually a static activity -- You design your tables, then build them with scripts (usually.)
What you usually do dynamically is what we database programmers lovingly call CRUD ("Create", "Read", "Update", "Delete".) This involves the records that are in the previously-created tables.