Click here to Skip to main content
15,904,503 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Calculate
{
    class CalculateProfits
    {
        static void Main()
        {
            double pricePerBar, casesSold;
            Console.Write("The amount of profit made on the granola sales will be displayed.\n" +
                 "First you will be asked to enter the sale price per granola bar.\n" +
                 "Then you will be asked to enter the number of cases of granola bars sold. \n\n\n");
            pricePerBar = GetInfo("price per bar");//gets the price per bar
            casesSold = GetInfo("number of cases sold");//gets the number of cases.
            //write the main methods here.
            static int BarsSold(double casesSold)
        {
            //Declare Variables
            int BARS_PER_CASE = 12;
            int BarsSold;
            BarsSold = casesSold * BARS_PER_CASE;
            return BarsSold;
        }
        
        static double GrossSales(double price, int BarsSold)
{			
            //Declare Variables
            double price;
            int BarsSold;
            GrossSales= price* BarsSold;
            return GrossSales;
                
}
        
        static double NetSales(double casesSold, double grossSales)
{
            const double WHOLESALE_COST_PER_CASE = 5.00;
            //Declare Variables
            double casesSold;
            double grossSales;
            NetSales= GrossSales-(casesSold*WHOLESALE_COST_PER_CASE);
            return NetSales;
	
        }
    
         
        static double GovFeeDue(double netSales)
        {
            const double ASSOCIATION_FEE_RATE = 0.10;
            double netSales;
            GovFeesDue=ASSOCIATION_FEE_RATE*NetSales;
            return GovFeeDue;
 
   
        }		
	
        static double NetProfit(double netSales, double govFeeDue)
        { 	
            double netSales;
            double govFeeDue;
        
             NetProfit=netSales-govFeeDue;
             return NetProfit;
}	
       
        static void DisplayResults(double profit)
{			
            
                    Console.WriteLine("Net Profit:{0:C}",NetProfit);
}
          

            Console.ReadLine();
        }
        static double GetInfo(string wantedInfo)
        {
            string inputValue;
            double number;
            Console.Write(
                "\nPlease enter the {0}: ", wantedInfo);
            inputValue = Console.ReadLine();
            number = double.Parse(inputValue);
            return number;
        }
        
    }
}


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 17-Jul-11 2:35am
v2
Comments
Keith Barrow 17-Jul-11 8:32am    
Please edit your question format your code, highlight it and click the "code block" button. Currently it is unreadable.
Then post you error message, without this you'll get no help as we aren't going to pick through your code to spot problems, for one thing we don't know what it is supposed to do. If you aren't getting an error message, please tell us specifically what is not working.
OriginalGriff 17-Jul-11 8:36am    
Can you post the problems you are having with it? Since we don't know what your specification is, it is difficult to class whatever it is that it does or doesn't do as an "error".

Try debugging through the source code. Here is a tutorial how to do that[^].
Look at the temporary variable values (or the errors) and try and figure out where things could be going wrong.
 
Share this answer
 
What error codes are you getting?

Are you using VS?

Is there any intellisense messages?

More info needed, but if it is VS, then the errors will be highlighted and you can at least tell us in which area there are problems.
 
Share this answer
 
The problematic places in your methods are the declaration of local variables which would shadow the declared method parameters. I'm not sure if this would even compile. At least there should be a couple of warnings.

Best Regards,

MRB
 
Share this answer
 
Comments
Espen Harlinn 17-Jul-11 18:34pm    
Yupp - a 5
casesSold = GetInfo("number of cases sold");//gets the number of cases.
//write the main methods here.
static int BarsSold(double casesSold)
...

Following the call to GetInfo() you have a comment line followed by a new method. But where is your code supposed to go after GetInfo()? You cannot just fall through into a new method, you must call each required method in turn and process the results as defined by your system design. Perhaps you should go back to your C# primer and read up on the structure of a program.
 
Share this answer
 
C#
class Program
{
    static void Main()
    {
        double pricePerBar;
        int casesSold;

        Console.Write("The amount of profit made on the granola sales will be displayed.\n" +
                      "First you will be asked to enter the sale price per granola bar.\n" +
                      "Then you will be asked to enter the number of cases of granola bars sold. \n\n\n");

        pricePerBar = GetDouble("price per bar"); //gets the price per bar
        casesSold = GetInteger("number of cases sold"); //gets the number of cases.


        //write the main methods here.

        double netSales = NetSales(casesSold, GrossSales(pricePerBar, BarsSold(casesSold)));
        double netProfit = NetProfit(netSales, GovFeeDue(netSales));
        DisplayResults(netProfit);
   }

    static int BarsSold(int casesSold)
    {
        //Declare Variables
        int BARS_PER_CASE = 12;
        return casesSold * BARS_PER_CASE;
    }

    static double GrossSales(double price, int BarsSold)
    {
        //Declare Variables
        return price * BarsSold;
    }

    static double NetSales(double casesSold, double grossSales)
    {
        const double WHOLESALE_COST_PER_CASE = 5.00;
        //Declare Variables
        return grossSales - (casesSold * WHOLESALE_COST_PER_CASE);
    }


    static double GovFeeDue(double netSales)
    {
        double fee_rate = 0.10;
        return fee_rate * netSales;
    }

    static double NetProfit(double netSales, double govFeeDue)
    {
         return netSales - govFeeDue;
    }

    static void DisplayResults(double profit)
    {

        Console.WriteLine("Net Profit:{0:C}", profit);
        Console.ReadLine();
    }

    static int GetInteger(string wantedInfo)
    {
        return Convert.ToInt32(GetInfo(wantedInfo));
    }

    static double GetDouble(string wantedInfo)
    {

        return Convert.ToDouble(GetInfo(wantedInfo));
    }

    static string GetInfo(string wantedInfo)
    {
        string inputValue;
        Console.Write("\nPlease enter the {0}: ", wantedInfo);
        inputValue = Console.ReadLine();
        return inputValue;
    }

}


Did you convert this from VB? Remember that C# is case sensitive and you seenm to have cases pretty mixed up. Also, C# treats functions slightly differently and you don't need to declare the return values if you are not using them for any other purpose.

I did not add any error checking - consider how to handle the user entering alpha characters or a ridiculously high number. Neither did I change your logic but I think you could simplify the implementation of your algorithm.

Another thing to look at is the way you hard-code constants. What if the wholesale cost changes or the goverment fee rate goes up? What do you think you could do so that you wouldn't have to change the program if either of these things happened?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900