CHAPTER 5: CASHING IN YOUR NUTS



We've reached the point where you will now be working on your first project to apply what you've learned to a real-life situation. Now, it may seem silly to program a cash register, seeing as they already exist, but it's important to know how these things work before you try to invent something new. And who knows; you may just make a game that needs cash registers!
PROJECT 1: CASH REGISTER

The first thing our register will need is a variable to store the total in, so start by creating one. I would just call it total but you can name it whatever you want.

Next, create a function for retrieving prices, and call it getPrice(). Give it two arguments, sItem and nAmount. Inside the function, create a switch statement that checks sItem against a case for each item you sell. Give bananas a price of 2.25$, onions for 1.50$, bread for 3$, and fish for 2.75$. Each case should return the value of the item times the amount being bought.

Test your function. Print the value of 5 bananas. The price you should get back is 11.25$.

Very good. Now, we'll be using getPrice() for more than just looking up items; it will also be used for adding items to the total, and voiding them. For now, add a function with the same arguments called add() and have it add the result of getPrice() to the total. Once you have that, create a function called printout() that takes no arguments. It should print the total plus a 5% tax.

Now that we have all this, let's do a practice transaction. Using add(), ring up one banana, two onions, 4 fish, and two bread, and ignore how bizarre that shopping list is because it's not your job to judge what your customers eat. Finally, use printout() to determine the total after taxes. What do you get? 23.3625$? That's not right! We need a way to round this to the second decimal place, but squirrel has no rounding function!

Time to add another function, this one being called round which takes two arguments. The first argument will be the value to be rounded, and the second will be the amount it's rounded by. If you don't know the math, that's OK; I'll give you the formula: return (x.tofloat() / y + ((x > 0) ? 0.5 : -0.5)).tointeger()*y;. Now you can modify your printout() function to round prices to the second decimal point by passing 0.01 as the second argument.

Now we can display our prices correctly, but, uh-oh! The customer wants to return one bread. We'll have to find a way to keep track of sales so we can go back and fix things. Create an array called history. In add(), modify each case so they push an array to history containing the item and amount last registered. Once you have that set up, create a function called voidLastItem(), and in it, have it check the length of history. If it's less than or equal to one, set the total to zero and pop the last value, otherwise, pop the value into a variable, and subtract that variable from the total.

Now call voidLastItem() for every item entered, and print it out. You should get zero. Run it several more times, and make sure that it does not try to take a value from an empty array. If it does this, you will get an error.

That's it! Of course, a real register would have some way of interfacing with it from outside the code, but we'll be learning how to do that part later on. For now, though, just be happy that you've made yourself a working cash register. You're on your way to becoming a Squirrel coding master!

Are you enjoying this tutorial? If so, why not send a donation? You can do so by clicking the donate link at the top of the screen, and if you do, please attach a note to your payment letting me know it's for the tutorial!

<< PREV | NEXT >>