C#: Attempting to create a shoe store: trying to evaluate the correct SKU

56 Views Asked by At

I'm trying to create a shoe store program where the user will type in the SKU number to get the desired shoe. I'm trying to create an if statement that will ask the user for the quantity if the SKUs match or display "Enter a valid SKU: ", if it is invalid. Now it works but it automatically says that the number is invalid no matter what SKU I put in. Then when the entry is valid it no longer asks for the quantity!

while (keepBuyingShoes == true)
{
    // ask user for sku and quantity 
    int quantity = 0;
    Console.WriteLine("Enter shoe SKU: ");
    String sku = Console.ReadLine();
    if (sku != s1.SKU || sku != s2.SKU || sku != s3.SKU || sku != s4.SKU || sku != s5.SKU)
    {
       Console.WriteLine("That is an invalid SKU number!");
       Console.WriteLine("Enter shoe SKU: ");
       sku = Console.ReadLine();
    }
    else if (sku == s1.SKU || sku == s2.SKU || sku == s3.SKU || sku == s4.SKU || sku == s5.SKU)
    {
      Console.WriteLine("Enter quantity: ");
      quantity = int.Parse(Console.ReadLine());
    }
3

There are 3 best solutions below

7
Etienne de Martel On

Did you notice how you use || in both if statements despite how one is supposed to be the opposite of the other? Isn't that strange? Shouldn't at least one use && in that case? Huh.

So, yeah, as you might probably guess, your condition is wrong:

if (sku != s1.SKU || sku != s2.SKU || sku != s3.SKU || sku != s4.SKU || sku != s5.SKU)
{
    // ...
}

This is obvious if you actually think about the logic: you're essentially saying "if the SKU isn't s1 or isn't s2 or isn't s3 or isn't s4 or isn't s5...". In order for this to be false, the SKU would have to be all of those at once, which is impossible. So, it's a tautology. No wonder it's always entering that if.

The solution is to use && instead:

if (sku != s1.SKU && sku != s2.SKU && sku != s3.SKU && sku != s4.SKU && sku != s5.SKU)
{
    // ...        
}

That way, you enter the if if the SKU is never any one of those.

And then you can just turn the following else if into a regular else.

Now, you should probably shove all those SKU objects into a collection and use things like Any and All, but that's a topic for another time.

0
Jon P On

Having multiple variables like s1 , s2 etc will not scale well. Everytime you add a new item like s101 you are going to need to update your logic, which is not ideal.

You are better off using a collection like an array or list. Then you can query the collection via linq

Something like:

var skuList = new List<YOUR_SKU_TYPE>() {
    new YOUR_SKU_TYPE() {SKU = "SKU1" } ,
    new YOUR_SKU_TYPE() {SKU = "SKU2" } ,
    new YOUR_SKU_TYPE() {SKU = "SKU3" }
};

while (keepBuyingShoes == true)
{
    // ask user for sku and quantity 
    int quantity = 0;
    Console.WriteLine("Enter shoe SKU: ");
    String sku = Console.ReadLine();

    //Check if sku matches one in our list
    if (skuList.Exists(x => x.SKU == sku)
    {
      Console.WriteLine("Enter quantity: ");
      quantity = int.Parse(Console.ReadLine());
    }
    else
    {
       Console.WriteLine("That is an invalid SKU number!");
       Console.WriteLine("Enter shoe SKU: ");
       sku = Console.ReadLine();
    }
}

NOTE code is completely untested and might need some debugging.

0
Alpha Reuel On

When entering an SKU into the console, it tests to see if it doesn't equal one of those shoe SKUs. If it isn't equal to just one of them, then the else if statement won't run. I spent some time dinking around with the code and found that this does exactly what you want (not including the object naming like s1...):

namespace TestStackQuestion           
{                  
    internal class Program         
    {

        public static string s1 = "SKU1";
        public static string s2 = "SKU2";
        public static string s3 = "SKU3";

        static void Main(string[] args)
        {
            Console.WriteLine("Enter your shoe SKU:");
            var UserSKU = Console.ReadLine();

            while (UserSKU != s1 && UserSKU != s2 && UserSKU != s3)
            {
                Console.WriteLine("Invalid SKU \n Enter a new SKU");
                UserSKU = Console.ReadLine();
            }
            if (UserSKU == s1 || UserSKU == s2 || UserSKU == s3)
            {
                Console.WriteLine("Enter quantity: ");
                int quantity = int.Parse(Console.ReadLine());
            }
            
        }
    }
}