user1863593
user1863593

Reputation: 199

Looping through Arrays - better way?

I am new to arrays but there has to be a better way to loop through arrays than what I am doing. This code is ugly. Please let me know of any ideas. The idea for this program is when a user enters what size shirt they need and they will receive a discount based on the quantity. If more than 2 (they receive 10% discount, if >=3 and <= 4 they receive 15% discount, if >= 5 they receive 25% discount .

Here is my code:

string userInputString;
string userInput;
int userInputNo= 0;
double userPrice = 0;
string userSize = "";
string[,] prices = {{"S", "5.00"}, {"M", "7.00"}, {"L", "9.00"}, {"X", "12.00"} };

Console.Write("What size shirt (S, M, L, OR X-Large): ");

userInputString = Console.ReadLine();
userInput = Convert.ToString(userInputString);
Console.Write("How many shirts do you need?: ");
userInputString = Console.ReadLine();
userInputNo = Convert.ToInt32(userInputString);

if (userInputNo == 2)
{
    if (userInput == prices[0,0])
    {
        userPrice = ((Convert.ToDouble(prices[0,1]) * 0.10) +  Convert.ToDouble(prices[0,1]));
        userSize = prices[0,0].ToString();
    }
    else if (userInput == prices[1, 0])
    {
        userPrice = ((Convert.ToDouble(prices[1, 1]) * 0.10) + Convert.ToDouble(prices[1, 1]));
        userSize = prices[1, 0].ToString();
    }
    else if (userInput == prices[2, 0])
    {
        userPrice = ((Convert.ToDouble(prices[2, 1]) * 0.10) + Convert.ToDouble(prices[2, 1]));
        userSize = prices[2, 0].ToString();
    }
    else
    {
        userPrice = ((Convert.ToDouble(prices[3, 1]) * 0.10) + Convert.ToDouble(prices[3, 1]));
        userSize = prices[3, 0].ToString();
    }
}
else if (userInputNo >= 3 && userInputNo <= 4)
{
    if (userInput == prices[0, 0])
    {
        userPrice = ((Convert.ToDouble(prices[0, 1]) * 0.15) + Convert.ToDouble(prices[0, 1]));
        userSize = prices[0, 0].ToString();
    }
    else if (userInput == prices[1, 0])
    {
        userPrice = ((Convert.ToDouble(prices[1, 1]) * 0.15) + Convert.ToDouble(prices[1, 1]));
        userSize = prices[1, 0].ToString();
    }
    else if (userInput == prices[2, 0])
    {
        userPrice = ((Convert.ToDouble(prices[2, 1]) * 0.15) + Convert.ToDouble(prices[2, 1]));
        userSize = prices[2, 0].ToString();
    }
    else
    {
        userPrice = ((Convert.ToDouble(prices[3, 1]) * 0.15) + Convert.ToDouble(prices[3, 1]));
        userSize = prices[3, 0].ToString();
    }
}
else if (userInputNo >= 5)
{
    if (userInput == prices[0, 0])
    {
        userPrice = ((Convert.ToDouble(prices[0, 1]) * 0.20) + Convert.ToDouble(prices[0, 1]));
        userSize = prices[0, 0].ToString();
    }
    else if (userInput == prices[1, 0])
    {
        userPrice = ((Convert.ToDouble(prices[1, 1]) * 0.20) + Convert.ToDouble(prices[1, 1]));
        userSize = prices[1, 0].ToString();
    }
    else if (userInput == prices[2, 0])
    {
        userPrice = ((Convert.ToDouble(prices[2, 1]) * 0.20) + Convert.ToDouble(prices[2, 1]));
        userSize = prices[2, 0].ToString();
    }
    else
    {
        userPrice = ((Convert.ToDouble(prices[3, 1]) * 0.20) + Convert.ToDouble(prices[3, 1]));
        userSize = prices[3, 0].ToString();
    }
}
else
{
    if (userInput == prices[0, 0])
    {
        userPrice = Convert.ToDouble(prices[0,1]);
        userSize = prices[0, 0].ToString();
    }
    else if (userInput == prices[1, 0])
    {
        userPrice = Convert.ToDouble(prices[1,1]);
        userSize = prices[1, 0].ToString();
    }
    else if (userInput == prices[2, 0])
    {
        userPrice = Convert.ToDouble(prices[2,1]);
        userSize = prices[2, 0].ToString();
    }
    else
    {
        userPrice = Convert.ToDouble(prices[3,1]);
        userSize = prices[3, 0].ToString();
    }
}

Console.WriteLine("For a size {0}, you will pay $ {1}.", userSize.ToString(), userPrice);

Upvotes: 1

Views: 110

Answers (1)

Ed Swangren
Ed Swangren

Reputation: 124632

There are no loops in your code. However, you want a lookup table, not an array, so use Dictionary<K,V>.

var dict = new Dictionary<string, double> {
    {"S", 0.05},
    {"M", 0.07}, 
    {"L", 0.09}, 
    {"X", 0.12}
};

And then...

userInputString = Console.ReadLine();
var discount = 0.0;
if (dict.TryGetValue(userInputString, out discount))
{
    // discount now holds the discount amount.
    // multiply to get the effective price
}
else
{
    // bad input, alert user
}

// side note: TryGetValue is a method which allows you to
// both test if a key exists and get the value at the same
// time.  if you know that a dictionary contains the key
// you need you can just index into it, i.e.,
// var discount = dict[userInputString];

if you are forced to use an array by your homework instructions, then the loop would look like this:

var discount = 0.0;
for(int i = 0; i < prices.GetLength(0); ++i)
{
    if (String.Equals(prices[i, 0], userInputString, StringComparison.OrdinalIgnoreCase))
    {
        discount = prices[i, 0];            
        break;  // found it, all done
    }
}

However, this is the wrong route to take. No need for an O(n) lookup here.

Additionally, don't store everything as a string just to convert it back to a number. You're writing your code backwards; think in terms of data when writing code and then format for output.

Upvotes: 3

Related Questions