5 Replies - 997 Views - Last Post: 17 February 2013 - 07:51 PM Rate Topic: -----

#1 lepazoga  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-February 13

Trouble with Methods in a class

Posted 17 February 2013 - 11:25 AM

So this should be simple. Here is my question I have to answer:
Write a program that computes the amount of money the computer club will receive from proceeds of their granola bar sales project.Allow the user to enter the number of cases sold and the sale price per bar.Each case contains 12 bars; each case is purchased at $5.00 per case from a local vendor.The club is required to give the student govern- ment association 10% of their earnings. Display their proceeds formatted with currency. Write appropriate methods for your solution.

**I should also note that I'm not looking for any answers to cheat, because this is an assignment, but I just need some guidance.

- My questions are:
1.) Am I declaring these variables correctly? For example, I have casesSold in the Main method, then I also have casesSold in the BarsSold and GovFees method. Is this
bad practice?
2.) How do I go about taking the information I have gathered from the user input and obtain the amount of netsales?

using System;

namespace Derp
{
	class MainClass
	{
		 static void Main ()
		{
			int casesSold;
			
			Console.WriteLine ("Enter in the amount of cases sold: ");
			casesSold = int.Parse (Console.ReadLine ());
			
			Console.WriteLine ("The amount of bars sold were: " + BarsSold(casesSold));
			Console.WriteLine ("The amount of cases sold were: " + casesSold);
			Console.WriteLine ("Student Government Fees were: {0:C} ", GovFees(casesSold));
			
		}
		
		public static int BarsSold(int casesSold)
		{
			int BARS_PER_CASE = 12;
			return casesSold * BARS_PER_CASE;
		}

		public static double GovFees(int casesSold)
		{
			const double PRICE_PER_CASE = 5.00;
			const double STU_GOV_FEES = .10;
			return STU_GOV_FEES * (PRICE_PER_CASE * casesSold);
		}
	}
}






Is This A Good Question/Topic? 0
  • +

Replies To: Trouble with Methods in a class

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,846
  • Joined: 02-June 10

Re: Trouble with Methods in a class

Posted 17 February 2013 - 11:53 AM

A 1) Yes, bad practice
A 2) By doing math. The question is vague. "How do I do xyz?" pretty much tells us nothing about what you are failing to understand from class. YOu seem to be doing this:
Line 12 get user input
Line 23 do math with user input
So what is the problem?

Line 20: YOu are making a new variable local to that method with the same name as the class-wide variable of that name. That means you are obscuring the class-wide variable so you can't reach it. Since you have a classwide variable for this, you don't need to have an argument for the method.
Was This Post Helpful? 0
  • +
  • -

#3 lepazoga  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-February 13

Re: Trouble with Methods in a class

Posted 17 February 2013 - 04:19 PM

Ok, this is what I got after much trial and error. It's slowly making more sense.....

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

namespace GranolaBars
{
    class Program
    {
        static void Main(string[] args)
        {
            // Declare some variables
            int casesSold;
            double pricePerBar;
            double profit;
            double proceeds;
            double finalOutcome;

            casesSold = GetCasesSold();
            pricePerBar = GetPricePerCase();
            profit = GetProfit(casesSold, pricePerBar);
            proceeds = GetProceeds(profit);
            finalOutcome = FinalOutcome(proceeds, profit);

            // Output
            Console.WriteLine("The amount of cases sold were: " + casesSold);
            Console.WriteLine("The price per bar was: {0:C}", pricePerBar);
            Console.WriteLine("The gross income was: {0:C}", profit);
            Console.WriteLine("The student government fees were: {0:C}", proceeds);
            Console.WriteLine("Income minus government fees were: {0:C}", finalOutcome);
        }

        // Method that gets number of cases sold
        public static int GetCasesSold()
        {
            int cSold;
            Console.WriteLine("Enter cases sold: ");
            cSold = int.Parse(Console.ReadLine());
            return cSold;
        }

        // Method that gets the price per case
        public static double GetPricePerCase()
        {
            double perBar;
            Console.WriteLine("Enter the price per bar: ");
            perBar = double.Parse(Console.ReadLine());
            return perBar;
        }

        // Method that gets the profit
        public static double GetProfit(int casesSold, double pricePerBar)
        {
            const double CASE_PRICE = 5.00;
            double profit;
            profit = ((pricePerBar * 12) - CASE_PRICE) * casesSold;
            return profit;
        }

        // Method that gets the proceedes
        public static double GetProceeds(double profit)
        {
            const double STU_GOV_FEES = .10;
            return (profit * STU_GOV_FEES);
        }

        public static double FinalOutcome(double proceeds, double profit)
        {
            return (profit - proceeds);
        }




    }
}


Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,846
  • Joined: 02-June 10

Re: Trouble with Methods in a class

Posted 17 February 2013 - 07:34 PM

View Postlepazoga, on 17 February 2013 - 05:19 PM, said:

Ok, this is what I got after much trial and error. It's slowly making more sense.....


Congrats! Trial and error are how we all learn this. *DOING* is the real teacher. You do wrong, you see the results, you try version 2, that's closer, version 3 closer still. Just like writing a novel: You can't get the first one right and you can't be told how to do it. You start with simple short stories and work you way up to more involved works.

The next thing you want to start working on in input validation.
What if someone enters "-8" as a price? Or even "Fred Flintstone" as the number of cases? You aren't validating the user input at the time they enter it.

Also, double is prone to rounding errors. decimal is the type normally used for currency.
Was This Post Helpful? 0
  • +
  • -

#5 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: Trouble with Methods in a class

Posted 17 February 2013 - 07:43 PM

One thing I'd suggest is to not put const variables inside a method. Either remove the const and leave those variables as local values or move them to a global scope. Generally, the reason you create a const variable is so that the value is available to the entire application and you don't want the value to be able to be changed. Creating a const variable within a method like that is kind of pointless because the only place it is accessible is within that method.
Was This Post Helpful? 0
  • +
  • -

#6 lepazoga  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-February 13

Re: Trouble with Methods in a class

Posted 17 February 2013 - 07:51 PM

I see. That would make sense. Thanks for the input.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1