7 Replies - 1236 Views - Last Post: 11 February 2011 - 09:34 AM Rate Topic: -----

#1 Guest_oscarmedia*


Reputation:

How can i shorten this piece of code?

Posted 11 February 2011 - 08:50 AM

a.I'm looking for a way to eliminate the 3 sections of almost duplicate code for the quality so that the total number of lines in the .cs file does not exceed 150.
b.Remove all the hardcoded prices from the literals and instead derive them from an array of prices to make the program easy to maintain if the prices should change.


I'm very new to c# and programming even though the program works i'm looking for a way to make it easy to maintain and less cumbersome.

Thanks in advance for your help.




 
using System;
using System.Collections.Generic;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        (new Program()).run();
    }

    public void run()
    {

        //Initialising the Variables used to store the values used to calculate the price of the pizza

         int quality; // this stores quailty of the pizza selected by the user
         double diameter; // this is the diameter of the pizza
         double total; // this is the total of the pizza order
         double  fixedcharge; // this is the fixed charge based on the selected order
         double pi = 3.14159; // this is PI used to calculate the area of the pizza
         double radius; // this is the radius of the pizza
        string confirm; // this is used to confirm the quailty of the pizza
        string finalconfirm; // this is used to confirm the final order
        string[] menu = new string[3]; // this is an arrary containing the desctiptions of the pizzas

        menu[0] = "Supreme";
        menu[1] = "Best Brand";
        menu[2] = "Regular";

        reenter: // this is used for the goto command so the order can be started from scratch.
        Console.Write("Enter the number of Quality Of Pizza: \nEnter 1 for {0} at 5.00\nEnter 2 for {1}  at 3.00\nEnter 3 for {2}  at 1.50 \n", menu[0], menu[1], menu[2]);
        quality = readInt("");
       
        if (quality == 1) //if supreme is selected then do the following
        {
            Console.Write("You have selected {0} at 5.00 are you sure? Y/N: ", menu[0]); // confirm that the user has selected the correct option
            
            confirm = Console.ReadLine();

        suprediam: // a label to re-enter the size of the pizza
            if (confirm.Equals("y") || confirm.Equals("Y"))
            {
                fixedcharge = 5; // this sets the fixed charge once the user has confirmed the selection.
                diameter = readDouble("Please Enter the diameter in mm: ");
           
                if (diameter < 120 || diameter > 1000)
                {
                    Console.Write("\n Invalid input, minimum allowed is 120mm and Maximum is 1000mm \n");
                    goto suprediam; // if the diameter is wrong go back and enter a valid number
                }
                radius = diameter / 2; // calculates the radius based on the users input
                radius = radius / 1000; // converts the radius form mm to square meter
                radius = radius * radius; 
                total = radius * pi;
        
                total = total * 30;
                total = total + fixedcharge; // adds the fixed charge 
                Console.Write("\nPrice of the Pizza :");
                total = Math.Round(total, 2);
                Console.Write(total);
                Console.Write("\nIs the order of the {0} Pizza with a diameter of ", menu[0]);
                Console.Write(diameter);
                Console.Write("mm correct Y/N: ");
                finalconfirm = Console.ReadLine();

                if (finalconfirm.Equals("n") || finalconfirm.Equals("N"))
                {
                    goto reenter;
                }


            }
            else
            {
                // if the user does not confirm that they selected to correct option, goto the start of the selection.
                goto reenter;
       
            }

        }
        if (quality == 2)
        {
            Console.Write("You have selected {0} at 3.00 are you sure? Y/N: ", menu[1]);
            confirm = Console.ReadLine();

        mdiam:
            if (confirm.Equals("y") || confirm.Equals("Y"))
            {
                fixedcharge = 3;
                diameter = readDouble("Please Enter the diameter in mm: ");
           
                if (diameter < 120 || diameter > 1000)
                {
                    Console.Write("\n Invalid input, minimum allowed is 120mm and Maximum is 1000mm \n");
                    goto mdiam;
                }
                radius = diameter / 2;
                radius = radius / 1000;
                radius = radius * radius;
                total = radius * pi;
              
                total = total * 30;
                total = total + fixedcharge;
                Console.Write("\nPrice of the Pizza :");
                total = Math.Round(total, 2);
                Console.Write(total);
                Console.Write("\nIs the order of the {0} Pizza with a diameter of ", menu[1]);
                Console.Write(diameter);
                Console.Write("mm correct Y/N: ");
                finalconfirm = Console.ReadLine();

                if (finalconfirm.Equals("n") || finalconfirm.Equals("N"))
                {
                    goto reenter;
                }

            }
            else
            {
                goto reenter;

            }
        }


        if (quality == 3)
        {
            Console.Write("You have selected {0} at 1.50 are you sure? Y/N: ", menu[2]);
            confirm = Console.ReadLine();

        ldiam:
            if (confirm.Equals("y") || confirm.Equals("Y"))
            {
                fixedcharge = 1.5;
                diameter = readDouble("Please Enter the diameter in mm: ");
           
                if (diameter < 120 || diameter > 1000)
                {
                    Console.Write("\n Invalid input, minimum allowed is 120mm and Maximum is 1000mm \n");
                    goto ldiam;
                }
                radius = diameter / 2;
                radius = radius / 1000;
                radius = radius * radius;
                total = radius * pi;
              
                total = total * 30;
                total = total + fixedcharge;
                Console.Write("\nPrice of the Pizza : ");
                total = Math.Round(total, 2);
                Console.Write(total);
                Console.Write("\nIs the order of the {0} Pizza with a diameter of ", menu[2]);
                Console.Write(diameter);
                Console.Write("mm correct Y/N: ");
                finalconfirm = Console.ReadLine();

                if (finalconfirm.Equals("n") || finalconfirm.Equals("N"))
                {
                    goto reenter;
                }

            }
            else
            {
                goto reenter;

            }
        }
        else
        {
            Console.Write("Invalid Input try again \n");
            goto reenter;

        }

        
    }

 
    private int readInt(string p)
    {
        Console.Write(p);
        string line = Console.ReadLine();
        return int.Parse(line);
    }

    private double readDouble(string p)
    {
        Console.Write(p);
        string line = Console.ReadLine();
        return double.Parse(line);
    }
}



Is This A Good Question/Topic? 0

Replies To: How can i shorten this piece of code?

#2 SpeedisaVirus   User is offline

  • Baller

Reputation: 115
  • View blog
  • Posts: 855
  • Joined: 06-October 08

Re: How can i shorten this piece of code?

Posted 11 February 2011 - 08:53 AM

You already stated what you need to do in b...so what's the issue there? Take the duplicate code, make a new function, and pass the parameters required to complete the output. Call that function in place of the old code.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: How can i shorten this piece of code?

Posted 11 February 2011 - 09:00 AM

goto? SERIOUSLY??? Is someone TEACHING you goto? Please say no...

Elimination of code requires the use of functions/methods.

You need a Pizza class and an Order class.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_oscarmedia*


Reputation:

Re: How can i shorten this piece of code?

Posted 11 February 2011 - 09:08 AM

View PostSpeedisaVirus, on 11 February 2011 - 08:53 AM, said:

You already stated what you need to do in b...so what's the issue there? Take the duplicate code, make a new function, and pass the parameters required to complete the output. Call that function in place of the old code.

As i said i am very new to c# and programming that's why I have come here for guidance on how to do that and as for the goto loops the do's and do wile got confusing for me
Was This Post Helpful? 0

#5 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: How can i shorten this piece of code?

Posted 11 February 2011 - 09:10 AM

Quote

goto loops the do's and do wile got confusing for me


Well, I can make goto much more simple for you: don't use them. Gotos are bad.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: How can i shorten this piece of code?

Posted 11 February 2011 - 09:11 AM

Quote

a.I'm looking for a way to eliminate the 3 sections of almost duplicate code for the quality so that the total number of lines in the .cs file does not exceed 150.
b.Remove all the hardcoded prices from the literals and instead derive them from an array of prices to make the program easy to maintain if the prices should change.


This looks very much like a homework assignment description with the words "I'm look for a way to" stuck in front of it.

Assignment said:

a. Eliminate the 3 sections of almost duplicate code for the quality so that the total number of lines in the .cs file does not exceed 150.
b.Remove all the hardcoded prices from the literals and instead derive them from an array of prices to make the program easy to maintain if the prices should change.


You've been given good great advice about what process to go through. I hope you weren't expecting someone to re-code your project for you.
Was This Post Helpful? 0
  • +
  • -

#7 SpeedisaVirus   User is offline

  • Baller

Reputation: 115
  • View blog
  • Posts: 855
  • Joined: 06-October 08

Re: How can i shorten this piece of code?

Posted 11 February 2011 - 09:12 AM

Well, I just pretty much told you what to do to shorten the code. I will not do it for you. Also, don't use goto. You are not advanced enough to know the very rare circumstances in which to use it. Call use loops, recursion, or any other number of methods but not goto.
Was This Post Helpful? 1
  • +
  • -

#8 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: How can i shorten this piece of code?

Posted 11 February 2011 - 09:34 AM

I see by the Brittish pound symbol you're in the UK. By any chance is your instructor from India? Its not your fault your code is designed this way, you are just doing as you are taught. But honestly the design architecture for this is horrific. It looks like the kind of code produced by Indian out-source companies, that later wind up being contracted out for complete re-write.

If you are paying for this teaching, then you should see what you can do to get your money back, or transferred to another instructor. Your money would be better spent buying self-teaching books from the local book store, then spend the same number of hours each day on teaching yourself and doing how-to tutorials as homework.

May I suggest picking up a basic C# introductory book? There are so many great "How do I build my first application" tutorials on the web... There are dozens of "Learn C# in 21 days", "My first C# program" books at your local book seller or even public library.

C# resources. Start here

The tutorials below walk through making an application including inheritance, custom events and custom controls.
Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2
Debugging tutorial
Working with environmental variables

Intro to C# online tutorial

Build a Program Now! in Visual C# by Microsoft Press, ISBN 0-7356-2542-5
is a terrific book that has you build a Windows Forms application, a WPF app, a database application, your own web browser.

C# Cookbooks
Are a great place to get good code, broken down by need, written by coding professionals. You can use the code as-is, but take the time to actually study it. These professionals write in a certain style for a reason developed by years of experience and heartache.

Microsoft Visual Studio Tips, 251 ways to improve your productivity, Microsoft press, ISBN 0-7356-2640-5
Has many, many great, real-world tips that I use all the time.

Writing a text file is always one of the first things people want to do, in order to store data like high-scores, preferences and so on
Writing a text file tutorial.

These are just good every-day references to put in your bookmarks.
MSDN C# Developers Center with tutorials
Welcome to Visual Studio

Have you seen the 500+ MSDN Code Samples? They spent a lot of time creating samples and demos. It seems a shame to not use them.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1