QMCS 281
Pat’s Potted Plants
We will continue developing the application for Pats Potted Plants. We will assume that the first assignment is complete and operational. In essence we will be adding an Order class and its collection class Orders and changing some of the existing classes. Below are the details regarding changes to make to the existing classes and new classes to develop.
First the good news! The classes Customer, Plant, Staff, Student, and Vendor will all remain unchanged. The Customers, Inventory, and Vendors classes will all be changed in a similar manner as follows:
- All will use a Vector object to store the collection of its objects. A Vector has the advantage of being able to expand as additional items are added. To accomplish this, instantiate an array of String objects that have the parameters needed for each object in the collection in a comma delimited string. For example, the following code could be used to declare the private Vector object and the private array of Strings for the Vendors class:
private Vector vendors;
private String[] v = {“Sam,Over there,2”,
“Lilly,Beyond there,3”,
“Daisy,Way far,5”}
The constructor for the class would then use this array of strings to instantiate objects and put them into the vector. Here’s some example code that would do that for the Vendors class:
public Vendors()
{
vendors = new Vector();
for ( int i = 0; i < v.length; i++)
{
StringTokenizer st = new StringTokenizer(v[i], ",");
String name = new String(st.nextToken());
String location = new String(st.nextToken());
int days = Integer.parseInt(st.nextToken());
Vendor vend = new Vendor(name,location,days);
vendors.addElement(vend);
}// for
}
Note the use of the StringTokenizer class to break apart the string into its component parts, separated by commas.
The getVendor, getCustomer, getPlant, and toString methods will need to be changed to iterate through the vector rather than through an array.
Finally, you need to add an “add” method to each collection class (addCustomer, addPlant, addVendor). This method will accept the appropriate object and add it to the vector of objects.
Special Note: the Customers class will need an additional parameter in its array of strings to indicate whether the Customer is a student or a staff member. This will be used in the constructor in an “if” statement to instantiate either a Student or Staff object.
Make sure that you have the “import java.util.*;” statement at the beginning of each of the collection classes. This is needed in order to use the Vector class.
Changes to PatsPottedPlants Class: Remove the printVendors, printInventory, and printCustomers methods. We won’t use these in the future. Add a variable for the Orders and add instantiation of the variable to the constructor. (The classes Orders and Order are detailed below.) Add a getOrders method to return the Orders object.
There will be two new classes you will need to write. The first is Order. The Order class will contain the information needed for a customer order of a plant. We will simplify things and restrict each order to one customer and one plant. The data items needed in this class are a Customer object, a Plant object, and a Date object. The constructor will accept a Customer object and a Plant object. It will store the Customer object, the Plant object and create a new Date object and store it. Write a getCustomer, getPlant, and getDate methods to return those objects. Finally, you will need a toString method. Working with Date objects is a little tricky. A date object is actually a long integer with the date stored as the number of milliseconds from a fixed date. The default formatting of that object isn’t very interesting. Therefore you will need to use the DateFormat object to make it look nice. The code below is an example of using the DateFormat format a date into month name, day, year:
DateFormat form = DateFormat.getDateInstance();
String prettyDate = form.format(dateVariableName);
The first line of code gets an object with default formatting properties and the second line uses that object to format the date using that default.
Note: You will need to import both java.util.* and java.text.* to use the Date and DateFormat objects.
The other new class is the Orders class, a collection class for the orders. This will be modeled after the other collection class, but with a few differences. The constructor will not instantiate any new Order objects, but rather just create a new Vector object. In other words, we’ll start out with no orders. There will need to be an addOrder method that accepts an Order object and adds it to the Vector. The toString method will be very similar to the other collection class methods of that name. We will need to return the orders placed by a customer. Since a customer can have more than one order, we’ll have a getOrders method that accepts a Customer object and returns a Vector object containing all the orders for that customer. You can check customer Ids to find the orders for a particular customer.
The TestPat program should now test out all these new and changed features. (Don’t wait until the end to test each change as you make it! Test as you go.) When finished the TestPat program will do the following:
Instantiate a PatsPottedPlants object
Get each of the collection class objects
Add items to each of the collection classes
Print out each of the collection classes to see if the item was added successfully
Note that you will need to add more than one Order for a particular customer!
Good luck!