0

I have the question: The program can delete clothing based on a given serial code.
I do not know how to delete some elements when it based on a given thing. On Case 3, Pls help me.

import javax.swing.*;
public class piashop
{
public static void main (String args[])
    {
      Piafashion piatest[] = new Piafashion [370];
        JOptionPane.showMessageDialog (null, "Welcome to Pia Fashion Shop");
        int no_of_clothing = 0;
        int choice = 0;
        boolean found;
        String piatargetcode, piatargetcode1;
            do
            {
                choice = Integer.parseInt(JOptionPane.showInputDialog (null, "1. Add Product  2. Report  3. Delete"));
                switch(choice)
            {
                case 1:
                    String piascode = JOptionPane.showInputDialog (null, "Enter Brand ID");
                    String piapp = JOptionPane.showInputDialog (null, "Enter Purpose");
                    String piab = JOptionPane.showInputDialog (null, "Enter Brand");
                    String piac = JOptionPane.showInputDialog (null, "Enter Colour");
                    String pias = JOptionPane.showInputDialog (null, "Enter Size");
                    double piapr = Double.parseDouble(JOptionPane.showInputDialog (null, "Enter Price"));
                    piatest [no_of_clothing] = new Piafashion (piascode, piapp, piab, piac, pias, piapr);
                        no_of_clothing++;
                        break;
                //case 1 for adding elements array into database    

                case 2:
                    String piareport ="";
                    for (int x=0; x < no_of_clothing; x++)
                    piareport = piareport + "\nBrand ID is "+piatest[x].piaserial_code + "\nPurpose is " +piatest[x].piapurpose + "\nBrand is " +piatest[x].piabrand + "\nColour is " + piatest[x].piacolour + "\nSize is " + piatest[x].piasize + "\nPrice is " + piatest[x].piaprice +"\n";

                    JOptionPane.showMessageDialog(null, piareport);

                    break;

                //case 2 for displaying the product info                                            

                case 3:
                    }
                        }while(choice!=4);
                        }
    }
1

1 Answer 1

1

I would not suggest using an array - when you have the magic of collections. Deleting an element from an array requires 'reshuffling' the entire array to remove the 'gap' or writing code so that the contents of each location within the array is checked before access. Not to mention arrays are fixed size (must be extended to add more).

You have the generic type ArrayList that can be used here, which supports methods like .remove(T) and .add(T).

Plus this looks like a homework problem :) You probably have a textbook or classmate that can answer this in the context of your class.

P.S. that code is very very badly optimized - an array list would speed it up considerably (or even a HashMap)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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