// TEST CODES FOR USING COLLECTION FROM OTHER CLASS /* This sample code shows how a collecion object and methods from other class can be used in another class. */ /* A collection object used for merchandise inventory is very similar in structure to the orders collection. Rather than creating a new collection for orders, the merchandise collection and its methods are used to create and display an orders collection. */ /* To test the code, compile this file using the command javac Order.java. To display the merchandise inventory, enter the command java Toys and press the enter key. To display the orders collection, enter the command java Order. */ // Define the basic unit of collection with its methods class Toy { String toyName; int toyQuantity; float toyPrice; float toyCost; // Use method to make initialization easier public void setToyDetails (String n, int q, float p) { toyName = n; toyQuantity = q; toyPrice = p; } public float computeToyCost() // Compute 1 collection unit cost { toyCost = toyPrice * toyQuantity; return toyCost; } public void displayToyDetails() // To display 1 collection unit { System.out.println("\nToy Name: " + toyName); System.out.println("Toy Quantity: " + toyQuantity); System.out.println("Toy Price: " + toyPrice); System.out.println("Toy Cost: " + toyCost); } } // Define the collection class class Toys { float toyAmount; float totalAmount = 0f; Toy [] toyList; // Use custom constructor to create array of variable size public Toys(int toyListSize) { toyList = new Toy[toyListSize]; for (int i = 0; i < toyListSize; i++) { toyList[i] = new Toy(); } } public void displayToyList() // Method to display collection { for (int i = 0; i < toyList.length; i++) { toyAmount = toyList[i].computeToyCost(); toyList[i].displayToyDetails(); } } public void computeTotalAmount() // Method to compute total { for (int i = 0; i < toyList.length; i++) { toyAmount = toyList[i].computeToyCost(); totalAmount = totalAmount + toyAmount; } } public void displayTotalAmount() // Display collection total { computeTotalAmount(); System.out.println("\nTotal Cost: " + totalAmount); } // Collection main instantiates and displays collection public static void main (String args[]) { Toys ourToys = new Toys(6); ourToys.toyList[0].setToyDetails("Doll", 20, 10f); ourToys.toyList[1].setToyDetails("Ball", 30, 20f); ourToys.toyList[2].setToyDetails("Car", 10, 30f); ourToys.toyList[3].setToyDetails("Book", 20, 20f); ourToys.toyList[4].setToyDetails("Bag", 20, 30f); ourToys.toyList[5].setToyDetails("Radio", 30, 10f); System.out.println("\nThe Toys Inventory"); ourToys.displayToyList(); ourToys.displayTotalAmount(); } } // Define another class that will use above collection public class Order { Toys orders; // Object to hold toy collection in Order class public Order(int orderSize) // Constructor specifies size { orders = new Toys(orderSize); } public static void main (String args[]) // Main for this class { Order myOrder = new Order(3); // Instantiate order object myOrder.orders.toyList[0].setToyDetails("Ball", 3, 20f); myOrder.orders.toyList[1].setToyDetails("Book", 3, 20f); myOrder.orders.toyList[2].setToyDetails("Bag", 3, 30f); // Use toy collection methods to display Order collection System.out.println("\nThe Orders"); myOrder.orders.displayToyList(); myOrder.orders.displayTotalAmount(); } }