4
\$\begingroup\$

I made a mobile store, with prices in Indian rupees. Now I went for nested switch over functions, because I wanted to try out something new - biggest mistake in my life.

Since 3 days I've been trying to perfect it, it just won't happen. I have to make sure the menu shows again if negative number or a character is entered. There is try-catch for this, but it's bugging me.

Now my coding is huge and I'm not sure I can post the whole thing here.

I cut out the 4th case in switch (ch) to get some space.

import java.io.*;
import java.util.Date;

class Mobile_Store_Project    
{
    public static void main (String[] args) throws IOException
    {
        InputStreamReader isr=new InputStreamReader (System.in);
        BufferedReader br=new BufferedReader (isr);

        int ch=0,a,gt=0;
        int pr[]=new int [10];
        int qu[]=new int [10];
        String x[]=new String[10];
        for (a=0;a<100;a++)
        {

            try
            {
                System.out.println(" Welcome to the Mobile Store. We have mobiles of all major manufacturers. \n \n Menu: \n 1)Apple \n 2)Samsung \n 3)Nokia \n 4)HTC \n 5)Bill \n 6)Exit \n Enter the number of your choice.");

                ch=Integer.parseInt(br.readLine());

                switch (ch)
                {
                    case 1: int g; 
                    System.out.println("Apple makes the popular iPhones. We are currently stocking: \n 1)iPhone 5 \n 2)iPhone 4S \n 3)iPhone 4 \n 4) Back \nEnter the no of your choice.");
                    g=Integer.parseInt(br.readLine());
                    if (g<0)
                    {
                        System.out.println("Negative numbers not allowed. Lets try this again. Lets try this again.");
                        g=0;
                        continue;
                    }

                    switch (g)
                    { 
                        case 1: int c1;
                        System.out.println("The iPhone 5 is the latest iPhone. Specifications: \n 1)4 inch retina display screen \n 2)OS: iOS 6 \n 3)8MP Camera with Panorama,HDR option. \n \n There are 6 options available.");

                        System.out.println ("1)16 GB White. Price Rs.45,504 \n 2)16 GB Black. Price Rs.45,504 \n 3)32 GB White.Price Rs.48,708 \n 4)32GB Black.Price Rs.48,708 \n 5)64 GB White. Price Rs.50,434 \n 6)64GB Black. Price Rs.50,434 \n Enter the number of your choice");
                        c1=Integer.parseInt(br.readLine());
                        if (c1<0)
                        {
                            System.out.println("Negative numbers not allowed. Lets try this again. Lets try this again.");
                            c1=0;
                            continue;
                        }
                        switch (c1)
                        { 
                            case 1: x[a]="iPhone 5 16GB White ";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=45504;
                            a++;

                            break;
                            case 2: x[a]="iPhone 5 16GB Black ";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=45504;

                            break;
                            case 3: x[a]="iPhone 5 32GB White ";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=48708;
                            break;

                            case 4: x[a]="iPhone 5 32GB Black ";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=48708;
                            break;

                            case 5: x[a]=("iPhone 5 64GB White ");
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=50434;
                            break;

                            case 6: x[a]="iPhone 5 16GB White ";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=50434;
                            break;
                            default: System.out.println("Oops, lets try this again.");
                        }
                        break;
                        case 2: int c2;
                        System.out.println("The iPhone 4S is the next popular phone from Apple. The specs are quite similar to the iPhone 5. Specifications: \n 1)3.5 inch retina display screen \n 2)OS: iOS 5 (Upgradable to iOS 6) \n 3)8MP Camera with Panorama,HDR option. \n \n There are 6 options available.");

                        System.out.println ("1)16 GB White. Price Rs.30,504 \n 2)16 GB Black. Price Rs.30,504 \n 3)32 GB White.Price Rs.35,708 \n 4)32GB Black.Price Rs.35,708 \n 5)64 GB White. Price Rs.40,434 \n 6)64GB Black. Price Rs.40,434 \n Enter the number of your choice");
                        c2=Integer.parseInt (br.readLine());
                        if (c2<0)
                        {
                            System.out.println("Negative numbers not allowed. Lets try this again. Lets try this again.");
                            c2=0;
                            continue;
                        }
                        switch (c2)
                        { 
                            case 1: x[a]="iPhone 4S 16GB White ";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=30504; 
                            break;
                            case 2: x[a]="iPhone 4S 16GB Black ";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=30504;
                            break;

                            case 3: x[a]="iPhone 4S 32GB White ";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=35708;
                            break;

                            case 4: x[a]="iPhone 4S 32GB Black ";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=35708;
                            break;

                            case 5: x[a]="iPhone 4S 64GB White ";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=40434;
                            break;

                            case 6: x[a]="iPhone 4S 16GB White ";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=40434;
                            break;

                            default: System.out.println("Oops, lets try this again.");

                        }
                        break;
                        case 3: int ch3;
                        System.out.println("The iPhone 4 was the first iPhone to sport the glass and aluminium look. Specifications: \n 1)3.5 inch retina display screen \n 2)OS: iOS 4 (Upgradable to iOS 6) \n 3)5MP Camera with HDR option. \n \n There are 6 options available.");
                        System.out.println ("1)16 GB White. Price Rs.20,104 \n 2)16 GB Black. Price Rs.20,104 \n 3)32 GB White.Price Rs.23,658 \n 4)32GB Black.Price Rs.23,658 \n 5)64 GB White. Price Rs.27,123 \n 6)64GB Black. Price Rs.27,123 \n Enter the number of your choice");
                        ch3=Integer.parseInt(br.readLine());
                        if (ch3<0)
                        {
                            System.out.println("Negative numbers not allowed. Lets try this again. Lets try this again.");
                            ch3=0;
                            continue;
                        }
                        switch (ch3)
                        { 
                            case 1: x[a]="iPhone 4 16GB White ";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=20104;
                            break;

                            case 2: x[a]="iPhone 4 16GB Black ";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=20104;
                            break;

                            case 3: x[a]="iPhone 4 32GB White ";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=23658;
                            break;

                            case 4: x[a]="iPhone 4 32GB Black ";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=23658;
                            break;

                            case 5: x[a]="iPhone 4 64GB White ";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=27123;
                            break;

                            case 6: x[a]="iPhone 4 16GB White ";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=27123;
                            break;

                            default: System.out.println("Oops, lets try this again.");

                        }
                        break;
                        case 4:
                        break;
                        default: System.out.println("Oops, lets try this again.");
                    }
                    break;
                    case 2: System.out.println("Samsung has revolutionized the smartphone market with it's amazing phones. We are currently stocking: \n 1)Galaxy S3 \n 2)Galaxy S Duos \n 3)Note 2 \n 4) Back \n Enter the number of you choice");
                    int ch1;
                    ch1=Integer.parseInt(br.readLine());
                    if (ch1<0)
                    {
                        System.out.println("Negative numbers not allowed. Lets try this again. Lets try this again.");
                        ch1=0;
                        continue;
                    }
                    switch (ch1)
                    { 
                        case 1: System.out.println ("The Samsung Galaxy S3 is one of the most popular selling Android devices, selling 30 million+ of them. Specifications: \n 1)4.7 inch AMOLED Screem \n 2)8MP Camera \n 3)1.4 GhZ Quad Core Processor\n There is only one memory option available, 16 GB, but there are two colour options available: \n 1)Marble White. Price Rs.31,454 \n 2)Pebble  Blue. Price Rs.31,454 \n Enter the number of your choice. ");
                        int cho1;
                        cho1=Integer.parseInt (br.readLine());
                        if (cho1<0)
                        {
                            System.out.println("Negative numbers not allowed. Lets try this again. Lets try this again.");
                            cho1=0;
                            continue;
                        }
                        else if (cho1==1)
                        { x[a]="Samsung Galaxy S3 16 GB Marble White";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=31454;

                        }
                        else if (cho1==2)
                        { x[a]="Samsung Galaxy S3 16 GB Pebble Blue";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=31454;

                        }
                        else
                            System.out.println("Oops. Lets try this again.");
                        break;

                        case 2: System.out.println ("The Samsung Galaxy S Duos is the look alike of S3, but with lower specs and dual sim option. Specifications: \n 1)1GhZ Dual Core processor \n 2)5 MP Camera \n 3)Android 4.0.4 Ice Cream Sandwich \n There is only one option available: 1)8GB White. Price Rs.14,999. \n Enter 1 to select.");
                        int ch2;
                        ch2=Integer.parseInt (br.readLine());
                        if (ch2<0)
                        {
                            System.out.println("Negative numbers not allowed. Lets try this again. Lets try this again.");
                            ch2=0;
                            continue;
                        }
                        else if (ch2==1)
                        {
                            x[a]="Samsung Galaxy S Duos 8GB White ";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=14999;

                        }  
                        if (ch2>1)
                        {
                            System.out.println("Oops. Lets try this again.");
                            ch2=0;
                            continue;
                        }
                        break;

                        case 3: System.out.println ("The Samsung Galaxy Note 2 is the sucessor of the sucessful Galaxy Note. Specifications: \n 1)5.5 inch screen \n 2)Android 4.1 Jelly Bean \n 3)1.5 GhZ Quad Core Processor \n 4)S Pen for super productivity \n The phone is available for Rs.38,656 and only in white option. \n Enter 1 to select.");
                        int ch3;
                        ch3=Integer.parseInt(br.readLine());
                        if (ch3<0)
                        {
                            System.out.println("Negative numbers not allowed. Lets try this again. Lets try this again.");
                            ch3=0;
                            continue;
                        }
                        else if (ch3==1)
                        {
                            x[a]="Samsung Galaxy Note 2 16 GB White ";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");;
                            pr[a]=38656;

                        }
                        if (ch3>1)
                        {
                            System.out.println("Oops. Lets try this again.");
                            ch3=0;
                            continue;
                        }
                        break;
                        case 4:
                        break;
                        default: System.out.println("Oops, lets try this again.");
                    }
                    break;
                    case 3: System.out.println("Nokia is a household name. Everyone has owned atleast one Nokia phone in their life. We are currently stocking: \n 1)Nokia Lumia 920 \n 2)Nokia Lumia 820 \n 3) Nokia Lumia 510. \n 4) Back \n Enter the number of your choice");
                    int ch2;
                    ch2=Integer.parseInt (br.readLine());
                    if (ch2<0)
                    {
                        System.out.println("Negative numbers not allowed. Lets try this again. Lets try this again.");
                        ch2=0;
                        continue;
                    }
                    switch (ch2)
                    {
                        case 1: System.out.println ("The Nokia Lumia 920 is a beautiful looking device. Specifications: \n 1)4.5 inch PureMotion+ screen \n 2)Windows Phone 8 OS \n 3)8.7MP Pureview+ Camera with Optical Image Stabilization (OIS) \n There is only one memory option available, 32 GB. There are 3 color options available : \n 1)Blue. Price Rs.27,545  \n 2)Red. Price Rs.27,545   \n 3)Yellow. Price Rs.27,545   \n Select the number of your choice.");
                        int cho1;
                        cho1=Integer.parseInt (br.readLine ());
                        if (cho1<0)
                        {
                            System.out.println("Negative numbers not allowed. Lets try this again. Lets try this again.");
                            cho1=0;
                            continue;
                        }
                        switch (cho1)
                        {
                            case 1:x[a]="Nokia Lumia 920 Blue 32 GB ";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=27545; 
                            break;

                            case 2:x[a]="Nokia Lumia 920 Red 32 GB ";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=27545;
                            break;   

                            case 3:x[a]="Nokia Lumia 920 Yellow 32 GB ";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=27545;
                            break;

                            default: System.out.println("Oops, lets try this again.");
                        }
                        break;

                        case 2:System.out.println ("The Nokia Lumia 820 is another great phone from Nokia. Specifications: \n 1)4.5 inch PureMotion+ screen \n 2)Windows Phone 8 OS \n 3)8 MP Camera \n There is only 32 GB memory option available. There are 3 colour choice available: \n 1)Blue. Price Rs.22,545 \n 2)Red. Price Rs.22,545 \n 3)Yellow. Price Rs.22,545 \n Select the number of your choice.");
                        int cho2;
                        cho2=Integer.parseInt(br.readLine());
                        if (cho2<0)
                        {
                            System.out.println("Negative numbers not allowed. Lets try this again. Lets try this again.");
                            cho2=0;
                            continue;
                        }
                        switch (cho2)
                        { 
                            case 1: x[a]="Nokia Lumia 820 32GB Blue ";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=22545;
                            break;

                            case 2: x[a]="Nokia Lumia 820 32GB Red ";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=22545;
                            break;

                            case 3: x[a]="Nokia Lumia 820 32GB Yellow ";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=22545;
                            break;

                            default: System.out.println("Oops, lets try this again.");
                        }
                        break;
                        case 3: System.out.println ("The Nokia Lumia 510 is a budget Windows Phone from Nokia. Specifications: \n 1)3.7 inch screen \n 2)5MP Camera \n 3)Windows Phone 7.5 (Upgradable to 7.8) \n There are 2 colour options available: \n 1)Black. Price Rs.9999 \n 2)White. Price Rs.9999 \n Enter the number of your choice.");
                        int cho3;
                        cho3=Integer.parseInt (br.readLine());
                        if (cho3<0)
                        {
                            System.out.println("Negative numbers not allowed. Lets try this again. Lets try this again.");
                            cho3=0;
                            continue;
                        }
                        else if (cho3==1)
                        {
                            x[a]="Nokia Lumia 510 4GB Black";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=9999;

                        }
                        else if (cho3==2)
                        {
                            x[a]="Nokia Lumia 510 4GB White";
                            System.out.println("How many do you want to buy?");
                            qu[a]=Integer.parseInt (br.readLine());
                            System.out.println(qu[a]+" "+x[a]+"Added to cart.");
                            pr[a]=9999;

                        }
                        else 
                        {
                            System.out.println("Oops. Lets try this again.");
                            continue; }

                        break;
                        case 4:
                        break;
                        default: System.out.println("Oops, lets try this again.");        
                    }
                    break;

                    case 5:                                         //Printing bill

                    Date date = new Date();
                    System.out.println("\t\t\t\t\t\tMobile Store\n");
                    System.out.println("\t\t\t\t\t\t\t\t\tBill Date : "+date.toString()+"\n");
                    int i,j=1;
                    System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
                    System.out.println("Sr.No       "+setSpace("ITEM",17)+"          "+setSpace("PRICE",17)+"      "+setSpace("QUANTITY",12)+"          "+setSpace("TOTAL",25));
                    System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");

                    for (a=0;a<qu[a];a++)
                    {
                        System.out.println(j+"    "+setSpace(""+x[a],25)+"      "+setSpace(""+pr[a],20)+"      "+setSpace(""+qu[a],20)+"     "+setSpace(""+pr[a]*qu[a],20));
                        j++;
                        gt+=pr[a]*qu[a];
                    }
                    System.out.println("\t\t\t\t\tGrand Total :- Rs"+(gt)+"/-\n\n\n");
                    System.out.println("\t\t\t\tThank you. Visit Again.");
                    System.exit(0);
                    break;
                } // End of main switch

            }
            catch(NumberFormatException nfe)
            {
                System.out.println("Oops. Lets try this again.");
                ch=0;
            }

        }
    }

    static  String setSpace(String data,int size) // Aligning the bill
    {
        int len=data.length();
        if(len<size)
        {
            for(int i=0;i<size-len;i++)
            {
                data+=" ";
            }
        }
        else
        {
            data=data.substring(0,size-2);
            data+="..";
        }
        return data;
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Hi Siddharth. What exactly is your problem? Is it that you want a review of the code or it's just not working. If the latter you will likely find more assistance on this on Stack overflow. \$\endgroup\$ – dreza Nov 29 '12 at 19:15
  • \$\begingroup\$ I want a review. Do you think it's sensible to use Nested Switch and Try Catch? \$\endgroup\$ – Sid Nov 29 '12 at 19:17
  • 3
    \$\begingroup\$ You said it yourself. Using a nested switch over functions is the biggest mistake you've made. Especially when it's that long. You've broken several good coding practices here. Try searching this site for DRY and SOLID principles and patterns and you'll get a whole lot of input on doing it better. \$\endgroup\$ – Lars-Erik Nov 29 '12 at 19:38
  • \$\begingroup\$ It can be easily crashed by entering a non-number string. Nice to check for negative numbers but you have to check if it's really a number first. \$\endgroup\$ – Sulthan Nov 29 '12 at 22:18
  • \$\begingroup\$ Your public static void main() was missing String[] args. I've added it for you (against our normal policy not to edit the code in a question). \$\endgroup\$ – 200_success Feb 8 '15 at 18:36
5
\$\begingroup\$

Crikey there's alot of duplicated code in there. Before I would even consider reviewing it too closely you might want to think about refactoring all those copy and pasted case statements into one method

  1. Remove code that has very likely been "copied and pasted". Oh how I cringe at copy and pasted code:

    x[a]="iPhone 5 16GB Black ";
    System.out.println("How many do you want to buy?");
    qu[a]=Integer.parseInt (br.readLine());
    System.out.println(qu[a]+" "+x[a]+"Added to cart.");
    pr[a]=45504;
    

    into something like

    case 1:
       capturePhoneSelection(a, "iPhone 5 16GB White", 45504);
       a++;
       break;     
    

    where the method would be something like (x, qu and pr could be class instance variables):

    int capturePhoneSelection(int selection, string phoneTitle, int pr) {
        x[selection]=phoneTitle;
    
        System.out.println("How many do you want to buy?");
        qu[selection]=Integer.parseInt (br.readLine());
    
        System.out.println(qu[selection]+" "+x[selection]+"Added to cart.");
        pr[selection]=pr;
    
        return selection++;
    }
    
  2. Change your variable names into something that other people not reading your code can easily understant. i.e what the heck is variable a, pr and qu supposed to represent?

  3. Going on from point 1. Break that main up into many smaller functions. By doing this you will notice duplicated code and so eliminate alot

  4. And No, a nested switch is probably not a good idea but I haven't reviewed it in enough detail to fully comment that.

These might be a good place to start. After making these changes I'm sure there'll be even more room to refactor and review and in this process it's always good to manage in an iterative process.

\$\endgroup\$
  • \$\begingroup\$ Don't forget the use of arrays instead of switch statements. \$\endgroup\$ – Sulthan Nov 29 '12 at 21:55
  • \$\begingroup\$ @Sulthan yes, good option. I thought might be worthwhile to suggest something like that after the OP had done an initial round of refactoring?? \$\endgroup\$ – dreza Nov 29 '12 at 22:02
4
\$\begingroup\$

Now I went for nested switch over functions, cause I wanted to try out something new- biggest mistake in my life.

I hope you never think that's a good idea again.

You could also really use a function to show a menu:

int showMenu(String header, String [] options)
{ ... }

This function takes the menu information as arguments, and would return the selected option. It would handle all the pieces about redisplaying the menu, etc.

The whole thing would be way better if it would read the information from an external file rather then putting all of that in the code.

\$\endgroup\$
1
\$\begingroup\$

Normally I'd give more detailed advice and break things down but there is a lot wrong there. Too much to tackle in a simple review.

There seems to be a lack of understanding of the finer points of program design and architecture. That's fine though. We all have to learn things for the first time at least once!

I would recommend picking up something like:

Head First Design Patterns.

It will explain ways of making code reusable and how to design around a problem.


As a general rule, a switch statement is a code smell. You are saying that a series of alternate steps need to be done, The only time you should use a Switch is when those steps are always the same.

for example, switching on days of the week is fine. there will never be the need to add one.

In your case, you might want to add additional products, so looping through a collection of actions is better.

Then, in the case you need a new product option you add it to the list.

e.g:

(now this is a simplified pseudocode mish mash version guys, not meant to be code reviewed :P)

 string[] displays;
 int[] ProductPurchases;

 ... main()
  {
     ShowProducts();
     while(userChoice.equals("exit"))
    {
        "Which Item Would you like to buy"...
        userChoice = ReadLine();
        if(userChoice > 0 && userChoice < displays.Length)
        {
            "How Many"...
             int amount = ParseInput();
             ProductPurchases[userChoice] = amount;
        }

    }
    ShowPurchases();

  }


 void ShowProducts()
{
 for(int i = 0;i < displays.length; i++)
 {
   System.out.print(displays[i]);
   System.out.print("**********");
 }
}

 void ShowPurchases()
{
 for(int i = 0;i < displays.length; i++)
 {
   if(ProductPurchases[i] != 0)
    {   
      System.out.print("You Ordered "+ProductPurchases[i]+" of "+displays[i]);
       System.out.print("**********");
    }
 }
}

So see how the ability to add products is far simpler, I know it does not have some of the nested menu features you had, but if you had some custom classes in there to store things and managed the state using a command/state pattern you could even do nested menus cleanly.

Good luck!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.