In this tutorial, we will be covering how to use Classes and Objets instead of Parallel Arrays. In order to teach
students how to use arrays, many instructors give assignmentsallows students to gain confidence
when using arrays, this type of approach becomes very cumbersome to use and difficult to manage, especially as the number of elements stored increases in size and more so as the use of resizable collections like ArrayLists come into play.
So let's go ahead and start out with a standard inventory program that uses parallel arrays to store price, quantity and name. This program will display a menu using a JOptionPane window requesting input for adding inventory, updating a current item's status, displaying the inventory, and quitting. If the user wants to update an item, we will display another menu asking for the index of the item to change. After we recieve the index, we will display another menu asking the user if they want to update the name, quantity or price, followed by a prompt for the new value. And as was the requirement when I did the assignment, we'll limit the size of the arrays to 10 items in inventory.
So far, this program isn't too bad. A few tedious things (beyond getting input) that I noticed included making the display String for a given item, like at this line String item = "Name: " + name[index] + "\nQuantity: " + quantity[index] + "\nPrice: " + price[index];. Consider this though- what would happen if there were no size limitations as to the number of items you could have in inventory (meaning we are now using ArrayLists in parallel instead of static arrays)? When you go to add or remove (which would be the next logical step) items, you wouldn't have to use a counter variable because the ArrayLists would automatically resize as soon as elements are added or removed from them. This gets a little sticky, however, when removing elements in the middle of the lists, as it is very easy to get attributes mixed up. For example, you could remove element 9 for the name list, but end up removing element 8 from the quantity and element 10 from the price list. This would completely mess up the data integrity and accuracy, plus it is very hard to debug this error.
Another logical step would be to sort the elements according to one of their attributes (name, quantity or price). Using parallel arrays, you have to enforce each step of the sort amongst all three arrays. So for example, if you sort the quantity array in ascending order, the changes aren't automatically affected throughout the other two arrays. This means that if you call Arrays.sort() on one array, it doesn't necessarily change the other two. And if you call Arrays.sort() on each of the three arrays, you have just sorted three arrays individually, so your name, qunatity and price array are all in order according to the values stored in them. However, you will have mismatched all the attributes from the item they are supposed to describe.
Now that we've seen the cons of parallel arrays, let's talk about the advantages of using classes instead. First off, the attributes representing each class are contained within the class. This means that the quantity, price and name cannot get mismatched like we saw when using parallel arrays. This also means that we only have to manage one collection, so it saves memory. Next, we can use methods to make our lives easier. This means that we can set up a toString() method in the class, and when invoked, it will return a nice formatted (to our specifications) String; and we can also use a single setter method so we can eliminate the group of if statements for updating an item. So in short, you are dealing with one variable that holds all the information and can have automated tasks set up instead of having to hard code each component as we saw above.
Now that we've discussed the advantages of classes, let's take a look into puttingputting them into use. To start, we'll take a look at designing a class to model an Item.
After looking at the Item class, I notice how it is a lot more organized than using parallel arrays, plus it provides a little tighter control on the attributes and more usability through many of its methods, most notably the toString() and update() methods. Now let's redo our Inventory program using the Item class instead of Parallel arrays.
As we can see, this program gives us tighter control over each Item because we don't have to worry about keeping up with which indices represent each Item. This becomes increasingly important as we move onto using resizable collections like ArrayList, and as we attempt to sort the Items by a given attribute (or multiple attributes). We also have the convenience of using methods defined in the Item class, something we didn't have the luxury of doing when using parallel arrays.
By using classes instead of parallel arrays, you will begin to get a better handle on Object-Oriented Programming. This will help you as you continue your studies of Java, especially as you come across more advanced OO concepts like inheritance, abstraction and polymorphism, in addition to helping you better organize your program.