简体   繁体   中英

In a java project I designed to simulate a dominos home , there is error where the code takes my input for size many times

import java.io.*;
class PizzaDelivery
{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String pizza[] = { "MARGHERITA", "DOUBLE CHEESE MARGHERITA" , "FARM HOUSE", "DELUXE VEGGIE" , "CHEESE N' CORN" , "PEPPERONI PIZZA" , "PERI-PERI CHICKEN" , "NON VEG SUPREME" , "CHICKEN FIESTA" , "CHICKEN SAUSAGE PIZZA" };
    int smallprice[] = { 195,300,285,355,235,205,300,285,355,300};
    int mediumprice[] = { 245,380,325,405,285,275,380,355,405,345};
    int largeprice[] = { 305,435,405,495,305,305,435,425,495,435};
    String sides[] = {"GARLIC BREADTSTICKS" , "STUFFED GARLIC BREADSTICKS" , "CHOCO LAVA CAKE" ,"COCA COLA"};
    int sideprice[] = { 155, 205 , 245 , 75};
    String cart[] = new String[100];
    int bill[] = new int[100];
    int k = 0,areacode;
    String cont,street,house,area;
    String name;
    double total=0.0;
    long phno;
    int f,flag,success;
    public void menu() throws IOException
    {
        String choice;
        int ch;
        System.out.println("**************** WELCOME TO DOMINOS PIZZA HOME DELIVERY ******************");
        System.out.println("Veg pizzas");
        System.out.println("1. MARGHERITA - A hugely popular margherita, with a deliciously tangy single cheese topping");
        System.out.println("2. DOUBLE CHEESE MARGHERITA - The ever popular margherita - loaded with extra cheese.....oodies of it");
        System.out.println("3. FARM HOUSE - A pizza that goes ballistic on veggies! Check out this mouth watering overload of crunchy crisp capsicum, succulent mushrooms and fresh tomatoes");
        System.out.println("4. DELUXE VEGGIE - For a vegetarian looking to eat the creme de la creme pizza for veggie lovers , this is it");     
        System.out.println("5. CHEESE N' CORN - Cheese(tons of it) and corn - what more could someone want?");
        System.out.println("Non-veg pizzas");    
        System.out.println("6. PEPPERONI PIZZA - for all the lovers of classic non veg pizzas out there");
        System.out.println("7. PERI-PERI CHICKEN - Pepper barbeque chicken and Red paprika : the perfect combination");
        System.out.println("8. NON VEG SUPREME - Bite into supreme delight of black olives, Onions , grilled mushrooms , pepper bbq chicken, peri-peri chicken, grilled chicken rashers");
        System.out.println("9. CHICKEN FIESTA - A fiesta of tasty chicken for the refined taste buds");
        System.out.println("10. CHICKEN SAUSAGE - A classic ");
        System.out.println("Sides and beverages");  
        System.out.println("11. GARLIC BREADSTICKS");
        System.out.println("12. STUFFED GARLIC BREAD");
        System.out.println("13. CHOCO LAVA CAKE");
        System.out.println("14. COCA COLA(500ml)");
        System.out.println("You can enter 15 to exit");
        do
        {

            System.out.println("Enter your choice of product");
            choice = br.readLine();
            ch = Integer.parseInt(0 + choice.replaceAll("\\D+",""));

            if(ch<=10 && ch>0)
            {

                switch(ch)
                {
                    case 1:  
                    cart[k] =  pizza[ch-1];
                    if(size()==1)
                        bill[k] = smallprice[ch-1];
                    else if(size()==2)
                        bill[k] = mediumprice[ch-1];
                    else if(size()==3)
                        bill[k] = largeprice[ch-1];
                    System.out.println("the price is: " + bill[k]);
                    System.out.println("The item "+ cart[k] + " has been added to your cart");
                    k++;
                    break;
                    case 2:  
                    cart[k] =  pizza[ch-1];
                    if(size()==1)
                        bill[k] = smallprice[ch-1];
                    else if(size()==2)
                        bill[k] = mediumprice[ch-1];
                    else if(size()==3)
                        bill[k] = largeprice[ch-1];
                    System.out.println("the price is: " + bill[k]);
                    System.out.println("The item "+ cart[k] + " has been added to your cart");
                    k++;
                    break;
                    case 3:  
                    cart[k] =  pizza[ch-1];
                    if(size()==1)
                        bill[k] = smallprice[ch-1];
                    else if(size()==2)
                        bill[k] = mediumprice[ch-1];
                    else if(size()==3)
                        bill[k] = largeprice[ch-1];
                    System.out.println("the price is: " + bill[k]);
                    System.out.println("The item "+ cart[k] + " has been added to your cart");
                    k++;
                    break;
                    case 4:  
                    cart[k] =  pizza[ch-1];
                    if(size()==1)
                        bill[k] = smallprice[ch-1];
                    else if(size()==2)
                        bill[k] = mediumprice[ch-1];
                    else if(size()==3)
                        bill[k] = largeprice[ch-1];
                    System.out.println("the price is: " + bill[k]);
                    System.out.println("The item "+ cart[k] + " has been added to your cart");
                    k++;
                    break;
                    case 5:  
                    cart[k] =  pizza[ch-1];
                    if(size()==1)
                        bill[k] = smallprice[ch-1];
                    else if(size()==2)
                        bill[k] = mediumprice[ch-1];
                    else if(size()==3)
                        bill[k] = largeprice[ch-1];
                    System.out.println("the price is: " + bill[k]);
                    System.out.println("The item "+ cart[k] + " has been added to your cart");
                    k++;
                    break;
                    case 6:  
                    cart[k] =  pizza[ch-1];
                    if(size()==1)
                        bill[k] = smallprice[ch-1];
                    else if(size()==2)
                        bill[k] = mediumprice[ch-1];
                    else if(size()==3)
                        bill[k] = largeprice[ch-1];
                    System.out.println("the price is: " + bill[k]);
                    System.out.println("The item "+ cart[k] + " has been added to your cart");
                    k++;
                    break;
                    case 7:  
                    cart[k] =  pizza[ch-1];
                    if(size()==1)
                        bill[k] = smallprice[ch-1];
                    else if(size()==2)
                        bill[k] = mediumprice[ch-1];
                    else if(size()==3)
                        bill[k] = largeprice[ch-1];
                    System.out.println("the price is: " + bill[k]);
                    System.out.println("The item "+ cart[k] + " has been added to your cart");
                    k++;
                    break;
                    case 8:  
                    cart[k] =  pizza[ch-1];
                    if(size()==1)
                        bill[k] = smallprice[ch-1];
                    else if(size()==2)
                        bill[k] = mediumprice[ch-1];
                    else if(size()==3)
                        bill[k] = largeprice[ch-1];
                    System.out.println("the price is: " + bill[k]);
                    System.out.println("The item "+ cart[k] + " has been added to your cart");
                    k++;
                    break;
                    case 9:  
                    cart[k] =  pizza[ch-1];
                    if(size()==1)
                        bill[k] = smallprice[ch-1];
                    else if(size()==2)
                        bill[k] = mediumprice[ch-1];
                    else if(size()==3)
                        bill[k] = largeprice[ch-1];
                    System.out.println("the price is: " + bill[k]);
                    System.out.println("The item "+ cart[k] + " has been added to your cart");
                    k++;
                    break;
                    case 10:  
                    cart[k] =  pizza[ch-1];
                    if(size()==1)
                        bill[k] = smallprice[ch-1];
                    else if(size()==2)
                        bill[k] = mediumprice[ch-1];
                    else if(size()==3)
                        bill[k] = largeprice[ch-1];
                    System.out.println("the price is: " + bill[k]);
                    System.out.println("The item "+ cart[k] + " has been added to your cart");
                    k++;
                    break;
                }
            }
            else if(ch>10 && ch<=15)
            {
                switch(ch)
                {
                    case 11:  
                    cart[k] =  sides[ch-11];              
                    bill[k] = sideprice[ch-11];
                    System.out.println("the price is: " + bill[k]);
                    System.out.println("The item "+ cart[k] + " has been added to your cart");
                    k++;
                    break;
                    case 12:  
                    cart[k] =  sides[ch-11];                
                    bill[k] = sideprice[ch-11];
                    System.out.println("the price is: " + bill[k]);
                    System.out.println("The item "+ cart[k] + " has been added to your cart");
                    k++;
                    break;
                    case 13:  
                    cart[k] =  sides[ch-11];                
                    bill[k] = sideprice[ch-11];
                    System.out.println("the price is: " + bill[k]);
                    System.out.println("The item "+ cart[k] + " has been added to your cart");
                    k++;
                    break;
                    case 14:  
                    cart[k] =  sides[ch-11];                
                    bill[k] = sideprice[ch-11];
                    System.out.println("the price is: " + bill[k]);
                    System.out.println("The item "+ cart[k] + " has been added to your cart");
                    k++;
                    break;
                    case 15: System.exit(0);
                }

            }
            else
            {
                System.out.println("You have entered a number that doesn't correspond to any item");

            }

                System.out.println("Do you want to continue buying? \n If yes, please enter \"yes\" else enter \"no\" . If you want to exit , please enter yes and then press 15");
                cont = br.readLine();
                if(cont.equalsIgnoreCase("No")==true)
                {
                    return;
                }
                if( cont.equalsIgnoreCase("yes")==true)
                {

                }
                else
                {
                    System.out.println("The input entered by you is not correct. Please enter yes or no");
                    cont = br.readLine();
                }

        }while(cont.equalsIgnoreCase("Yes"));

    }
    int size() throws IOException
    {    
        int size ;
        int f=0;

        do
        {
            System.out.println("Enter the number for the size of the pizza you want (1.Small/2,Medium/3.Large):");
            size = Integer.parseInt(br.readLine());
            if(size>=1 && size<=3)
            {
               break;
            }
            if(size>3 && size<1)
            {
                System.out.println("That number doesnt correspond to a size");
                System.out.println("Please enter a valid choice");
                f=0;
            }    
        }while(f==0);
        return size;
    }
    void bill() throws IOException
    {
        for(int i = k-1;i>=0;i--)
            total+= bill[i];
        System.out.println ("\n\n*********************************  BILL  *****************************************");
        System.out.println ("\n NAME: "+ name);
        System.out.println ("\n-----------------------------------------------------------------------------------------");
        System.out.println ("\nITEMS \t\t\t\t \t COST  ");
        System.out.println ("\n-----------------------------------------------------------------------------------------");
        for(int i = 0;i<k;i++)
        {
            System.out.println(cart[i] + "\t \t \t \t \t" + bill[i]);
        }
        System.out.println("\n Total : \t \t \t \t \t \t" + total);
        System.out.println("SGST: 2.5% \t \t \t \t \t " + (0.025 * total));
        System.out.println("CGST: 2.5% \t \t \t \t \t " + (0.025 * total));
        System.out.println("GRAND TOTAL: \t \t \t \t \t" + (1.05* total));

        System.out.println ("\n************************************************************************************");
        System.out.println ("\n ENTER YOUR AREA NAME ");
        String area =br.readLine();
        System.out.println("\n ENTER YOUR STREET NAME");
        String street = br.readLine();
        System.out.println("\n ENTER YOUR HOUSE NAME , FLAT NUMBER(IF ANY)");
        String house = br.readLine();
        System.out.println("\n ENTER YOUR KOLKATA AREA CODE WITHOUT A SPACE IN BETWEEN");

        do
        {
            try
            {
                f=0;
                areacode = Integer.parseInt(br.readLine());
                if(areacode <= 700000 && areacode >= 700163) 
                {
                    f++;
                    System.out.println("Your area code is not valid");
                    System.out.println("Enter a valid area code");
                }
            }
            catch(Exception e)
            {

                System.out.println("Your area code is not valid");
                System.out.println("Enter a valid area code");
                areacode = Integer.parseInt(br.readLine());
                f++;
            }

        }while(f>0);

        System.out.println ("\n PAYMENT CAN BE MADE IN THE FOLLOWING WAYS:");
        System.out.println ();
        System.out.println ("1. CREDIT CARD OR DEBIT CARD");        
        System.out.println ("2. CASH ON DELIVERY");
        System.out.println ("\n\n ENTER YOUR CHOICE");
        for(int f = -1;f==-1;)
        {
            int paychoice =Integer.parseInt(br.readLine());
            switch (paychoice)
            {
                case 1:
                System.out.println ("ENTER CREDIT/DEBIT CARD NO.:");
                String ccn=br.readLine();
                System.out.println ("ENTER CVV NO.:");
                String cvv=br.readLine();
                System.out.println("Enter the date till which the card is valid");
                String date = br.readLine();
                System.out.println("Enter the name on the card");
                String name_card = br.readLine();
                System.out.println ("\n\n CONGRATULATIONS!!!!!! THE PAYMENT HAS BEEN MADE");   
                f=1;
                break;
                case 2:                
                f=1;
                break;
                default:
                System.out.println ("OOPS!!! WRONG CHOICE");
                System.out.println("Enter your choice again");
                f=-1;
            }

        }
        do
        {
            flag=0;
            System.out.println("Please enter your mobile number");
            System.out.print("+91");
            phno = Long.parseLong(br.readLine());
            int c = 0;
            for(long i = phno;i!=0;i/=10)
            {
                int d = (int)(phno%i);
                c++;
            }

            if(c!=10)
            {
                System.out.println("The entered number is not valid. Please try again");
                flag++;
            }
        }while(flag>0);

        System.out.println("Please enter your email id");
        String emailid = br.readLine();            
        System.out.println("Your product will be delivered within 30mins. If the delivery time is more than 30 mins , you will not have to pay anything. However,if prior information is given regarding any delay in delivery time, this rule will not be valid. Further details will be provided via SMS and email");           
        System.out.println ("                 \n\nFOR FUTHER DETAILS AND QUERIES, CONTACT us using the queries section of dominos website or at the number 033 2461 4505");
        System.out.println ("\n\n******************************************************  THANK YOU  ******************************************************");
        System.out.println ("\n\n************************************************ !!!!! VISIT AGAIN !!!!!  ***********************************************");

    }

    void main() throws IOException
    {
        do
        {
            System.out.println("Enter your name");
            name = br.readLine();
            success=0;
            for(int i = 0;i<name.length();i++)
            {
                char ch = name.charAt(i);
                if(ch==' ')
                {
                    success++;
                }


            }
            if(success==0)
            {
                    System.out.println("Please enter your full name separated by a space");                
            }
        }while(success==0);
        PizzaDelivery obj = new PizzaDelivery();
        obj.menu();
        obj.bill();
    }
}

ERROR: Enter the size is being asked too many times for one pizza

In your main switch code, every case is running the size() method multiple times in the if statements:

if(size()==1)
    bill[k] = smallprice[ch-1];
else if(size()==2)
    bill[k] = mediumprice[ch-1];
else if(size()==3)
    bill[k] = largeprice[ch-1];

You need to run size() once before the switch cases, and save the int value returned by it in a variable which is then checked in the if statements.

代替使用size函数中的“ do-while”循环,只需使用while循环即可。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM