(Sorry, my
C#
is a bit rusty)
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
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));
}