using System;

class Program
{
    static void Main()
    {
        double total_Shipping = 0, total_Weight, standard_Item = 3.00, express_Item = 4.00, over_Night_Item = 5.50, standard_Weight = 1.45, express_Weight = 2.50, over_Night_Weight = 3.00, rate = 0, surcharge_Standard = 2.50, surcharge_Express = 5.00, surcharge_OverNight = 8.00; 
        int shipping_Speed, total_Items = 0;
        char shipping_Type, hawaii;
        

            Console.WriteLine("Please enter which shipping method you want:");
            Console.WriteLine("Enter 1- for Standard shipping.");
            Console.WriteLine("Enter 2- for Express shipping.");
            Console.WriteLine("Enter 3- for Overnight shipping.");
        shipping_Speed = int.Parse(Console.ReadLine());
            Console.WriteLine("Thank you.");
           
            Console.WriteLine("Is this shipment in category A or B?");
        shipping_Type = char.Parse(Console.ReadLine());
            Console.WriteLine("Thank you.");
           
            Console.WriteLine("Will this package be shipped to Alaska or Hawaii?(Y/N)");
        hawaii = char.Parse(Console.ReadLine());
            Console.WriteLine("Thank you.");

            if (shipping_Type == 1)
            {
                Console.WriteLine("How many items are to be shipped?");
                total_Items = (int)(Console.Read());
            }
            else if (shipping_Type == 2)
                Console.WriteLine("How heavy is your package?(In pounds)");
            total_Weight = double.Parse(Console.ReadLine());





            if (shipping_Speed == 1 && shipping_Type == 'a')
            {
                rate = standard_Item;
            }
            else if (shipping_Speed == 1 && shipping_Type == 'b')
            {
                rate = standard_Weight;
            }
            else if (shipping_Speed == 2 && shipping_Type == 'a')
            {
                rate = express_Item;
            }
            else if (shipping_Speed == 2 && shipping_Type == 'b')
            {
                rate = express_Weight;
            }
            else if (shipping_Speed == 3 && shipping_Type == 'a')
            {
                rate = over_Night_Item;
            }
            else if (shipping_Speed == 3 && shipping_Type == 'b')
            {
                rate = over_Night_Weight;
            }
        

       
                if (shipping_Speed == 1 && shipping_Type == 'a' && hawaii == 'y')
                {
                    total_Shipping = rate * total_Items + surcharge_Standard;
                }
                else if (shipping_Speed == 1 && shipping_Type == 'a' && hawaii == 'n')
                {
                    total_Shipping = rate * total_Items;
                }
                else if (shipping_Speed == 1 && shipping_Type == 'a' && hawaii == 'y')
                {
                    total_Shipping = rate * total_Weight + surcharge_Standard;
                }
                else if (shipping_Speed == 1 && shipping_Type == 'b' && hawaii == 'n')
                {
                    total_Shipping = rate * total_Weight;
                }
                else if (shipping_Speed == 2 && shipping_Type == 'a' && hawaii == 'y')
                {
                    total_Shipping = rate * total_Items + surcharge_Express;
                }
                else if (shipping_Speed == 2 && shipping_Type == 'a' && hawaii == 'n')
                {
                    total_Shipping = rate * total_Items;
                }
                else if (shipping_Speed == 2 && shipping_Type == 'b' && hawaii == 'a')
                {
                    total_Shipping = rate * total_Weight + surcharge_Express;
                }
                else if (shipping_Speed == 2 && shipping_Type == 'b' && hawaii == 'n')
                {
                    total_Shipping = rate * total_Weight;
                }
                else if (shipping_Speed == 3 && shipping_Type == 'a' && hawaii == 'y')
                {
                    total_Shipping = rate * total_Items + surcharge_OverNight;
                }
                else if (shipping_Speed == 3 && shipping_Type == 'a' && hawaii == 'n')
                {
                    total_Shipping = rate * total_Items;
                }
                else if (shipping_Speed == 3 && shipping_Type == 'b' && hawaii == 'y')
                {
                    total_Shipping = rate * total_Weight + surcharge_OverNight;
                }
                else if (shipping_Speed == 3 && shipping_Type == 'b' && hawaii == 'n')
                {
                    total_Shipping = rate * total_Weight;
                }
         

            if (shipping_Speed == 1)  Console.WriteLine("Standard Shipping");  
                
            else if (shipping_Speed == 2)  Console.WriteLine("Express Shipping");
               
            else if (shipping_Speed == 3)   Console.WriteLine("Over-Night Shipping");
               
                
            
            if (shipping_Type == 1)
                Console.WriteLine ("{0}", total_Items);
            else if (shipping_Type == 2)
                Console.WriteLine ("{0}", total_Weight);

            Console.WriteLine("Total shipping costs: {0:$}", total_Shipping);
            Console.WriteLine("Thank you for shopping at Red Fern On-Line Electronics!");
            Console.WriteLine("Press any key to continue...");
               


        Console.ReadLine();

Recommended Answers

All 13 Replies

What doesn't work about it?

Also please enclose your code in code tags:

[code]

//code goes here

[/code]

There's still a few minutes left on your editing time for it.

Hint: shipping type is a character, it's not likely to be equal to 1 in this context. In a similar vein, Console.Read() reads in a character and returns the code for that character (so if you put in 10 it would return the ascii code for 1 which is 49). Use the Int32.Parse() just like you did the others. Put those changes in and see where you are.

if (shipping_Type == 1)
            {
                Console.WriteLine("How many items are to be shipped?");
                total_Items = (int)(Console.Read());
            }
            else if (shipping_Type == 2)
                Console.WriteLine("How heavy is your package?(In pounds)");

EDIT: Also, another hint, beware of the fact that I can get your code to skip over all of those if elses in the middle just by entering in A or B or Y or N.

I'm new to C#. I was wondering if using a Switch case statement would be more handy in this case or not? Wouldnt it narrow down the usage of if-else statements?

I'm new to C#. I was wondering if using a Switch case statement would be more handy in this case or not? Wouldnt it narrow down the usage of if-else statements?

In my view, switch case and if/else-if structures are functionally equivalent, the latter has the bonus of not needing to use the keyword break to keep execution from falling into a latter case. This is assuming, of course, you only want 1 case statement to execute.

I think that block can be drastically reduced into a couple of separate passes where the base price is calculated and the extras are added on with another if for each. That was going to be in phase three :)

why the heck you created a console app?

why the heck you created a console app?

Because that is the general learning curve.

why the heck you created a console app?

Also, he did say the code sucked.

Sorry, about my last comment. On a serious note: As a newbie, you really should learn how to debug code. Insert a breakpoint and walk through your code. Use the watch windows to see what is happening. If you need to, get someone to show you how to do this.

Try:

total_Weight = 0;
            if (shipping_Type == 'a')
            {
                Console.WriteLine("How many items are to be shipped?");
                total_Items = int.Parse(Console.ReadLine());
                Console.WriteLine("Amount:" + total_Items.ToString());
            }
            else if (shipping_Type == 'b')
            {
                Console.WriteLine("How heavy is your package?(In pounds)");
                total_Weight = double.Parse(Console.ReadLine());
            }
            else Console.WriteLine("Shipping type is not correct:" + shipping_Type);
            Console.WriteLine("Thank you.");

in the one section that is totally messed up.
1. Alarms went off in my head when I didn't see "total_Weight" initiallized and I managed to hit code to blow up.
2. You ask for an A or B and then check if it is == 1. I believe that will cast char as byte and then see if it is 1. (It wouldn't be true even if you typed 1.)
3. C# is case sensitive and you check for 'a', convert entries to lower case so if someone follows your instructions it still checks out.
4. "total_Items = (int)(Console.Read());" blows up on my machine. For some reason it keeps on reading until a newline is hit. I'm not sure what is going on because it doesn't produce an exception message but it won't execute the debug line I entered after it. (I'm using csc to compile the code because my VS doesn't support C#. I don't know how to create breaaklines in command line mode so, I wrote the poor man's debugger line.It didn't print probably because I was adding a null value)
3. Note that I added a new else at the end. (Assume you client is an idiot and can't follow directions. or in this case Does follow directions and types 'A'
4. I also added a Thank you to keep it consistent.
5. Of course this doesn't solve your problem, it still produces garbage output at the end, but I've given some hints. You solve the problem.

"total_Items = (int)(Console.Read());" blows up on my machine.

I looked into that a little. Console.Read() is meant to get a character and turn it into an int (for its ASCII representation) so if you type in 2 you'll end up sending 50 back (it's supposed to be the getchar() for the .NET world I think). Also, it blocks so I believe if your prompt hasn't cleared the buffer it's going to suppress it. All around not at all the right method to use.

Also, look at using int.TryParse rather than Parse. TryParse will copy the value to an output variable and returns a boolean value to show wether the parse succeeded. int.Parse will throw an exception if the user doesnt enter a valid number.

Console.WriteLine("Please enter which shipping method you want:");
            Console.WriteLine("Enter 1- for Standard shipping.");
            Console.WriteLine("Enter 2- for Express shipping.");
            Console.WriteLine("Enter 3- for Overnight shipping.");
while(!int.TryParse(Console.ReadLine(), out shipping_Speed)
{
            Console.WriteLine("Incorrect value. Please enter a number between 1 and 3:");
}
            Console.WriteLine("Thank you.");

Also, look at using int.TryParse rather than Parse. TryParse will copy the value to an output variable and returns a boolean value to show wether the parse succeeded. int.Parse will throw an exception if the user doesnt enter a valid number.

Console.WriteLine("Please enter which shipping method you want:");
            Console.WriteLine("Enter 1- for Standard shipping.");
            Console.WriteLine("Enter 2- for Express shipping.");
            Console.WriteLine("Enter 3- for Overnight shipping.");
while(!int.TryParse(Console.ReadLine(), out shipping_Speed)
{
            Console.WriteLine("Incorrect value. Please enter a number between 1 and 3:");
}
            Console.WriteLine("Thank you.");

Thank you, I have incorrectly maligned C# because it didn't do just what this does.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.