January 25, 2020
Introduction to Computer Programming – Example – Write an E-commerce Purchase Program

Introduction to Computer Programming – Example – Write an E-commerce Purchase Program

We’re going to finish our study of primitive
data types with an e-commerce purchase. Let’s write a program to calculate the cost of a
purchase for an online sale at Glamazon.com. They’re going to sell three items: mascara
at 8.99, lipstick at 3.99, and eye shadow at 6.89. Taxes are 6.5%, and they’re applied
before shipping, and shipping costs 8 dollars. So the first thing we need to do is to start
Eclipse. You want to create a workspace for the class if you haven’t already. Make sure
that you know where it is. So you could put it in a home directory. You could put it on
your desktop. It really doesn’t matter where it is so much as that you can find it. So
the first thing we want to do is to start Eclipse. When Eclipse starts, it’ll have a
default workspace. Make sure you know where the workspace is. So here’s what Eclipse looks
like what it starts. The next thing we want to do is to create a project. Make sure you
give your project a meaningful name. To create a project you go to the File Menu, and choose
New Java Project. Now project names aren’t very fussy. So I’m going to call my project
Glamazon.com. We wouldn’t be able to use that as a class name because it has a period in
it, and that’s not legal in a class name. We now have our project over here. I’m highlighting
it and choosing the source directory. The source directory is where the Java Files get
saved. The next thing we need to do is to create
a class. All of the Java programs are in a class. Class names start with capital letters
and have no spaces and punctuation. The body of the class is in curly braces. The problem
statement from the PowerPoint is a great comment to start the class. So I’ll grab that on my
way in so that we have it. So I’m copying the problem statement, creating a new class.
There are two different ways you can do this. One is to go to the File menu and choose New
Class. The other way, and the way you’ll probably use the most is right here with the little
green target with the yellow plus sign on it. I’m calling the class Glamazon. Notice
the capital G because it’s a class name, and notice I didn’t put dot com because you can’t
put a period in a class name. As far as the boxes to check, this is the correct superclass.
We won’t know what that means for a while, so don’t worry about it. And we’re going to want to have a main program
created. It’s okay to have Eclipse create your main programs for you, but remember that
when you’re doing tests, you’re going to need to know what those things are. So you might
want to write a few of them yourself. In fact, why don’t I set a good example and write it
this time. So Eclipse will create a class. Now I don’t like the way it really creates
it very well because it does curly braces a different way than I like to do them. I
like to have my curly braces line up on the left. I find that keeps the code neater and
prevents mistakes. Now I’m going to put a comment in that has the data that we captured
from the original PowerPoint. Comments start with a slash asterisk and they end with an
asterisk slash. We’ll do more structured comments later on. For right now I’m going to clean
this up a little bit to make sure that we can see exactly what it looks like, kind of
liked the way that little asterisk looks, so let’s put those in on every line. It’s
extremely important to keep your code neat and well-organized, so you’ll notice when
I’m writing code, I’ve spent a lot of time fussing with little things. I know it looks
silly, but it makes a big difference in how easy it is to work with code in the long run.
So lining up those little asterisks makes it a little prettier, and now we have a good
place to start our program. The next thing we need to do is create the
main program. The main program is inside of the class. Remember the signature for it is
public static void main open parenthesis string square bracket args close parenthesis. The
body is also contained in curly braces, and we indent it to show the structure properly.
So here I am inside the class. I indented public static void main string square brackets—whoops,
one key off—args, and here are the curly braces. Now you’ll notice that Eclipse has
a tendency to go nuts while you’re doing these things and puts up all sorts of little red
boxes. Just ignore them. It’s trying to compile your code before you finished writing it,
which is one of Eclipse’s bad habits, so don’t worry about them until after the lines are
done. You’ll notice we don’t have any problems right now, and you see this in Eclipse because
there’s no little red x’s hanging around over here, and over here nothing is in the red.
So those little red x’s and boxes are ways that Eclipse tells you that you’re making
mistakes. The next thing we need to do is outline the
main program. Now some people when they get in a program get really excited to write code,
and so they forget to think about what they’re doing first. That turns out to be a big mistake,
so what we’re going to do is create comments that describe what the main program is going
to do. Now all of our programs are going to have the same structure, at least for a little
while. We’re going to get some input from the user, do some calculations, and then show
the user some output. So that’s what our comments will basically say. Here’s our first comment.
Now I could just say get input from user, but let’s try to be a little bit more specific
than that. Get the number of mascara, lipstick, and eye shadows that are purchased. Notice
that when I was getting ready to go off the screen that I created a new line. You really
do not want to have code that’s hanging off the screen because it’s hard to debug code
that you can’t see. So that’s the first thing we’re going to do. Then we’re going to calculate the price of
the full purchase, and the last thing we’re going to do is show the receipt to the consumer.
Now notice none of this code actually runs yet, but it does help us get structure, and
it helps us break down the problem a little bit. One of the problems with programming
is it feels like there’s so much that you need to do, and you really do want to start
a little bit at a time instead of getting overwhelmed by that. So this is a way of breaking
it down. This actually has a name in computer science. It’s called subsequent subdivision.
I know it’s a big word, but it’s a way to think about it. The next thing we need to do is to make decisions
about our data. We need to select identifiers and types for data. So we know we’re going
to need to have the number of each of three items ordered. Now all of these items are
things that are countable. You could order one mascara or two mascaras or three, but
you can’t order 2.5 of them. So that tells us what the type should be. It should definitely
be an int. And there should be a cost for each item. Now this one turns out to be a
little bit trickier. The cost could be a double or could be an int, depending on whether we’re
storing the cost in pennies or dollars. It doesn’t really matter which one you choose,
as long as your pay attention to which one you’re using and make sure you document it
correctly, so you know which one you’re using. So let’s create it in double. That means we’ll
be storing things in dollars. We also need the name of each item. The names of items
are created of characters, and so those should be stored in a string. The tax rate, on the
other hand, has a decimal point in it, so that should be a double. And the shipping
cost–now this one’s a little bit tricky because our current shipping cost is eight dollars,
so it might be tempting to think, “Oh, that doesn’t have a decimal point in it, and so
we should make it an int.” But if you know anything about online retailers, you know
that they’re going to be changing their costs, so tomorrow shipping may be 7.99 or 9.99 or
3.99. So you want to make it a double so that the program can be changed if it needs to
be later. The other thing we’re going to need to keep
track of is the total cost. That’s money, so that’s going to be a double too. So now
we have a whole bunch of decisions made about data that we’re ready to transfer to the program. So let’s write some code. I’m going to do
one item first, so let’s do our mascara. We decided that the number of mascara needed
to be an int. We decided the name of mascara needed to be a string. The price of mascara
needed to be a double. Our total price needed to be a double, so let’s start our total price
at zero because we haven’t purchased anything yet. Now I could put the number of lipsticks,
and eye shadows, and the names, and all those in at this point, but let’s wait instead and
work with a little bit of code before it gets too overwhelming. We also needed variables
for our tax rate. You’ll notice that when I’m creating these constants like 0.065 that
I always put a zero before the decimal point. That’s a habit. It doesn’t have to be done
that way. The computer will understand it fine without the zero, but it is a way of
communicating to other people who are reading the program that this is a double instead
of an int. Little things like dots are easy to miss in code, so you’re always trying to
make the code easier to read. The other thing we needed was our shipping cost. Once again,
I could’ve said just eight, and the program would’ve worked fine, but because I made it
a double, I put 8.00 in it. Now notice I’ve missed a space there, and I missed another
space up above. I’m going to go back and fix those things, so that my code really looks
nice. Okay, so we’re going to continue with our
strategy to work with one item first, and then we can copy and paste code. The other
thing we want to do while we’re in our code is to create the input. Now this is going
to use a scanner object. Remember we need to prompt the user because they don’t know
what we’re thinking, so let’s go and write that code too. So that code should go right
here where it says calculate the price of the full purchase. So we’re going to prompt
our user, system out print line how many, and then mascara–remember mascara is a string
variable, so we need to use pluses to tell it to concatenate with how many–do you wish
to purchase. Now the next thing we need to do is to get input. I like to leave my scanner
objects up at the top of the class because they get used a lot. So scanner input is new
scanner dot system dot in. Now notice that that’s underlined in red right now. The reason
that Eclipses isn’t happy with that is because it doesn’t know where to find the scanner
class. The scanner class is not part of Java dot Lang where a lot of other classes are.
So we have to give Eclipse instructions. We do that up here by telling Eclipse to import
Java dot util dot scanner, and notice Eclipse is happy again. So let’s go down now and get the number of
mascara from the user. So number mascara equals input dot next int. Now the reason I used
next int instead of next double is the number of mascara is an int. Now we could run our
program at this point, but we really wouldn’t know if we had been successful. So one thing
we might want to do is to do just a little bit of output to check. We have more sophisticated
ways to debug, and we’ll use those later, but for right now the important thing is to
make sure that our input is coming in correctly. I know it looks like I’m being very cautious,
and I am, but this actually saves time and programming to be cautious. Now we’re ready to run the program. That’s
this green arrow button up here. How many mascara do you wish to purchase? Let’s get
one. We saw one as output. Let me raise my console window up so you can see all of that.
So the green one was the character I input, and the one was the echoing it. We can see
that that was done correctly. The other thing that I noticed is that I have a question here,
and I didn’t put a question mark on it. That looks a little bit odd, so let’s fix that
in our code too. Now one of the things that happened when we did that–you may notice
my code is starting to shift back and forth. That’s because this line has gotten a little
bit too long, so let’s break that line into two pieces. The next thing we want to do is do some output.
Now we did a little bit of output to check that our input was correct, but I’m talking
now about the final output from the program. So let’s write some code. Now if I was the
user, what I would want to see here is not just how much money I owe, but I’d like to
see my purchases echoed to make sure that everything is being done correctly. Let’s
show the receipt to the customer. Now we haven’t done most of our calculations, so we are just
going to show the number that were ordered, so that will be number mascara—whoops, and
I was about to type in mascara and then I remembered that we have a variable for that.
There’s going to be something that goes wrong here if we continue on this path. Once we
type in number of mascara, remember that’s an integer, and mascara is a string that doesn’t
have a space in front of it. So if we type those in the way they are right now, what
we’ll see is the number one will be right in front of the word mascara, and that won’t
look pretty. So let’s put in an extra space now. The other thing we might like to put
in is a period to end the sentence. So that will be after mascara, and there we go. So let’s run the program now. Give you a little
more console, so you can see. How many mascara do you wish to purchase? One. You ordered
one mascara. Looking good. The next thing we need to do are the calculations, so we
can add up the cost of the three items. Now at this point, we don’t have three items in
there, so now it’s time to—okay, I see I made a mistake here. Do you notice that I
put the input statements where it said calculate the price of a full purchase? That’s something
we need to fix now. That should’ve been up above there. You’ll notice these little things
when you’re programming, easy to make lots of mistakes. While we’re doing that and copying
and pasting that into the right position, let’s do it two more times so that we can
get our data for other items. Now this was just debugging output that we put in. We don’t
need that anymore. Would’ve been wise to remove that, by the way, before we had copied and
pasted it three times. Let’s get that out of there, and go back and create our variables. So we’ll have an int for number of lipstick.
We’ll have a string for lipstick, and we’ll have a double for the price of lipstick. And
I don’t remember what lipstick happened to cost, so let’s go back up and take a look.
Notice this is where it’s really nice to have these comments at the top, so we can just
go back and look and not have to go back to the PowerPoint. So 3.99 is lipstick, and while
we’re at it the eye shadow 6.99, so we should probably remember that. So the last thing
we need is the number of eye shadow. So we’ll have a string for eye shadow, and a double
for the price of eye shadow. Notice that I’ve left a space between each group of variables.
I do that to make it a little bit easier to read the code. So you don’t want to leave
too many spaces because then you don’t see much code on the screen, but you also don’t
want to just smash everything together because then it’s hard to read. So now let’s go down
here, so instead of mascara, we’ll ask about lipstick next, and then we’ll go down. Notice
I’ve got an extra line there. I’m going to remove that to keep the code looking neat,
and we’ll look for eye shadow. Okay, so now we’re ready to calculate the
price. Total price is going to be the number of mascara times the price of mascara plus–and
I think maybe I’ll put that on another line because I think we’re going to run out of
space. Notice that I’m indenting it far enough so that it’s still to the right of the equal
sign. So we’ll have the number of lipstick times the price of lipstick. Don’t worry about
the fact that Eclipse is going nuts at this point with the red lines. And we’ll have the
number of eye shadow times the price of eye shadow. So that’s the total price. Now that
we’ve finished this line, we can see that Eclipse actually has a point. I did make a
typo there, and Eclipse is happy once again. Probably it’s a good time to run this program.
So let’s print out the total price now. Oh, I didn’t want to do that. That was all that
nice input that we had just put out, so we don’t want to get rid of that. Let’s just
print out system out print line the total cost of your order was plus total price. Now
we see that Eclipse has a little red bar there, so we might go and see—ah, once again Eclipse
is right. I forgot something there. I forgot to put a semicolon in. Those things make a
big difference in programming. So Eclipse is happy. We can now see this because there
are no little red bars over here and no little red x’s over here. Don’t worry too much about
these yellow things. What it’s telling us is that we haven’t used these variables yet.
We actually knew that, so that isn’t a problem. Let’s run the program. How many mascara? One.
How many lipstick? One. How many eye shadow? One. You ordered one mascara. Now the reason
it’s not showing all of them is that we didn’t print those out. And the total cost of your
order was 19.87. Okay, so that looks about right. Let’s go
back and fix our output. The other thing that I notice–it’s a little odd that we have capital
letters in here. So let’s go back and fix those too. So let’s make the L in lipstick
small, the E in eye shadow small, mascara should be up a little bit. So let’s make the
M in mascara small, and go down and echo our other inputs. So here’s the number of mascara.
I’m pasting it twice. Whoops, I’m pasting it in the wrong spot too. We want those to
be before the output, not after it. It’s a really good idea to cut and paste these lines
because that way the formatting is exactly the same. We’re going to do number lipstick
and lipstick here, and number eye shadow
and eye shadow there. And Eclipse is unhappy because I forgot the capital S in eye shadow. Okay, so why is Eclipse unhappy about number
of lipstick? Well, if you look up in the formula above, you’ll see that the S in stick wasn’t
capital. So there we go, and once again we have restored order to Eclipse. So let’s run
our program. This time let’s buy two mascara, two lipstick, and two eye shadow. Okay, so
things are looking good, and we’re getting close to having this program finished. So
the next thing we need to do is to add the tax. An important thing to consider is what
to do about the partial cents because there’s no guarantee that the tax will work out perfectly
to a penny. Once we’ve calculated the price, we know that we’re going to have to calculate
the tax. Now the reason I’m creating a variable for this is because users usually want to
see how much tax they’re paying, particularly on an e-commerce application since it can
be different depending on what state they live in. The tax is going to equal the total
price times the tax rate. Now let’s think about that for a minute. First off, let’s
make sure that our tax rate was done the correct way. So our tax rate is .065, which is in
fact to 6 and 1/2%, and so that should be multiplied by the total price. But at this
point both of these are doubles, and so we have to worry about what to do with possible
half cents. Now when you’re doing things like this commercially
as a software engineer, you need to be careful because very often states will have specific
rules about just exactly how tax calculations are supposed to be done, so you really don’t
want to break those rules. But, you know, it’s just a Java course. We’re going to be
okay. So let’s just round up to the nearest penny. To do that, we’ll do tax equals tax
plus .005. If you think about that for a minute, you’ll think that is just adding half a penny
on. At this point we’ll be able to truncate, so we will have our tax equal to tax times
100. That gets it up to pennies, and then we’ll make it an int, and that truncates it
to an even penny. Now there are other ways that we can solve this problem, we just don’t
know the Java yet. We’ll get to them later. We certainly now want to tell the user about
the tax. So that should probably come between their order and the total cost. So system
out print line your tax was plus tax. The other thing we might want to think about is:
have we added the tax into the total order or not? If you look up here you’ll see we
calculated the tax, but we didn’t add it in. So let’s add it in now. Total price equals
total price plus tax. Now let’s run the program. One mascara, one lipstick, one eye shadow
our tax was $129. That seems like a lot of tax on a purchase that’s 18 bucks, so let’s
see what went wrong. Well, if you look in the program, you’ll notice that we multiplied
by 100, but we forgot to divide by 100 too. So the reason we multiply by 100 was to put
the price in pennies. Then we could truncate those half cents so we knew we were handling
our tax correctly, but then we have to move it back into dollars, so it’s a complicated
calculation. Let’s run it again. One mascara, one lipstick, one eye shadow—ah, $1.29.
That looks like a little bit more reasonable tax rate. The last thing we need to do is to add in
the shipping cost. Once again that’s something that users are going to want to see spelled
out on their final receipt, so we need to remember to include it there too. Now our
shipping was just a flat rate, so our total price equals total price plus shipping. One
thing I noticed looking at the code now is it would be a good idea to add in a few more
comments. I see that I’ve forgotten the name of our variable shipping too, so we may want
to look at that while we’re going up. I probably called it something like shipping cost. Yes,
I did, and that’s a better name. So let’s add in some comments, like calculate the tax
rate, since that’s what we were doing. Add the tax to the price. Add the shipping. Now commercial software engineers actually
are supposed to write as many lines of comments as they write of code. In fact, that’s a professional
standard. We don’t usually do that in little programs for class, but you can think of that
as being an ideal. So it’s very unlikely that you’re writing too many comments. Okay, so
here’s our tax. Let’s tell the user about the shipping. This time I’ll remember that
we called it shipping cost, and there we go. So let’s run the program one last time. One
mascara, one lipstick, one eye shadow, tax was $1.29, shipping was 8, and the total cost
was 29.16. Now you’ll notice that our shipping is shown as 8.0 instead of 8.00. That’s a
problem that we have with print line. We’ll know how to fix that later, but for right
now we don’t. It’s technically correct; it’s just not very pretty. So our program is almost
finished now. So we’ve tested the code, but we only tested it once, and running a program
once is really not enough. So the first thing we should do is try testing it by buying zero
of each object. This is something called a boundary condition. Things tend to go wrong
in computer science at the edges, and so we test the boundaries very carefully. Now before
we run the program, we might want to think about what we’re expecting to see from this.
You have to be a little careful here. In our program right now what we’re going to see
is that if we buy zero of everything, we get eight dollars of shipping. That’s kind of
unfortunate, but it just has to do with the fact that we don’t know how to avoid that
yet. Still, let’s see it in the code. So we’re going to run again: zero mascara, zero lipstick,
zero eye shadow, and we end up paying eight bucks. That’s probably not going to make customers
very happy, but then again we’re not quite ready to write commercial software just yet.
We tried the case where we bought one of each object, but let’s try just buying one of each
object one at a time. That’s a good boundary case too. So let’s buy one mascara, no lipstick,
no eye shadow. 17.57. That mascara got expensive. That’s what eight bucks of shipping will do
for you. Now let’s buy no mascara, one lipstick, no eye shadow. 12.25. Remember lipstick was
a lot less expensive. And now no mascara, no lipstick, one eye shadow. 15.34. So things
are looking good. Now you would certainly want to take your calculator and make sure
that those numbers are correct, but I checked them. They’re fine. Maybe we could buy one, two, and three of
the objects too because we really haven’t tested whether numbers greater than one are
working correctly. So one mascara, two lipstick, three eye shadow. 48 bucks–pretty hefty purchase,
but then again it’s a lot of makeup. Now here’s an interesting question. How can we test whether
the tax is rounding properly? Because our numbers have been odd here, it’s a little
hard to tell, but we did something really smart. We made our numbers variables, so let’s
change one of them to an even number where we can really easily tell if the tax is being
worked with correctly, like for example $10. So we’ll go back to our code and let’s change
the price of mascara, way back up at the top to $10. Now at this point we know exactly
how many cents we should get. We should have 65 cents of tax added in. So let’s run our
program. Just buy one mascara, no lipstick, no eye shadow, and we get 18.65. So that was
10 dollars for the mascara, 65 cents for the tax, and 8 dollars for the shipping. So that
works too. Oh yes, before we go back, one of the things you want to make sure you do
when you do that kind of testing is that you restore your program to its original state.
That’s easy to forget, and that actually leaves you with an incorrect program. Here’s my advice on implementing programs
in general. First off, get started early. A lot of people get into programming trouble
because they start the night before something is due, and then if something goes wrong,
they can’t get help. There are a lot of things that can go wrong in programming, and it helps
if you have the time to get some support. Notice that I write just a little bit of code
at a time. I don’t write hundreds of lines and then try to fix it. I also keep the code
neat and organized at all times. Indentation is especially important, particularly as we
go on. And I use meaningful variable names. That way I know what things mean in the program.
And I test as soon as possible, and I test as often as possible. Do those things. You
should have a lot of success programming too.

1 thought on “Introduction to Computer Programming – Example – Write an E-commerce Purchase Program

Leave a Reply

Your email address will not be published. Required fields are marked *