Saturday, September 13, 2014

#Letspractice C# CarpetCalculator




Lets talk practice. I am going to break down a simple coding exercise that I found in a tome of forgotten lore(It is most definitely not “C# programming from problem analysis to Program Design(fourth edition)”).

I will try to cover as much ground as possible. Basically almost everything presented in the second chapter and then some. I will borrow some knowledge from Shawn Sommer, he wrote a great post called everything-isnt-int. We will use the theory he exposed to make our program as light as possible.

So basically we are designing an application to determine what it would cost to put new carpeting in a single room. Single room size:

Length = 12 feet 2 inches.
Width = 14 feet 7 inches.

We can choose from two carpet options. They are:

Berber(best) @ $27.95 per square yard.
Pile(economy) @ $15.95 per square yard.

The question is, what will each selection cost? Before we code we need to analyze the data, and plan our approach. What do we know: We are given feet and inches(whole numbers and integers) to work with but the price(number with decimal part, or “double” or “float”) of the carpet comes per square yards.

To determine square yards we first must calculate square feet. To do that we first convert all our feet and inches to feet. The book tells us that we can use int for all the values but we know better because we have read everything-isnt-int. Right? SO we scrap that and we do it our way.


For now lets focus on what values are constant. We know that there are 9 square feet in a square yard, it's because 1 yard is 3 feet and in a square all sides are equal in length, which makes 1 sq. yard equals to 3 * 3 = 9 square feet.(This data came from here).
So that will be a constant, we can give it a nice descriptive identifier and we are done: (Remember the book says this are integers but we know that we don’t need any more than a byte to identify this constant)


Now we do the same for:


We also know that the best and worst carpet have immutable names (for this example) so we can call them constants as well:

Now it would be a good time to think about and craft a flowchart. The more planning we do the least debugging we will have to deal with later. Here is a picture of my flowchart: It is the same one as in the book but this one I made myself using gliffy.



CarpetCalculator is only going to be 1 class that executes right then and there so we could also do a class diagram but we do not absolutely need it and this post is long enough as is. Alright now we are ready to start the coding. Refer to this post if you have never ever created a C# Console Application on Visual Studio before. We start by: ---
--- Because this program has only one class “CarpetCalculator” evertyhing will happen inside this Main() the second step is to define our constants or “const” ---
--- So far so good, notice how we did not write ---
--- because 12 fits just fine into a byte and we know it won’t change. Lets continue. Now we look at our flow chart to get an idea of what other variables we need to define. ---
--- Same as before, ushort instead of byte. I did this just to show another type of numeric value we can use to define our variables. I am sure you know we could've used byte. Now we define all our floats. We use floats instead of the default doubles because floats are 32 bits in size instead of 64, and they are precise up to 7 digits which is more than enough for this exercise. ---
--- It is time to basically follow the flowchart step by step now: ---
-- Mind about how we cast the roomLengthInches & roomWidthInches with a prefix that looks like this (float). And also pay close attention to the suffix we used on carpetPrice. We do this because the default floating point type is double & we must let the computer know 27.95 is not a double but a float. If we don’t we will get a syntax error. Now to finish up the code add this part: ---
--- Here we are formatting a string for the first time: ---
--- Because we are going to end up with a number with several decimals and we only care about how many cents that would be we need the decimals at only two digits. To do this we use “{0:c}”, totalCost totalCost will come up looking like dollars and cents, making a lot more sense to the user. n\n\ are escape operators for making sure the info is not display right on top of the previous data. And now we are finally done. If we run the program we should see this:

       Total Cost of Berber is $551.02
       
       Total Cost of Pile is $314.45  

And that basically does it. Our code looks professional and it is very light! Here is all the code at once:

--If you have any questions leave a comment in the section below I will get to it as soon as posible. Cheers


Share This

No comments:

Post a Comment

Contact Me

Do you need to get in touch with me? Shoot me an email!



Made with ♥ in Milwaukee Inspired By Blogger Templates