Page 438 - Introduction to Programming with Java: A Problem Solving Approach
P. 438

                404 Chapter 10 Arrays and ArrayLists
 SalesClerks
 -clerks : SalesClerk[] -filledElements : int= 0
 +SalesClerks(initialSize : int)
+dumpData() : void
+addSale(name : String, amount : double) : void -findClerk(name : String) : int
-doubleLength() : void
  SalesClerk
 -name : String -sales : double = 0
 +SalesClerk(name : String) +getName() : String
+getSales() : double +adjustSales(amount : double) : void
   Figure 10.21 UML class diagram for the SalesClerks program Sales Clerks Program
Let’s now implement a complete program that adds sales and prints sales for a group of sales clerks in a department store. As is customary, we’ll first get a big-picture view of things by presenting a UML class diagram. Figure 10.21’s class diagram shows two classes. The SalesClerks class represents sales data for the entire department store, and the SalesClerk class represents total sales for one particular sales clerk.
The SalesClerks class contains two instance variables—clerks and filledElements. clerks is an array of SalesClerk objects. filledElements stores the number of elements that have been filled so far in the clerks array. For a filledElements example, see Figure 10.20, where filledElements would be 3A. TpheaSagloesClPerDksF’s coEnsntruhctoar instcanetiartes the clerks array, using the constructor’s initialSize parameter for the array’s size.
The SalesClerks class contains four methods—dumpData, addSale, findClerk, and doubleLength. The dumpData method is the most straightforward of the four. It prints all the data in the clerks array. The term dump is a computer term which refers to a simple (unformatted) display of a program’s data. See the dumpData method in Figure 10.22b and verify that it prints the data in the clerks array.
The addSale method processes a sale for a particular sales clerk. More specifically, the addSale method finds the sales clerk specified by its name parameter and updates that sales clerk’s total sales with the value specified by its amount parameter. To find the sales clerk, the addSale method calls the findClerk helper method. The findClerk method performs a sequential search through the clerks array, and returns the index of the found sales clerk or 􏰂1 if the sales clerk is not found. If the sales clerk is not found, addSale adds a new SalesClerk object to the clerks array in order to store the new sale transaction in it. In adding a new SalesClerk object to the clerks array, addSale checks to make sure that there is available space in the clerks array for the new SalesClerk object. If the clerks ar- ray is all full (that is, filledElements equals clerks.length), then addSale must do something to provide for more elements. That’s where the doubleLength helper method comes to the rescue.
The doubleLength method, as its name suggests, doubles the size of the clerks array. To do that, it instantiates a new array, clerks2, whose length is twice the length of the original clerks arrays. Then it copies all the data from the clerks array into the lowest-numbered elements in the clerks2 array. Finally, it assigns the clerks2 array to the clerks array so the clerks array points to the new longer array. See the addSale, findClerk, and doubleLength methods in Figures 10.22a and 10.22b and verify that they do what they’re supposed to do.
The SalesClerk class, shown on the right side of Figure 10.21, is fairly straightforward. It contains two instance variables, name and sales, for the sales clerk’s name and the sales clerk’s total sales. It
 Start with a UML class diagram to get a big-picture understanding.
  















































































   436   437   438   439   440