Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I need to display price value based on quantity...Example I have

string str = " 28.00 3.00 71.40 6.00 134.40 " - is in STRING FORMAT

which means for 1 & 2 QUANTITY the PRICE should be 28.00 for 3-5 QUANTITY PRICE is 71.00, for 6 & above PRICE is 134.40



Can someone please help me with the code.using Regular Expressions or SPLIT method or whatever way it meets my requirement.

Thank you.
Posted
Comments
CoderPanda 5-Mar-14 5:11am    
For the given string, how do you want your output to appear? It may be easier for me to help if you mention that.
Bajid Khan 5-Mar-14 5:21am    
Qty 1 or 2 = 28.00 here 1 qty price is 28.oo for 2 ->56.00

3 or 4 or 5 = 71.40 here 71.40/3=23.8 here 1 qty price is 23.8 for 4 4*23.8= 95.2

6 & above = 134.40 here 134.40/6 =22.33 here 1 qty price is 22.33 for 7 7*22.33=156.31
Ramug10 5-Mar-14 5:20am    
show me your whole code..
Bajid Khan 5-Mar-14 5:25am    
did u under stand what i'm mentioned above ?
Amalraj Ramesh 5-Mar-14 5:59am    
I can't understand.

C#
int qty=5;//YOUR VALUE
stirng[] data=str.Split(' ');
if(data.length>0)
{
decimal price=data[0];
for(int i=1;i<data.length;i+2)
{
if(qty<=data[i])
{
price=convert.toDecimal(data[i-1])*qty;
}
else
{
break;
}
}
}



i dint tested,hope it will work
 
Share this answer
 
v3
(Sorry, my C# is a bit rusty)
C#
public class Pricer
{
  Dictionary<double, double> price;

  public Pricer(string s)
  {
    string[] a = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    price = new Dictionary<double, double>();
    CultureInfo INVC = CultureInfo.InvariantCulture;

    for (int n = 0; n < a.Length - 1; n += 2)
      price.Add(double.Parse(a[n+1], INVC), double.Parse(a[n],INVC));
    price.Add(double.PositiveInfinity, double.Parse(a[a.Length - 1], INVC));
  }

  public double getPrice(double quantity)
  {
    double result = 0.0;
    foreach (double d in price.Keys)
      if (quantity < d)
      {
        result = price[d]*quantity;
        break;
      }
    return result;
  }
}


then
C#
static void Main(string[] args)
{
  string str = " 28.00 3.00 71.40 6.00 134.40 ";

  Pricer pr = new Pricer(str);
  Console.WriteLine("Quantity {0} price {1}", 0.5, pr.getPrice(0.5));
  Console.WriteLine("Quantity {0} price {1}", 5.5, pr.getPrice(5.5));
  Console.WriteLine("Quantity {0} price {1}", 1000, pr.getPrice(1000));
}
 
Share this answer
 
v2
Try this
C#
string str=" 28.00 3.00 71.40 6.00 134.40 ";
string[] s=str.split(' '); double price;double unitprice;
if(Quantity==1 || Quatity==2)
{
if(Quantity==2)
price=convert.todouble(s[0]*2);
else
price=s[0];
}
elseif(Quantity==3 || Quatity==4 || Quantity==5)
{
 unitprice=convert.todouble(s[2]/3);
if(Quantity==4)
price=unitprice*4;
elseif(Quantity==5)
price=unitprice*5;
else
price=convert.todouble(s[2]);
}
else
{
 unitprice=convert.todouble(s[4]/6);
if(Quantity>6)
price=unitprice*Quantity;

else

{
price=convert.todouble(s[4]);
}
 
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