Results 1 to 5 of 5

Thread: Deleteing entries from arrays

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    2

    Deleteing entries from arrays

    Hi folks.

    I am just learning Java, and am curious as to whether or not you can help me figure something out.

    For reference, here is my code:

    Code:
    import java.text.*;
    
    public class JavaMart3
    {
    	public static void main (String[] args)
    	{
    
    
    		//Declare and Initialize Variables
    		boolean ismore = true;
    		String[][] deptCode = new String[13][2];
    		String[] itemNum = new String[5];
    		String[] itemDesc = new String[5];
    		String[] name = new String[25];//To display department name
    		String letters;
    		String stringConverter; //Used to sort array
    		double[] unitPrice = new double[5];
    		double doubleConverter; //Used to sort array
    		double totalCost = 0;
    		int[] itemQuant = new int[5];
    		int choice = 0;
    		int count = 0; //Count the number of transactions
    		int flag = 0;
    		int verify = 0;  //Test Variable
    		int test = 0; //Test Variable			Both used for searching
    		int i = 0;
    		int length;
    		int intConverter; //used to sort array
    
    		//For Formatting numbers
    		NumberFormat cf = NumberFormat.getCurrencyInstance();
    		NumberFormat nf = NumberFormat.getNumberInstance();
    
    		//Heading for Program
    		System.out.println("\t\t\t\tJavaMart Inventory System");
    		System.out.println("\t\t\tProgrammed by Cameron Platt");
    		System.out.println("\t\t\tFor Karyn Mart");
    
    
    		//Load Department Info into array...Can probably be done easier
    		deptCode[0][0] = "AU";	deptCode[0][1] = "Automotive";
    		deptCode[1][0] = "CO";	deptCode[1][1] = "Cosmetics";
    		deptCode[2][0] = "CW";	deptCode[2][1] = "Children's Wear";
    		deptCode[3][0] = "EL";		deptCode[3][1] = "Electronics";
    		deptCode[4][0] = "FD";		deptCode[4][1] = "Food";
    		deptCode[5][0] = "FR";		deptCode[5][1] = "Furniture";
    		deptCode[6][0] = "HW";	deptCode[6][1] = "Hardware";
    		deptCode[7][0] = "JE";		deptCode[7][1] = "Jewlery";
    		deptCode[8][0] = "KB";	deptCode[8][1] = "Kitchen & Bath";
    		deptCode[9][0] = "MW";	deptCode[9][1] = "Men's Wear";
    		deptCode[10][0] = "PH";	deptCode[10][0] = "Pharmacy";
    		deptCode[11][0] = "TO";	deptCode[11][1] = "Toys";
    		deptCode[12][0] = "WW";	deptCode[12][1] = "Women's Wear";
    
    
    		do
    		{//Begin do loop
    
    			choice = 0;
    			flag = 0;
    
    			System.out.println("\n\t\t1. Add an Inventory Item");
    			System.out.println("\t\t2. Modify an Inventory Item");
    			System.out.println("\t\t3. Delete an Inventory Item");
    			System.out.println("\t\t4. Inventory Detail by Department");
    			System.out.println("\t\t5. Inventory Summary by Department");
    			System.out.println("\t\t6. Exit the Program");
    
    			System.out.print("\n\t\tEnter your choice (1-6):");
    			choice = TextIO.getlnInt();
    
    			if ((choice < 1) || (choice > 6))
    			{
    				System.out.println("**ERROR** You must enter a number 1 through 6");
    				ismore = true;
    			}//end if
    
    			if (choice == 1)
    			{
    				//Read in and verify Item Number
    				do
    				{
    					verify = 0;
    					i=0;
    					test = 0;
    
    					System.out.print("\nPlease enter the Item number:");
    					itemNum[count] = TextIO.getlnString();
    					itemNum[count] = itemNum[count].toUpperCase();
    
    					//Validation of Length
    					if (itemNum[count].length() == 5)
    					{
    						//Validation of 2 Letters
    
    						letters = itemNum[count].substring(0,2);
    						letters = letters.toUpperCase();
    
    						while ((test == 0) && (verify < 13))
    						{
    							if (letters.equals(deptCode[verify][0]))
    							{
    								test = 1;
    							}// end if
    							else
    							{
    								i++;
    								verify++;
    								test = 0;
    							}//end else
    						}//end while
    					if (test == 1)
    					{
    						//Validation of 3#'s
    						if ((!Character.isLetter(itemNum[count].charAt(2))) && (!Character.isLetter(itemNum[count].charAt(3))) && (!Character.isLetter(itemNum[count].charAt(4))))
    						{
    							flag = 0;
    						}//end if
    						else
    						{
    							System.out.println("**ERROR03** Item Number must end with 3 numbers");
    						}//End else
    					}//end if
    						else
    						{
    							System.out.println("**ERROR02** Item Number does not exist");
    							flag = 1;
    						}//end else
    					}// end if
    					else
    					{
    						System.out.println("**ERROR01** Item Number must be 5 characters");
    						flag = 1;
    					}//end else
    				} while (flag == 1); //end inner do while loop
    
    			name[count] = deptCode[verify][1];
    
    			//Read in and Verify item description
    			do
    			{
    				System.out.print("\nPlease enter Item Description:");
    				itemDesc[count] = TextIO.getlnString();
    				length = itemDesc[count].length();
    
    				if ((length > 0) && (length <= 20))
    				{
    					flag = 0;
    				}//End if
    				else
    				{
    					System.out.println("**ERROR04** Item Description is required, and must be max 20 characters");
    					flag = 1;
    				}//end else
    			} while (flag ==1); //End inner do while loop
    
    			//Read in and Verify Unit Price
    			do
    			{
    				System.out.print("\nPlease enter unit price:");
    				unitPrice[count] = TextIO.getlnDouble();
    
    				if ((unitPrice[count] >= 0.0) && (unitPrice[count] <=5000))
    				{
    					flag = 0;
    				}//end if
    				else
    				{
    					System.out.println("**ERROR05** -  Unit Price must be between 0 and 5000");
    					flag = 1;
    				}//end else
    			}while (flag == 1);//end inner do while loop
    
    			//Read in and Verify QOH
    			do
    			{
    				System.out.print("\nPlease enter Quantity on Hand:");
    				itemQuant[count] = TextIO.getlnInt();
    
    				if ((itemQuant[count] >= 0) && (itemQuant[count] <=500))
    				{
    					System.out.print(" Item " +itemNum[count]);
    					System.out.println(" has been added");
    					flag = 0;
    					ismore = true;
    				}//end if
    				else
    				{
    					System.out.println("**ERROR06** Quantity on Hand must be between 0 and 500");
    					flag = 1;
    				}//end else
    			}while (flag == 1); //end inner do while loop
    			//add to counter
    			count++;
    		}//end if (choice ==1)
    
    
    
    			if (choice == 2)
    			{
    				System.out.println("\nOption not yet implemented");
    				ismore = true;
    			}//end if
    
    			if (choice == 3)
    			{
    				if (count == 0)
    				{ 
    					System.out.println("Array is empty...Nothing to delete...");
    					ismore = true;
    				}// end if
    				
    				System.out.println("Enter item number: ");
    				String delNum = textIO.getlnString();
    				delNum = delNum.toUpperCase();
    				
    				
    			}//end if
    
    			if (choice == 4)
    			{
    				if (count == 0)
    				{
    					System.out.println("\nNo detail items to report");
    					ismore = true;
    				}// end if
    
    				String currentMax = "";
    				int currentMaxIndex;
    
    				for (i = count - 1; i >= 1; i--)
    				{
    					//Find max in list
    					currentMax = itemNum[i];
    					currentMaxIndex = i;
    
    					for (int j = i - 1; j >= 0; j--)
    					{
    						if (currentMax.compareTo(itemNum[j]) < 0)
    						{
    							currentMax = itemNum[j];
    							currentMaxIndex = j;
    						}//end if
    					}//end inner for loop
    
    					//swap [i] w/ [currentMaxIndex] (if neccessary)
    
    					if (currentMaxIndex != i)
    					{
    						itemNum[currentMaxIndex] = itemNum[i];
    						itemNum[i] = currentMax;
    
    						stringConverter = name[currentMaxIndex];
    						name[currentMaxIndex] = name[i];
    						name[i] = stringConverter;
    
    						stringConverter = itemDesc[currentMaxIndex];
    						itemDesc[currentMaxIndex] = itemDesc[i];
    						itemDesc[i] = stringConverter;
    
    						intConverter = itemQuant[currentMaxIndex];
    						itemQuant[currentMaxIndex] = itemQuant[i];
    						itemQuant[i] = intConverter;
    
    						doubleConverter = unitPrice[currentMaxIndex];
    						unitPrice[currentMaxIndex] = unitPrice[i];
    						unitPrice[i] = doubleConverter;
    
    					}//end if
    				}//end outer for
    
    				//Display sorter Array
    				System.out.println("\nNumber of item's in system: " + count);
    				System.out.println("\n\tInventory Items by Department\n");
    
    				for (i=0; i < count; i++)
    				{
    					double cost = itemQuant[i] * unitPrice[i];
    
    					String priceCurrency = cf.format(unitPrice[i]);
    					String quantNumber = nf.format(itemQuant[i]);
    					String costCurrency = cf.format(cost);
    
    					System.out.println(name[i] + "\t" + itemNum[i] + "\t" + itemDesc[i] + "\t" + quantNumber + "units @ " + priceCurrency + " /unit = " + costCurrency);
    
    					totalCost += cost;
    				}//end for loop
    
    				String totalCostCurrency = cf.format(totalCost);
    
    				System.out.println("TOTAL INVENTORY COST: " + totalCostCurrency);
    				ismore = true;
    			}//end if (choice == 4)
    
    			if (choice == 5)
    			{
    				System.out.println("\nOption not yet Implemented");
    				ismore = true;
    			}//end if
    
    			if (choice == 6)
    			{
    				ismore = false;
    			}//end if
    
    		} while (ismore);//end big do while loop
    	}//end main
    }//end class
    The problem lies with deleteing an entry from the array (Selection 3 from the menu.) I have started coding it, but am not quite sure how to complete the code.

    I have a slight thought as to what would be needed, but any input from you guys would be appriciated, as I need to have this code done ASAP.

    Thanks!

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Deleteing entries from arrays

    What's in the "TextIO" class?
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    2

    Re: Deleteing entries from arrays

    TextIO is basically a way of reading in data from the user. We have the class provided to us.

    So for example,

    testNum = TextIO.getlnInt();

    Would read in an integer from the user.

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Deleteing entries from arrays

    In that case before I look at your code you should know you have a syntax error in line:
    Code:
    String delNum = TextIO.getlnString() ;
    TextIO instead of textIO
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Deleteing entries from arrays

    If you are using an array then my suggestion would be set the value at that position to zero or something. ArrayList would be easier to manage.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width