User Information into an Array (first ever array)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • no1zson
    New Member
    • Jul 2007
    • 38

    User Information into an Array (first ever array)

    I have been reading through many of the array questions and cannot find one that addresses my issue. Maybe someone can help me out.
    Same story, I am learning Java and have just written a CD Inventory application. It works, does what I want it to and all that, but now I need to put an array in there to store more than one cd at a time. Seems simple enough until I actually start coding. I want to save as much of the code as I can since I worked so hard to get it just the way I want it, but am not exactly sure of how an array will work in to it all.
    All the examples I see on arrays have the information contained in that array entered when the array is created, example - int array[] = {1,2,3,4};.
    That is great, but I want my array to use the fields I have already defined and store the information the user enters. Can this be done? Do I need to create a new class altogether, or can I use one of the two I already have and just add a method? Here are the two classes I currently have coded, any help would be appreciated.
    Code:
    import java.util.Scanner; //uses class Scanner
    
    public class Inventory
    {// begin class Inventory
    	public static void main(String[] args)	
    	{//begin method main
    	
    	Compactdisk thisCompactdisk = new Compactdisk(); //call Compactdisk class
    	
    	Scanner input = new Scanner(System.in);  // create scanner
    	
    	
    			// begin display method
    			System.out.print("Enter CD Name or STOP to Exit: ");
    			thisCompactdisk.setCDName(input.next());  // read cd name
    			
    		  	while (!thisCompactdisk.getCDName().equalsIgnoreCase("STOP"))
    			{// begin main While
    
    				System.out.print("Enter Price of this CD: "); // prompt for price
    				thisCompactdisk.setPrice(input.nextFloat());	// price input from user.
    				while (thisCompactdisk.getPrice()<= 0)
    				{// begin while
    				System.out.print("Price Must Be Greater Than Zero. Enter Price: ");
    				thisCompactdisk.setPrice(input.nextFloat()); // cd price loop from user.
    				} // End while
    		
    				System.out.print("Enter CD Item Number: "); // prompt for cd item number
    				thisCompactdisk.setItemno(input.nextInt()); // cds item number input from user
    		
    				System.out.print("Enter Number of these CDs in Stock: "); // prompt for cd stock
    				thisCompactdisk.setNstock(input.nextInt()); // cds in stock input from user
    				
            
    				System.out.print("CD "+thisCompactdisk.getCDName()+", Item Number "+thisCompactdisk.getItemno()+","); // display name
    				System.out.printf(" is worth %c%.2f.\n", '$', thisCompactdisk.getPrice()); // display individual price
    				System.out.printf("We have %d copies in stock, making our inventory worth %c%.2f\n", thisCompactdisk.getNstock(), '$', thisCompactdisk.getValue()); //inventory value
    		
    				System.out.print("Enter CD Name or STOP to Exit: "); // internal loop prompt
    				thisCompactdisk.setCDName(input.next()); //name input from user
    						
    			} // End main While
    		System.out.print("Ending Program.");
    	
    
    	}// end method main
    } // end class Payroll
    and

    Code:
    public class Compactdisk
    {// begin class
    
    
    	//InventoryCD class has 5 fields
    	String cdName; //  cd name
    	float price; // price of cd
    	int itemno; // item number of cd
    	int nstock; // how many units in stock	
    	
    	//Compact disk class constructor
    	public Compactdisk()
    	
    		// 4 fields need to be set up
    		{ 
    		cdName = "";
    		price = 0;
    		itemno = 0;
    		nstock = 0;
    		}
    		
    		// set values
    	   public void setCDName(String diskName)
    	   {
    	   cdName = diskName;
    	   }
    		public void setPrice(float cdPrice)
    	   {
    	   price = cdPrice;
    	   }
    		public void setItemno(int cdItemno)
    	   {
    	   itemno = cdItemno;
    	   }
    		 public void setNstock(int cdStock)
    	   {
    	   nstock = cdStock;
    	   }
    		
    	   // return values
    		public String getCDName()
    		{	
    		return (cdName);
    		}
    		public float getPrice()
    		{	
    		return (price);
    		}
    		public int getItemno()
    		{	
    		return (itemno);
    		}
    		public int getNstock()
    		{	
    		return (nstock);
    		}
    					
    		// returns inventory value
    	   public float getValue()
    	   {
    	   return(price * nstock);
    	   }
    
    }// end class
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Would it be over your head to have a look at [b]ArrayList[b]s? The trouble with
    arrays is that they have a fixed length (you have 101 CDs and an array of length
    100; that means trouble).

    Here's a hypothetical sketch of an example:
    [code=java]
    List<CD> inventory= new ArrayList<CD>() ;
    ...
    CD cd= new CD( ...);
    inventory.add(c d);
    [/code]

    Give it a try.

    kind regards,

    Jos

    Comment

    • blazedaces
      Contributor
      • May 2007
      • 284

      #3
      Question, don't think this deserves it own thread, just a curiosity thing:

      I've heard that arraylist is sort of the "new and improved" vector ... why is that? The only obvious difference I notice is that there aren't 3 methods that do the same things... perhaps it's more efficient in some way? Could someone shed some light on the subject?

      Thanks for the help,

      -blazed

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by blazedaces
        Question, don't think this deserves it own thread, just a curiosity thing:

        I've heard that arraylist is sort of the "new and improved" vector ... why is that? The only obvious difference I notice is that there aren't 3 methods that do the same things... perhaps it's more efficient in some way? Could someone shed some light on the subject?

        Thanks for the help,

        -blazed
        The Vector class is an old class; it already existed in Java 1.0. All of its public
        methods are synchronized which seemed like a handy idea but it isn't, The
        Collection Framework introduced the ArrayList class which basically did the
        same thing; only a bit better: none of the methods are synchronized and the
        reallocation scheme (when the array grows) is a bit better as well.

        Nowadays the Vector class only exists as a retrofitted class so that it implements
        the List interface, same as the ArrayList does. There's no need to use Vectors
        anymore.

        kind regards,

        Jos

        Comment

        • no1zson
          New Member
          • Jul 2007
          • 38

          #5
          Originally posted by JosAH
          Would it be over your head to have a look at [b]ArrayList[b]s? The trouble with
          arrays is that they have a fixed length (you have 101 CDs and an array of length
          100; that means trouble).

          Here's a hypothetical sketch of an example:
          [code=java]
          List<CD> inventory= new ArrayList<CD>() ;
          ...
          CD cd= new CD( ...);
          inventory.add(c d);
          [/code]

          Give it a try.

          kind regards,

          Jos
          Thanks for the reply. I will read anything I can get my hands on. Where is this "ArrayList" you speak of? It may be over my head but I will certainly give it a try none-the-less.
          I do understand the fixed lenght confines of an array, but since this is more of a learning exercise I am not too concerned with that right now. I will not enter in more than 5 cds for what I am doing. I just want to learn how to do it.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Here are the API documents. Note that you can download them all.
            (look at the top right corner). An ArrayList is so much more convenient and using
            Collection classes like this one may even influence your design and drag it away
            from a Fortanesque way of thinking about programs to a more object oriented
            way of thinking; do give it a try; it'll help you and ArrayLists aren't that difficult.

            kind regards,

            Jos

            Comment

            Working...