By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,586 Members | 623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,586 IT Pros & Developers. It's quick & easy.

User Information into an Array (first ever array)

P: 38
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.
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner; //uses class Scanner
  2.  
  3. public class Inventory
  4. {// begin class Inventory
  5.     public static void main(String[] args)    
  6.     {//begin method main
  7.  
  8.     Compactdisk thisCompactdisk = new Compactdisk(); //call Compactdisk class
  9.  
  10.     Scanner input = new Scanner(System.in);  // create scanner
  11.  
  12.  
  13.             // begin display method
  14.             System.out.print("Enter CD Name or STOP to Exit: ");
  15.             thisCompactdisk.setCDName(input.next());  // read cd name
  16.  
  17.               while (!thisCompactdisk.getCDName().equalsIgnoreCase("STOP"))
  18.             {// begin main While
  19.  
  20.                 System.out.print("Enter Price of this CD: "); // prompt for price
  21.                 thisCompactdisk.setPrice(input.nextFloat());    // price input from user.
  22.                 while (thisCompactdisk.getPrice()<= 0)
  23.                 {// begin while
  24.                 System.out.print("Price Must Be Greater Than Zero. Enter Price: ");
  25.                 thisCompactdisk.setPrice(input.nextFloat()); // cd price loop from user.
  26.                 } // End while
  27.  
  28.                 System.out.print("Enter CD Item Number: "); // prompt for cd item number
  29.                 thisCompactdisk.setItemno(input.nextInt()); // cds item number input from user
  30.  
  31.                 System.out.print("Enter Number of these CDs in Stock: "); // prompt for cd stock
  32.                 thisCompactdisk.setNstock(input.nextInt()); // cds in stock input from user
  33.  
  34.  
  35.                 System.out.print("CD "+thisCompactdisk.getCDName()+", Item Number "+thisCompactdisk.getItemno()+","); // display name
  36.                 System.out.printf(" is worth %c%.2f.\n", '$', thisCompactdisk.getPrice()); // display individual price
  37.                 System.out.printf("We have %d copies in stock, making our inventory worth %c%.2f\n", thisCompactdisk.getNstock(), '$', thisCompactdisk.getValue()); //inventory value
  38.  
  39.                 System.out.print("Enter CD Name or STOP to Exit: "); // internal loop prompt
  40.                 thisCompactdisk.setCDName(input.next()); //name input from user
  41.  
  42.             } // End main While
  43.         System.out.print("Ending Program.");
  44.  
  45.  
  46.     }// end method main
  47. } // end class Payroll
and

Expand|Select|Wrap|Line Numbers
  1. public class Compactdisk
  2. {// begin class
  3.  
  4.  
  5.     //InventoryCD class has 5 fields
  6.     String cdName; //  cd name
  7.     float price; // price of cd
  8.     int itemno; // item number of cd
  9.     int nstock; // how many units in stock    
  10.  
  11.     //Compact disk class constructor
  12.     public Compactdisk()
  13.  
  14.         // 4 fields need to be set up
  15.         { 
  16.         cdName = "";
  17.         price = 0;
  18.         itemno = 0;
  19.         nstock = 0;
  20.         }
  21.  
  22.         // set values
  23.        public void setCDName(String diskName)
  24.        {
  25.        cdName = diskName;
  26.        }
  27.         public void setPrice(float cdPrice)
  28.        {
  29.        price = cdPrice;
  30.        }
  31.         public void setItemno(int cdItemno)
  32.        {
  33.        itemno = cdItemno;
  34.        }
  35.          public void setNstock(int cdStock)
  36.        {
  37.        nstock = cdStock;
  38.        }
  39.  
  40.        // return values
  41.         public String getCDName()
  42.         {    
  43.         return (cdName);
  44.         }
  45.         public float getPrice()
  46.         {    
  47.         return (price);
  48.         }
  49.         public int getItemno()
  50.         {    
  51.         return (itemno);
  52.         }
  53.         public int getNstock()
  54.         {    
  55.         return (nstock);
  56.         }
  57.  
  58.         // returns inventory value
  59.        public float getValue()
  60.        {
  61.        return(price * nstock);
  62.        }
  63.  
  64. }// end class
Jul 13 '07 #1
Share this Question
Share on Google+
5 Replies


Expert 10K+
P: 11,448
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:
Expand|Select|Wrap|Line Numbers
  1. List<CD> inventory= new ArrayList<CD>();
  2. ...
  3. CD cd= new CD( ...);
  4. inventory.add(cd);
  5.  
Give it a try.

kind regards,

Jos
Jul 13 '07 #2

blazedaces
100+
P: 284
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
Jul 13 '07 #3

Expert 10K+
P: 11,448
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
Jul 13 '07 #4

P: 38
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:
Expand|Select|Wrap|Line Numbers
  1. List<CD> inventory= new ArrayList<CD>();
  2. ...
  3. CD cd= new CD( ...);
  4. inventory.add(cd);
  5.  
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.
Jul 13 '07 #5

Expert 10K+
P: 11,448
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
Jul 13 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.