Home > Software design >  Cake Shop Project
Cake Shop Project

Time:08-06

I am trying to create a mock program for a bakery, the program has to be a dynamic program that uses data lists to store all of the cake order information, currently I have created a program that does take user input, and stores it but it outputs my desired output 4 times! I do not understand what is happening. enter image description here

I have included my full code down below:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {


    public static void main(String[] args)throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String name[]=new String[100];
        String flavour [] = new String [100]; 
        String icing[]=new String[100];
        String filling[]=new String[100];
        String decor[]=new String[100];
        String message []=new String[100];
        int i=0;
        while(true){
            System.out.println("\n\n***** Cake Town *****");
            System.out.println("1. Enter Cake Orders/ Add a Cake Order ");
            System.out.println("2. Modify a Cake Order  ");
            System.out.println("3. Delete a Cake Order ");
            System.out.println("4. Sort a Cake Order ");
            System.out.println("5. Show all Cake Order ");
            System.out.println("5. Print to a File ");
            System.out.println("6. Exit ");
            System.out.println("\nChoose an option(1-6):  ");
            int choice=Integer.parseInt(br.readLine());

            switch(choice){
                case 1: System.out.println("Name for the Cake Order :");    
                        name[i]=br.readLine();
                        System.out.println("Pick the flavour of the cake (Strawberry, Chocolate, Vanilla):  "); 
                        flavour[i]=br.readLine();
                        System.out.println("Pick the filling of the cake (Strawberries, Blackberries, and Creme): "); 
                        filling[i  ]=br.readLine();
                        System.out.println("Pick the icing color: "); 
                        icing[i  ]=br.readLine(); 
                        System.out.println("Pick the decorations on your cake(Balloons,Sprinkles,Flowers): ");
                        decor[i  ]=br.readLine();
                        System.out.println("Pick a special message for your cake: ");
                        message[i  ]=br.readLine();
                        break;
                case 2: System.out.println("Enter a order to be modified:  ");
                        String order=br.readLine();int flag=0;
                        for(int j=0;j<i;j  ){
                            if(order.equals(name[j]))
                            {
                                System.out.println("Flavour :" flavour[j]); 
                                System.out.println("\n Filling :" filling[j]); 
                                System.out.println("\n Icing :" icing[j]);
                                System.out.println("\n Decorations: "  decor[j]);
                                System.out.println("\n Message: "  message[j]); 
                                System.out.println("Enter data to be modified:\n flavour, filling, icing, decorations, or message: ");

                                String ch=br.readLine();
                                System.out.println("Enter New Infomation: ");
                                String newvalue=br.readLine();
                                if(ch.equals("flavour"))
                                    flavour[j]=newvalue;
                                if(ch.equals("filling"))
                                    filling[j]=newvalue;
                                if(ch.equals("icing"))
                                    icing[j]=newvalue;
                                if(ch.equals("decorations"))
                                    decor[j]=newvalue; 
                                if(ch.equals("message"))
                                    message[j]=newvalue; 
                                System.out.println("The Order is updated!");
                                flag=1;
                                break;
                            }
                        }
                        if(flag==0)
                            System.out.println("Order not found!! \nPlease try again!!");
                        break;
                case 3:System.out.println("Please enter the name of order to be deleted");
                       order=br.readLine();flag=0;
                       for(int j=0;j<i;j  ){
                           if(order.equals(name[j]))
                           {
                               for(int k=j;k<(i-1);k  ){
                                   name[k]=name[k 1];
                                   flavour[k]=flavour[k 1];
                                   filling[k]=filling[k 1];
                                   icing[k]=icing[k 1];
                                   decor[k]=decor[k 1];
                                   message[k]=message[k 1];
                               }
                               System.out.println("The order is deleted");
                               flag=1;
                               break;
                           }
                       }
                       if(flag==0)
                           System.out.println("Order not found!! \nPlease try again!!");
                       break;
                case 4:System.out.println("What would you like to sort by ? \n  Name, Flavour, Filling, Icing , Decoration, or Message: ");
                       String key=br.readLine();
                       if(key.equals("Name")){
                           for(int j = 0; j<i; j  ) {
                               for (int k = j 1; k<i; k  ) {
                                   if(name[j].compareTo(name[k])>0) {
                                       String temp = name[j];
                                       name[j] = name[k];
                                       name[k] = temp;
                                       temp = flavour[j];
                                       flavour[j] = flavour[k];
                                       flavour[k] = temp;
                                       temp = filling[j];
                                       filling[j] = filling[k];
                                       filling[k] = temp;
                                       temp = icing[j];
                                       icing[j] = icing[k];
                                       icing [k] = temp; 
                                       temp = decor[j]; 
                                       decor[j] = decor[k]; 
                                       decor[k] = temp; 
                                       temp = message[j];
                                       message[j] = message[k]; 
                                       message[k] = temp; 
                                   }
                               }
                           }
                       }
                       if(key.equals("Flavour")){
                           for(int j = 0; j<i; j  ) {
                               for (int k = j 1; k<i; k  ) {
                                   if(flavour[j].compareTo(flavour[k])>0) {
                                       String temp = flavour[j];
                                       flavour[j] = flavour[k];
                                       flavour[k] = temp;
                                       temp = name[j];
                                       name[j] = name[k];
                                       name[k] = temp;
                                       temp = filling[j];
                                       filling[j] = filling[k];
                                       filling[k] = temp;
                                       temp = icing[j];
                                       icing[j] = icing[k];
                                       icing [k] = temp; 
                                       temp = decor[j]; 
                                       decor[j] = decor[k]; 
                                       decor[k] = temp; 
                                       temp = message[j];
                                       message[j] = message[k]; 
                                       message[k] = temp; 

                                   }
                               }
                           }
                       }
                       if(key.equals("Filling")){
                           for(int j = 0; j<i; j  ) {
                               for (int k = j 1; k<i; k  ) {
                                   if(filling[j].compareTo(filling[k])>0) {
                                       String temp = filling[j];
                                       filling[j] = filling[k];
                                       filling[k] = temp;
                                       temp = name[j];
                                       name[j] = name[k];
                                       name[k] = temp;
                                       temp = flavour[j];
                                       flavour[j] = flavour[k];
                                       flavour[k] = temp;
                                       temp = icing[j];
                                       icing[j] = icing[k];
                                       icing [k] = temp; 
                                       temp = decor[j]; 
                                       decor[j] = decor[k]; 
                                       decor[k] = temp; 
                                       temp = message[j];
                                       message[j] = message[k]; 
                                       message[k] = temp; 
                                   }
                               }
                           }
                       }
                       if(key.equals("Icing")){
                           for(int j = 0; j<i; j  ) {
                               for (int k = j 1; k<i; k  ) {
                                   if(filling[j].compareTo(filling[k])>0) {
                                       String temp = icing[j];
                                       icing[j] = icing[k];
                                       icing[k] = temp;
                                       temp = decor[j];
                                       decor[j] = decor[k];
                                       decor[k] = temp;
                                       temp = message[j];
                                       message[j] = message[k];
                                       message[k] = temp;
                                       temp = name[j];
                                       name[j] = name[k];
                                       name [k] = temp; 
                                       temp = flavour[j]; 
                                       flavour[j] = flavour[k]; 
                                       flavour[k] = temp; 
                                       temp = filling[j];
                                       filling[j] = filling[k]; 
                                       filling[k] = temp; 
                                   }
                               }
                           }
                       }
                       if(key.equals("Decoration")){
                           for(int j = 0; j<i; j  ) {
                               for (int k = j 1; k<i; k  ) {
                                   if(filling[j].compareTo(filling[k])>0) {
                                       String temp = decor[j];
                                       decor[j] = decor[k];
                                       decor[k] = temp;
                                       temp = message[j];
                                       message[j] = message[k];
                                       message[k] = temp;
                                       temp = name[j];
                                       name[j] = name[k];
                                       name[k] = temp;
                                       temp = flavour[j];
                                       flavour[j] = flavour[k];
                                       flavour [k] = temp; 
                                       temp = filling[j]; 
                                       filling[j] = filling[k]; 
                                       filling[k] = temp; 
                                       temp = icing[j];
                                       icing[j] = icing[k]; 
                                       icing[k] = temp; 
                                   }
                               }
                           }
                       }
                       if(key.equals("Message")){
                           for(int j = 0; j<i; j  ) {
                               for (int k = j 1; k<i; k  ) {
                                   if(filling[j].compareTo(filling[k])>0) {
                                       String temp = message[j];
                                       message[j] = message[k];
                                       message[k] = temp;
                                       temp = name[j];
                                       name[j] = name[k];
                                       name[k] = temp;
                                       temp = flavour[j];
                                       flavour[j] = flavour[k];
                                       flavour[k] = temp;
                                       temp = filling[j];
                                       filling[j] = filling[k];
                                       filling [k] = temp; 
                                       temp = icing[j]; 
                                       icing[j] = icing[k]; 
                                       icing[k] = temp; 
                                       temp = decor[j];
                                       decor[j] = decor[k]; 
                                       decor[k] = temp; 
                                   }
                               }
                           }
                       }

                       for(int j=0;j<i;j  ){
                           System.out.println("The Sorting was successful!! "   "name :" name[j] "\tfilling :" filling[j] "\tflavour :" flavour[j] "\ticing :" icing[j]   "\tdecoration : " decor[j]   "\tmessage : " message[j]);
                       }
                       break;
                case 5:System.out.println("Here are the sorted orders");
                       for(int j=0;j<i;j  ){
                           System.out.println("name :" name[j] "\tfilling :" filling[j] "\tflavour :" flavour[j]   "\ticing :" icing[j]   "\tdecoration : " decor[j]   "\tmessage : " message[j]);
                       }
                       break;
                case 6:System.exit(1);
                       break;
                default:System.out.println("Sorry that choice is invalid \n Please try again");

            }
        }
    }





}

Any help will be appreciated!

CodePudding user response:

First, I strongly recommend you learn about classes and create class Cake.

Also, rather than use arrays with hard limits. Use an ArrayList<>

In any case, for this loop

for(int j=0;j<i;j  ){
    System.out.println("name :" name[j] "\tfilling :" filling[j] "\tflavour :" flavour[j]   "\ticing :" icing[j]   "\tdecoration : " decor[j]   "\tmessage : " message[j]);
}

You will print i lines. What is i? Well, you've called i many times as you were gathering input.

Ideally, you'd loop up to names.length times, for example rather than use i, and possibly break when names[j] == null... Creating new String[100] will pre-populate that array with 100 null values... ArrayLists don't.

for(int j=0;j<names.length;j  ){
    if (names[j] == null) break;
    System.out.println("name :" name[j] "\tfilling :" filling[j] "\tflavour :" flavour[j]   "\ticing :" icing[j]   "\tdecoration : " decor[j]   "\tmessage : " message[j]);
}

Or, if you had a Cake[] cakes, you could simply do

Arrays.stream(cakes).forEach(System.out::println);

CodePudding user response:

You increment i too many times.

Here's your code.

                case 1: System.out.println("Name for the Cake Order :");    
                        name[i]=br.readLine();
                        System.out.println("Pick the flavour of the cake (Strawberry, Chocolate, Vanilla):  "); 
                        flavour[i]=br.readLine();
                        System.out.println("Pick the filling of the cake (Strawberries, Blackberries, and Creme): "); 
                        filling[i  ]=br.readLine();
                        System.out.println("Pick the icing color: "); 
                        icing[i  ]=br.readLine(); 
                        System.out.println("Pick the decorations on your cake(Balloons,Sprinkles,Flowers): ");
                        decor[i  ]=br.readLine();
                        System.out.println("Pick a special message for your cake: ");
                        message[i  ]=br.readLine();
                        break;

Here's the solution:

                case 1: System.out.println("Name for the Cake Order :");    
                        name[i]=br.readLine();
                        System.out.println("Pick the flavour of the cake (Strawberry, Chocolate, Vanilla):  "); 
                        flavour[i]=br.readLine();
                        System.out.println("Pick the filling of the cake (Strawberries, Blackberries, and Creme): "); 
                        filling[i]=br.readLine();
                        System.out.println("Pick the icing color: "); 
                        icing[i]=br.readLine(); 
                        System.out.println("Pick the decorations on your cake(Balloons,Sprinkles,Flowers): ");
                        decor[i]=br.readLine();
                        System.out.println("Pick a special message for your cake: ");
                        message[i]=br.readLine();
                        i  ;
                        break;
  • Related