Need help with removing a product from the list

Design and code a program that will maintain a list of product names. Use a String type to represent the product name and an array of strings to implement the list. Your program must implement the following methods:

Add a product to the list

Remove a product from the list

Display then entire list

Find out if a particular product is on the list.

You need to create a command command loop with a menu() function. The program must continue asking for input until the user stops.

This is the assignment and this is what I have so far. I need some help writing the remove function.

Thanks

/*

* Title: SimpleSearchableList.java

* Description: this example will show a reasonably efficient and

* simple algorithm for rearranging the value in an array

* in ascending order.

*/

public class SimpleSearchableList {

private static String[] List = new String[25]; //These variables (field variables)

private static int Size; //are common to the entire class, but unavailable

//except to the methods of the class...

public static void main(String[] args)

{

String Cmd;

for(;;) {

Menu();

System.out.print("Command: ");

Cmd = SimpleIO.inputString();

if(Cmd.equals("Quit"))

break;

else if(Cmd.equals("Fill"))

FillList();

else if(Cmd.equals("Search"))

SearchList();

else if(Cmd.equals("Show"))

ShowList();

else if(Cmd.equals("Remove"))

Remove();

}

}

//Tells you what you can do...

public static void Menu()

{

System.out.println("Choices..................................");

System.out.println("\tFill to Enter Product");

System.out.println("\tShow to Show Products");

System.out.println("\tSearch to Search for Product");

System.out.println("\tRemove a Product");

System.out.println("\tQuit");

System.out.println(".........................................");

}

//This method will allow the user to fill an array with values...

public static void FillList()

{

int Count;

System.out.println("Type Stop to Stop");

for(Count = 0 ; Count < List.length ; Count++)

{

System.out.print("Enter Product: ");

List[Count] = SimpleIO.inputString();

if(List[Count].equals("Stop"))

break;

}

Size = Count;

}

//This method will rearrange the values in the array so that

// go from smallest to largest (ascending) order...

public static void SearchList()

{

String KeyValue;

boolean NotFoundFlag;

int Z;

System.out.println("Enter Product Names Below, Stop To Quit");

while(true)

{

System.out.print("Enter: ");

KeyValue = SimpleIO.inputString();

if(KeyValue.equals("Stop")) //Note the use of a method for testing

break; // for equality...

NotFoundFlag = true; //We'll assume the negative

for(Z = 0 ; Z < Size ; Z++)

if(List[Z].equals(KeyValue)) {

NotFoundFlag = false; //If we fine the name, we'll reset the flag

System.out.println(List[Z] + " was found");

}

if(NotFoundFlag)

System.out.println(KeyValue + " was not found");

}

}

//This method will display the contents of the array...

public static void ShowList()

{

int Z;

for(Z = 0 ; Z < Size ; Z++)

System.out.println("Product " + (Z+1) + " = " + List[Z]);

}

public static void Remove()

{

}

}

[3611 byte] By [javanoob21a] at [2007-11-27 11:20:56]
# 1

Don't crosspost

http://forum.java.sun.com/thread.jspa?threadID=5197651

CeciNEstPasUnProgrammeura at 2007-7-29 14:45:38 > top of Java-index,Java Essentials,New To Java...
# 2

There are several ways you could go about this. For one, you could list all the products preceeded by a number and let the user choose a number of the item to remove.

By the way, are you prevented from using ArrayLists? Do you have to use an array?

Other notes:

this is not considered good coding:

for (;;)//!! this is not good

{

Menu();

System.out.print("Command: ");

//Cmd = SimpleIO.inputString();

Cmd = sc.nextLine();//!! I used a Scanner variable here since we don't have your

//!! simpleIO class

if (Cmd.equals("Quit"))

break; //!! better to set some boolean to true and leave loop if true.

else if (Cmd.equals("Fill"))

FillList();

else if (Cmd.equals("Search")) //!! also better to use equalsIgnoreCase()

SearchList();

else if (Cmd.equals("Show"))

ShowList();

else if (Cmd.equals("Remove")) Remove();

}

petes1234a at 2007-7-29 14:45:38 > top of Java-index,Java Essentials,New To Java...
# 3

Yes, I have to use an array

javanoob21a at 2007-7-29 14:45:38 > top of Java-index,Java Essentials,New To Java...
# 4

> Yes, I have to use an array

Pity. My guess is that after you remove an object, you'll have to create a new array and copy all but the removed object.

petes1234a at 2007-7-29 14:45:38 > top of Java-index,Java Essentials,New To Java...
# 5

How would you do it if you could use an arraylist or a list?

javanoob21a at 2007-7-29 14:45:38 > top of Java-index,Java Essentials,New To Java...
# 6

> How would you do it if you could use an arraylist or

> a list?

you would do ArrayList.remove(item to remove);

petes1234a at 2007-7-29 14:45:38 > top of Java-index,Java Essentials,New To Java...