Jump to content

Need help with C# methods please. Newbie.

- - - - -

  • Please log in to reply
1 reply to this topic

#1
shim91

shim91

    Newbie

  • Members
  • Pip
  • 3 posts
I have an assignment due and have been struggling with the methods. Please help..

Prior to the project we were given the following classes :

namespace Project_1

{

    static class ProvidedData

    {

        public static Plan[] TelstraPlans

        {

            // The following information is sourced from Telstra's web site as at March 8th, 2011

            // http://www.telstra.c...asual_plan.html

            get

            {

                return new Plan[] {

                    new Plan(10.00M, 0.00M, 0.50M),

                    new Plan(20.00M, 20.00M, 0.45M),

                    new Plan(30.00M, 30.00M, 0.44M),

                    new Plan(40.00M, 40.00M, 0.36M),

                    new Plan(60.00M, 60.00M, 0.26M),

                    new Plan(80.00M, 80.00M, 0.24M),

                    new Plan(100.00M, 100.00M, 0.22M),

                    new Plan(150.00M, 150.00M, 0.20M),

                    new Plan(200.00M, 200.00M, 0.19M)

                };

            }

        }


        public static string CustomerName

        {

            get

            {

                return "J. Citizen";

            }

        }


        public static string PhoneNumber

        {

            get

            {

                return "0412 345 678";

            }

        }


        public static PhoneCall[] CallsMade

        {

            // The following information was created using the random number generator

            get

            {

                return new PhoneCall[] {

new PhoneCall("01/03/2011",  1, 46),

new PhoneCall("01/03/2011",  7,  0),

new PhoneCall("02/03/2011",  4, 34),

new PhoneCall("02/03/2011",  8, 31),

new PhoneCall("02/03/2011",  0, 47),

new PhoneCall("02/03/2011",  4, 15),

new PhoneCall("03/03/2011",  8, 33),

new PhoneCall("04/03/2011",  0,  4),

new PhoneCall("04/03/2011",  4, 38),

new PhoneCall("04/03/2011",  2, 32),

new PhoneCall("05/03/2011",  4, 33),

new PhoneCall("06/03/2011",  4, 17),

new PhoneCall("06/03/2011",  7, 31),

new PhoneCall("06/03/2011",  8,  6),

new PhoneCall("06/03/2011",  4, 29),

new PhoneCall("07/03/2011",  0, 21),

new PhoneCall("07/03/2011",  1,  1),

new PhoneCall("08/03/2011",  0,  4),

new PhoneCall("08/03/2011",  7, 11),

new PhoneCall("08/03/2011",  4, 15),

new PhoneCall("09/03/2011",  4, 12),

new PhoneCall("09/03/2011",  1, 52),

new PhoneCall("09/03/2011",  6, 37),

new PhoneCall("09/03/2011",  6, 46),

new PhoneCall("10/03/2011",  0, 42),

new PhoneCall("10/03/2011",  1, 56),

new PhoneCall("10/03/2011",  8, 12),

new PhoneCall("10/03/2011",  1, 34),

new PhoneCall("11/03/2011",  0, 58),

new PhoneCall("12/03/2011",  0, 42),

new PhoneCall("13/03/2011",  5, 38),

new PhoneCall("13/03/2011",  4,  3),

new PhoneCall("13/03/2011",  6, 42),

new PhoneCall("13/03/2011",  0,  8),

new PhoneCall("14/03/2011",  5, 27),

new PhoneCall("14/03/2011",  1, 45),

new PhoneCall("15/03/2011",  8, 12),

new PhoneCall("15/03/2011",  0, 18),

new PhoneCall("15/03/2011",  7,  1),

new PhoneCall("15/03/2011",  4, 31),

new PhoneCall("16/03/2011",  1, 23),

new PhoneCall("16/03/2011",  5, 58),

new PhoneCall("16/03/2011",  0, 25),

new PhoneCall("16/03/2011",  4, 30),

new PhoneCall("17/03/2011",  5, 31),

new PhoneCall("18/03/2011",  4, 54),

new PhoneCall("18/03/2011",  6, 39),

new PhoneCall("19/03/2011",  2, 17),

new PhoneCall("20/03/2011",  1, 50),

new PhoneCall("20/03/2011",  7, 55),

new PhoneCall("20/03/2011",  5, 20),

new PhoneCall("21/03/2011",  3, 31),

new PhoneCall("21/03/2011",  6, 32),

new PhoneCall("21/03/2011",  7, 29),

new PhoneCall("21/03/2011",  3, 46),

new PhoneCall("22/03/2011",  3, 43),

new PhoneCall("22/03/2011",  5, 57),

new PhoneCall("23/03/2011",  8, 56),

new PhoneCall("23/03/2011",  3, 42),

new PhoneCall("23/03/2011",  7, 47),

new PhoneCall("23/03/2011",  7, 10),

new PhoneCall("24/03/2011",  2, 40),

new PhoneCall("24/03/2011",  6, 18),

new PhoneCall("24/03/2011",  8, 45),

new PhoneCall("25/03/2011",  1, 48),

new PhoneCall("25/03/2011",  0, 31),

new PhoneCall("26/03/2011",  1,  8),

new PhoneCall("27/03/2011",  8, 39),

new PhoneCall("27/03/2011",  4,  4),

new PhoneCall("27/03/2011",  5, 23),

new PhoneCall("28/03/2011",  7, 58),

new PhoneCall("28/03/2011",  4,  3),

new PhoneCall("29/03/2011",  3, 48),

new PhoneCall("29/03/2011",  1, 38),

new PhoneCall("30/03/2011",  5, 24),

new PhoneCall("30/03/2011",  0, 30),

new PhoneCall("30/03/2011",  4, 40),

new PhoneCall("31/03/2011",  8, 31),

new PhoneCall("31/03/2011",  0, 44)

               };

            }

        }

    }

}

I have also written this class, titled Phonecall; As far as I know there are no issues here, all is fine.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


namespace ConsoleApplication4

{

    

        class PhoneCall

        {

            //Private string attribute(s)

            private string _Date;


            //Private int attribute(s)

            private int _Minutes, _Seconds;


            //Public Properties

            public string Date

            {

                get { return _Date; }

            }


            public int Minutes

            {

                get { return _Minutes; }

            }


            public int Seconds

            {

                get { return _Seconds; }

            }


            //constructors

            public PhoneCall(string Date)

            {

                _Date = Date;

            }


            public PhoneCall(int Minutes, int Seconds)

            {

                _Minutes = Minutes;

                _Seconds = Seconds;

            }


        }


}

Now I have this Plan class; All is ok (I think) until the public method goes here.
Basically I need to access the seconds and minutes from the phonecall class, and convert them to ChargeBlocks. For example 3 minutes and 27 seconds of talk time is equal to 7 blocks (30 seconds a block). As long as it i sbetween 3min and 3.30 min it would be 7 blocks.
I need to write a method here which will convert the minutes and seconds into blocks and then multiply the CHARGE_BLOCK by call rate.

The instructions of the step:

Quote

Create a public method CalculateCallCost(), with a phoneCall (PhoneCall data type) parameter and returning a decimal result. This method calculates the cost for an individual phone call, ignoring any call allowance in the plan, returning the result.
I just don't understand how to reference the other classes within the method, and define the minutes/seconds attributes. i also -as you can see, as what i wrote is probably wrong-don't understand what the first statement should be.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;


namespace ConsoleApplication1

{

    class Plan

    {


        private const int CHARGE_BLOCK = 30;

        private decimal _MonthlyFee, _CallAllowance, _CallRate;


        //Public Properties - Read Only

        public decimal MonthlyFee

        {

            get { return _MonthlyFee; }

        }


        public decimal CallAllowance

        {

            get { return _CallAllowance; }

        }


        public decimal CallRate

        {

            get { return _CallRate; }

        }


        //Constructor

        public Plan(decimal MonthlyFee, decimal CallAllowance, decimal CallRate)

        {

            _MonthlyFee = MonthlyFee;

            _CallAllowance = CallAllowance;

            _CallRate = CallRate;

        }


        //public method goes here

        public decimal CalculateCallCost(decimal phoneCall)

        {


        }


        //ToString

        public override string ToString()

        {

            return string.Format("{0} per month. ({1}) call allowance. }", _MonthlyFee, _CallAllowance);

        }


        static void Main(string[] args)

        {

        }

    }

}

having a lot of trouble with this, please help.

#2
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
In order to reference the call time from the phone call, you need to reference an instance of the phone call.

I've read over this, but haven't analyzed it in great detail yet, but I think your issue resides in the understanding of the difference between instance and type members.

for example:

class classA {

   public int field1 { get; set; }

}


in order to access field1, you would have to have an instance of classA.

classA instance = new classA();

classA.field1 = 15;


If the field were static, you wouldn't require an instance, instead, you would treat it as a type-member.

class classA {

    public static int field1 { get; set; }

}


in this case, you would just...

classA.field1 = 15;




so in a round-about way, if you want to access the call times from that method, you would likely use something like...:

foreach (PhoneCall call in ProvidedData.CallsMade) {

    //do something with call

}






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users