Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sales Calculator Programming Help

Status
Not open for further replies.

Mibble

IS-IT--Management
Sep 9, 2004
28
US
Hi there all,
My task is to create a sales calculator, where sales up to 500 are given a 3.5% commission rate, up to 1000 are given 4% and above 1000 are given 5%.
The total sales are tallied, and increment a number of sales counter.
This is then divided to give an average sale.
commission due is based upon the commission rate times the sales amount.
if average sales at least 50 and commission rate is 4%, then a bonus level is set to true and an additional $25 is added to the commission rate.
if average is at least 75 and commission rate is 5% then a bonus level is set to true and an additional $200 is added.
Am i going in the right way with this? also, what is 'use of unassigned variable? this is my code thus far:
Code:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text.RegularExpressions;


namespace sampleapp1
{
    static class Program
    {
    
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

            string saleAmount;
            int numSales;
            double addingSales;
            double ttlSales;
            double avgSale;
            double commDue;
            double commRate;
            bool bnsLevel;
            double bnsAmount;
            double ttlCommDue;

            saleAmount = Console.ReadLine();

            //Validation to perform
            if (validateInput(saleAmount))
            {
                // convert string
                addingSales = Convert.ToInt32(Convert.ToDouble(saleAmount));
                ttlSales += addingSales;
                numSales++;
                avgSale = ttlSales / numSales;
                commDue += (commRate * addingSales);
                if (ttlSales <= 500.00)
                {
                    commRate = 3.5;
                }
                else if (ttlSales < 999.99)
                {
                    commRate = 4;
                }
                else
                {
                    commRate = 5;
                }

                if (ttlSales <= 500.00)
                {
                    if (avgSale >= 50.00 && avgSale < 75.00 && commRate == 4)
                    {
                        bnsLevel = true;
                        bnsAmount = 25.00;
                        ttlCommDue = (commDue + bnsAmount);
                    }
                    else if (avgSale >= 75.00 && commRate == 5)
                    {
                        bnsLevel = true;
                        bnsAmount = 200.00;
                        ttlCommDue = (commDue + bnsAmount);
                    }
                    else if (avgSale >= 1000 && commRate == 5)
                    {
                        bnsLevel = true;
                        bnsAmount = 200.00;
                        ttlCommDue = (commDue + bnsAmount);
                    }
                    else
                    {
                        bnsLevel = false;
                        ttlCommDue = commDue;
                    }
                }
                else if (ttlSales <= 1000.00)
                {
                    if (avgSale >= 50.00 && avgSale < 75.00 && commRate == 4)
                    {
                        bnsLevel = true;
                        bnsAmount = 25.00;
                        ttlCommDue = (commDue + bnsAmount);
                    }
                    else if (avgSale >= 75.00 && commRate == 5)
                    {
                        bnsLevel = true;
                        bnsAmount = 200.00;
                        ttlCommDue = (commDue + bnsAmount);
                    }
                    else if (avgSale >= 1000 && commRate == 5)
                    {
                        bnsLevel = true;
                        bnsAmount = 200.00;
                        ttlCommDue = (commDue + bnsAmount);
                    }
                    else
                    {
                        bnsLevel = false;
                        ttlCommDue = commDue;
                    }
                }
            }
        }

            

        // Function to Test for Positive Number both Integer & Real

        private static bool validateInput(string s)
        {
            Regex r = new Regex(@"^\d+(\.\d{1,2})?$");
            return r.IsMatch(s);
        }
    
    }
    
}
 
Code:
 Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());

is this a windows app or a console app?

anyways - your code doesn't compile... try this...

ttlSales += addingSales;

what is ttlSales starting at? 0? where did you tell it that?

double ttlSales = 0.00; //this will give you a good start

this is your unassigned local variable. How can you put money into an account that hasn't been opened...

nice use of the regex. The other option is to just perform a convert.

Code:
private bool ValidateInput(string dbl)
{
    try
    {
         Convert.ToDouble(dbl);
         return true;
    }
    catch (Exception ex)
    {
        return false;
    }
}
 
yes, this is a windows application. my first one! thus i will most likely debug it as a console app, then figure out how to make it a windows app.
thanks for the info on setting to 0.00
I do have ttlSales += addingSales;

this is where the error is for unassigned variables.

numSales++;
avgSale = (ttlSales / numSales);
commDue += (commRate * addingSales);


numSales and commDue and commRate
 
I converted to a console app, found out there was way too much stuff in there, and have it to this point. however my logic is wrong at the 501 level, where the 25 bonus is to kick in.

Code:
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;


namespace sampleapp1
{
    static class Program
    {

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {

            string saleAmount;
            int numSales = 0;
            double addingSales;
            double ttlSales = 0.00;
            double avgSale;
            double commDue = 0.00;
            double commRate = 0.035;
            bool bnsLevel = false;
            double bnsAmount = 0.00;
            double ttlCommDue;

            Console.WriteLine("enter amt");
            saleAmount = Console.ReadLine();

            //Validation to perform
            if (validateInput(saleAmount))
            {
                // convert string
                addingSales = Convert.ToInt32(Convert.ToDouble(saleAmount));
                Console.WriteLine("amount is {0}", saleAmount);
                ttlSales += addingSales;
                numSales++;
                Console.WriteLine(" totalsales = {0}, adding = {1}, number sales = {2}", ttlSales, addingSales, numSales);
                avgSale = (ttlSales / numSales);
                Console.WriteLine("average is {0}", avgSale);
                if (ttlSales <= 500.00)
                {
                    commRate = 0.035;
                }
                else if (ttlSales <= 999.99)
                {
                    commRate = 0.04;
                }
                else
                {
                    commRate = 0.05;
                }

                commDue += (commRate * addingSales);
                Console.WriteLine("commission due is {0} and rate is {1} and average is {2}", commDue, commRate, avgSale);

                    if (avgSale >= 50 && avgSale < 75 && commRate == 0.04)
                    {

                        bnsLevel = true;
                        bnsAmount = 25.00;
                        ttlCommDue = (commDue + bnsAmount);
                    }
                        
                    else if (avgSale >= 75.00 && commRate == 0.05)
                    {
                        bnsLevel = true;
                        bnsAmount = 200.00;
                        ttlCommDue = (commDue + bnsAmount);
                    }
                    else
                    {
                        bnsLevel = false;
                        ttlCommDue = commDue;
                    }
                commDue += bnsAmount;
                Console.WriteLine("commission due is {0}", commDue);

            }
        }

        // Function to Test for Positive Number both Integer & Real

        private static bool validateInput(string s)
        {
            Regex r = new Regex(@"^\d+(\.\d{1,2})?$");
            return r.IsMatch(s);
        }

    }

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top