Price Calulation Method

This is a discussion on Price Calulation Method within the C++ Programming forums, part of the General Programming Boards category; Hi, I created a base class which contains the product info such as name and price. I created a derived ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    81

    Angry Price Calulation Method

    Hi,

    I created a base class which contains the product info such as name and price.

    I created a derived class which inherited the base class and add 3 new data members:

    string demand
    string supply
    double price

    The question is that the demand and supply data members whose values are one of the qualitative values "low", "medium" and "high". I need to compute the product price change based on those values.

    Here is the header file:

    class Product
    {
    private:
    string name;
    double price;
    int life;
    public:
    Product(string na, double pr, int li);
    double pricechange();

    };

    class Fruitublic Product
    {
    private:
    string demand;
    string supply;
    double percent;

    public:
    Fruit(string na, double pr, int li, string de, string su, double per):Product(na, pr, li), demand(de), supply(su), percent(per){}

    double calpercent(string de, string su, double per);

    };


    Here is the implementation file:

    Product::Product(string na, double pr, int li)
    {
    name = na;
    price = pr;
    life = li;
    }


    double Fruit::calpercent(string de, string su, double per)
    {
    demand = de;
    supply = su;
    percent = per;

    double dd, ss; // Temp local variables.

    if de = "low"
    {
    dd = -0.3; //Manipulated figure only
    }
    else if de = "medium"
    {
    dd = 1;
    }
    else if de = "high"
    {
    dd = 1.2;
    }
    else
    {
    cout << "Your demand input is wrong." << endl;
    }

    if su = "low"
    {
    ss = 1.3;
    }
    else if su = "medium"
    {
    ss = 1;
    }
    else if su = "high"
    {
    ss = 0.8;
    }
    else
    {
    cout << "Your supply input is wrong." << endl;
    }

    per = dd + ss;

    return per;

    }



    I have not yet done the pricechange() as there are many errors in the calpercent(). I do not know how to pass the low, medium and high values to change the changing price percentage.

    Anyone could give me some hints to let me rewrite the code?

    Thanks a lot.

    gogo

  2. #2
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    When testing a logical expression, always use the double equal operator:
    Code:
    if de == "low"
    
    /*  if de = "low"
          the single equal sign is an assignment operator.  The 
          condition will always be true in this case.
    */
    It is always good practice to use parans to group conditional statements:
    Code:
    if (de == "low")
    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    Thanks a lot.

    gogo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 06:50 PM
  2. Price Calculation Problem using Class
    By Bernard4 in forum C++ Programming
    Replies: 4
    Last Post: 07-23-2008, 12:12 PM
  3. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21