Naruto Jr
Naruto Jr

Reputation: 11

C# Console Application using methods compile error (beginner seeking HELP)

So I am super new to programming and I was trying to write a program from my book.

Here is the assignment:

Write a program that allows this school club ability to determine a few things about their candy bar sales. They can enter things like how many boxes they have, the cost per box, the cost per candy bar etc. Then the program using methods will return some calculations for them such as their gross profit, deduct % due to their student government and give the net profit.

Error CS0116 A namespace cannot directly contain members such as fields or methods Assignment3Israel ERROR: CS0103 The name 'totlalDuesOwedIn' does not exist in the current context Assignment3Israel
ERROR: CS0029 Cannot implicitly convert type 'string' to 'decimal' Assignment3Israel

  using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assignment3Israel
{
    class GranolaBarSalesProject
    {
        //Constant variable sga 10% cut per candy bar sold//
        const decimal sgaDues = .010m;

        static void Main(string[] args)
        {
            //Capture total number of candy bars//
            decimal totalAmountCases = GetCases();
            //Capture cost per case of candy bars//
            decimal perCaseCost = GetCaseCost();
            //Capture total cost of goods (cogs)
            decimal cogs = CalctotalAmountCases(perCaseCost);
            //Capture total number of candy bars per case
            int candyBarsPerCase = GetCandyBarsPerCase();
            //Get cost per candy bar
            decimal pricePerCandyBar = GetPricePerCandyBar();
            //Get the total amount of candy bars
            decimal totalAmountCandyBars = CalctotalAmountCases(candyBarsPerCase);
            //Get the amount of candy bars sold
            decimal totalCandyBarsSold = GetTotalCandyBarsSold();
            //Calculate the total amount of bars sold 
            decimal totalCandyBarSales = CalctotalCandyBarsSold(pricePerCandyBar);
            //Calculate the Gross Profits
            decimal grossProfit = CalctotalACandyBarSales(cogs);
            //Capture the total amount due to the student government
            decimal totalDuesOwed = CalcgrossProfit(sgaDues);
            decimal netProfit = CalcgrossProfit(totalDuesOwed);
            Console.ReadKey();
        }

        public static string GetCases()
        {
            Console.Write("How many cases of candy bars: ");
            return Console.ReadLine();
        }

        public static string GetCaseCost()
        {
            Console.Write("How much per case of candy bars: ");
            return Console.ReadLine();
        }

        public static string GetCandyBarsPerCase()
        {
            Console.Write("how many candy bars per case: ");
            return Console.ReadLine();
        }

        public static string GetPricePerCandyBar()
        {
            Console.Write("how much per candy bar: ");
            return Console.ReadLine();
        }

        public static string GetTotalCandyBarsSold()
        {
            Console.Write("how many candy bars did you sell: ");
            return Console.ReadLine();
        }



    }
    public static decimal CalctotalAmountCandyBars(decimal candyBarsPerCase)
    {
        return totalAmountCases * candyBarsPerCase;
        //returns the total number of candy bars
    }
    //Calculating the total nuber of candy bars and returning that calculation
    public static void ShowTotalAmountCandyBars(string totlalAmountCandyBarsIn)
    {
        Console.WriteLine("Here are the total number of candy bars {0}: ", totlalAmountCandyBarsIn);
    }


    public static decimal CalctotalCandyBarSales(decimal pricePerCandyBar)
    {
        return totalCandyBarsSold * pricePerCandyBar;
        //returns the candy bar sales before the 10% cut to Student gov
    }
    //Calculating candy bar sales and displaying amount
    public static void ShowtotalCandyBarsales(string totalCandyBarSalesIn)
    {
        Console.WriteLine("Here is the Candy Bar sales before dues to SG {0:C}: ", totalCandyBarSalesIn);
    }


    public static decimal CalcgrossProfit(decimal cogs)
    {
        return totalCandyBarSales - cogs;
        //returns the gross profit
    }
    //Calculating the gross profit and returning that calculation
    public static void ShowgrossProfit(string grossProfitIn)
    {
        Console.WriteLine("Here is the Gross Profits of sales {0:C}: ", grossProfitIn);
    }

    public static decimal CalctotalDuesOwed(decimal sgaDues)
    {
        return grossProfit * sgaDues;
        //returns the total amount due to SG
    }
    //Calculating the total amount of dues to SG and display
    public static void ShowtotalDuesOwed(string totalDuesOwedIn)
    {
        Console.WriteLine("Here is the total due to the Student Government {0:C}: ", totlalDuesOwedIn);
    }

    public static decimal CalcnetProfit(decimal totalDuesOwed)
    {
        return grossProfit - totalDuesOwed;
        //returns the net profit of candy bar sales
    }
    //Calculating the net profit and displaying it
    public static void ShownetProfit(string netProfitIn)
    {
        Console.WriteLine("Here is the Net Profit of your sales {0:C} ", netProfitIn);

    }

}

Upvotes: 0

Views: 124

Answers (2)

Abion47
Abion47

Reputation: 24671

Error CS0116 A namespace cannot directly contain members such as fields or methods

You have a lot of methods that you declared outside the class, which is not allowed. Move the methods inside the brackets that mark the class scope.

ERROR: CS0103 The name 'totlalDuesOwedIn' does not exist in the current context

The method totlalDuesOwedIn doesn't exist. This is likely a typo, and you meant to type "totalDuesOwedIn".

ERROR: CS0029 Cannot implicitly convert type 'string' to 'decimal'

Your methods are returning values of type string but you are trying to store the values in variables of type decimal. Those two types are not implicitly compatable with either other, but you can use conversion methods like decimal.Parse(stringVal) to try and convert them.

Upvotes: 1

Daryl G
Daryl G

Reputation: 61

There are quite a few errors, but to get to started

This will not compile as written because of where totalAmountCases is declared

public static decimal CalctotalAmountCandyBars(decimal candyBarsPerCase)
{
    return totalAmountCases * candyBarsPerCase;
    //returns the total number of candy bars
}

move totalAmountCases declaration out of main to this -:

class GranolaBarSalesProject
{
    const decimal sgaDues = .010m;
    decimal totalAmountCases;

    static void Main(string[] args)
    {
        //Capture total number of candy bars//
        totalAmountCases = GetCases();

    }

Upvotes: 1

Related Questions