Wednesday, October 22, 2008

C# Tutorial - Jewelry Pricing Calculator


Click the link to download the source code for this post

Licensing and Warranty

You may use the code as you wish - it may be used in commercial or other applications, and it may be redistributed and modified. The code is provided "as-is". No claim of suitability, guarantee, or any warranty whatsoever is provided. By downloading the code, you agree to defend, indemnify, and hold harmless the Author and the Publisher from and against any claims, suits, losses, damages, liabilities, costs, and expenses (including reasonable legal or attorneys' fees) resulting from or relating to any use of the code by you.




One of the things that I do on the side is make jewelry. In fact, I'm in the process of starting a jewelry business. In order to sell the jewelry, I need to determine what to charge for each piece. I decided to make myself a tool (using C#) that I can use to find out what to charge for jewelry.

In a nutshell, users of this application enters the cost of materials, the hourly rate, and the number of hours it took to make the piece. Once that information is entered, the tool will calculate the minimum price to charge and the maximum price to charge based on an equation used for determining MSRP (Manufacturer's Suggested Retail Price).



The application also has checks to make sure that the user entered decimal values in the fields, and since this is a multi-form application, it has checks to make sure that the form is closed before it attempts to open the form.

The application uses a variety of different components, such as:

  • String formatting

  • Toolbar and menu

  • Timer

  • Rich Text Box



Here are some code snippets highlighting some of the things that the application does.
Using the Timer to make a "rotating banner"
On the main application form (Form1.cs), I created a "banner" displaying directions on how to use the application. In order to do the rotating banner, I used the timer control, the label control, and a string array containing the messages.
1) I defined a label on the form (called lblHelp).
2) I added a Timer object on the form. I configured the following properties:

  • Enabled = true

  • Interval = 5000 (5 seconds)


3) In the program (line 14), I defined a string array containing the messages. I also defined a variable to keep track of the current position.


int pos = 0;
string[] textToDisplay = new string[] { "Enter the total cost of materials used for the project. For example, if you purchased a lot of 20 beads for $5.00, and you used 10 beads, the total cost would be $2.50", "Enter the hourly wage for the person who made the jewelry", "Enter the total hours it took to make the project. Round up to the nearest 15 minutes. For example, if the project took you 38 minutes, round it to 45 minutes.", "Select Action - Calculate from the menu, or click the calculator button at the top of the screen." };


4) In the Tick event of the timer (line 185), I added the following code to change the message in the label:


pos++;
if (pos == textToDisplay.Length)
{
  pos = 0;
}
lblHelp.Text = textToDisplay[pos];


In this snippet, I am changing the position of the array. In order to prevent the application from "crashing" because the position of the array is bigger than the array, I check to see if pos is equal to the number of elements in the array (textToDisplay[pos]). If it is equal, then I reset the position to 0. Remember that arrays in C# are base 0 arrays, which means that the first element in the array is position 0. Finally, I set the value of the label to the value in the current position of the array.

Getting the Application Version
One of the things that I wanted to display in the about form (frmAbout.cs) was the current version of the application. Using the System.Diagnostics.FileVersionInfo class, I can get the version of any file. In this case, I want to get the version of the executable file that's created from this project. Here is the snippet (line 17):


System.Diagnostics.FileVersionInfo thisVersion = System.Diagnostics.FileVersionInfo.GetVersionInfo("JewelryPricingCalculator.exe");




Alternative to using the foreach loop for checking whether a form is open in C#
Forms in VB .NET have a slick property called Visible that will indicate whether a form is visible (open) or not. Remember that Microsoft developed the .NET architecture for flexibility. If you import the Microsoft.VisualBasic.dll in your project and you make a reference to the dll (using Microsoft.VisualBasic;), you can utilize the Visible property in C#. However, with C#, you still have to define an instance of the form. Here is an example:


Form2 f = new Form2();
if (f.Visible == true)
{
  f.Focus();
}
else
{
  f.Show();
}




Checking to see if a form is already open
Since this is a multi-form application, I wanted to check whether the form is already open before I attempt to open the form. In order to do this (in Form1.cs), I use a foreach loop through the Application.OpenForms object. Here is the code snippet (line 164) determining whether the help form is already open:


bool helpOpen = false;
frmHelp helpF = new frmHelp();
foreach (Form a in Application.OpenForms)
{
  if (a is frmHelp)
  {
    helpOpen = true;
    break;
  }
}

if (helpOpen)
{
  helpF.Focus();
}
else
{
  helpF.Show();
}



As you'll noticed in this example (versus the previously published examples), I took user experience into consideration. Even so, there's always more that can be done. If you are going to experiment with this code, here are some suggested things to try:

  • Using the Crystal Reports component in Visual Studio, create a report of the results and allow the user to print that report

  • Use the app.config file to control certain numbers and constants, such as overhead markup percentages and cost markup numbers.

  • Using this application as a foundation, create a database back-end and use the application as the interface to a database containing your prices.



Enjoy!


Supplemental Information: The Equation
The equation used to calculate the retail price for jewelry is: total cost + labor + overhead.

To calculate the total cost, you take the cost of the materials used for the piece and multiply that number by 2 (for retail pricing) or by 1.4 (for wholesale pricing). Example: if you bought a lot of 20 beads for $5.00, and you used 10 beads, your total cost is $2.50 * 2 = $5.00 (for retail). Note that some people take the cost of the lot of beads into consideration for the total cost, ex: $5.00 * 2 = $10.00 (for retail), since you may have leftover beads that you can't reuse in other projects.

To calculate the labor, you multiply the hourly rate by the hours worked. Typically, you want to round up to the quarter hour. For example, if your hourly rate is $10.00, and it took you 20 minutes to create a piece, you multiply $10 * .5 (30 minutes) = $5.00.

To calculate overhead (rent, electricity, consumables, travel time, etc), you can do one of two equations:
- For minimum overhead, you take the total cost + labor and multiply it by .20.
- For maximum overhead, you take the total cost + labor and multiply it by .25.

No comments: