Some of you are having trouble with this assignment. Here are 11 (fairly
easy) steps that should help you get a good start if you're having a lot of
trouble.
You don't have to follow these steps, they are just suggestions. But if
you're lost, they're a great place to start.
- Think about the design of your clothing hierarchy. Don't worry about
methods yet, just put together a listing of the superclass, the 7 classes
for the types of clothing, and a couple of intermediate classes, possibly
to represent tops and bottoms, or casual and non-casual clothing. (Either
is fine.)
- Set up your subdirectories and files. You'll need (at least)
csc324/driver/ to hold the csc324.driver.Driver class. You should also
have another package (like csc324.clothes) to hold the clothing classes.
Create the subdirectories for that package, and create only one clothing
class (called something like 'TShirt'). Don't bother putting instance
variables in it, or even declaring a constructor. Just leave it empty for
now.
- Create a class to hold the inventory. As seen in lecture, it's a
reasonable thing to have a Vector instance variable to store the items.
Don't go whole hog yet with all the methods; just make something that
compiles!
- In Driver.main(), instantiate the inventory class so you have an object
in which to store the items. (I'll assume you called that variable
'inventory'.)
- Decide on what actions the user should be able to take. The user needs
to be able to create clothing items, so have a 'c' command to start that
process. Decide on the others; I strongly recommend sticking with
one-character commands, since the format is already set up for you.
- Set up a method to handle each command. You should, therefore, have a
method called something like Driver.createItem().
Here, I'll start those last two bits for you:
case 'q':
// Quit
break;
case 'c':
// Create new clothing.
createItem( in, inventory );
break;
// Add your other commands here.
Notice the 'in' parameter -- you need to pass in the BufferedReader so you
can continue to read from it in Driver.createItem().
-
Inside Driver.createItem(), just create one TShirt. Add it to the
Vector inside your inventory variable.
-
Now compile what you have. Fix any syntax errors and package problems now.
-
Now that you've got a fairly simple, working program, you can expand it.
First, create all the classes for the clothing hierarchy. You can
initially have them all empty, and add things as you need them. For
example, you'll soon find that you need to store the price somewhere -- I
recommend the top of your hierarchy.
Simplicity is key! For example, feel free to store the size as a String,
so you can store both "13" and "medium", depending on the subclass.
-
Now start being more picky about the inventory class -- make the Vector in
your inventory class non-public, and create accessor methods for it.
Also create various methods for listing the types of clothing that your
inventory can handle; this will be useful when prompting the user for what
type of clothing to create, and so on.
In fact, here's a pair of methods that will do just that. You can put
these in your inventory class:
// A list of the types of clothes that I sell.
private String[] clothes = {
"csc324.clothes.TShirt",
"csc324.clothes.CasualShirt",
"csc324.clothes.DressShirt",
"csc324.clothes.Sweater",
"csc324.clothes.DressPant",
"csc324.clothes.Jeans",
"csc324.clothes.Shorts"
};
// Return the types of clothes I sell.
public String[] getClothingTypes() {
return clothes;
}
-
Now write code to create the various classes based on user input.
// Find out what type of clothes to create.
System.out.println(" Select from the following list:" );
String[] clothingTypes = inventory.getClothingTypes();
// Print each type of clothing to the screen.
for( int i=0; i < clothingTypes.length; i++ ) {
System.out.println( " " + i + " " + clothingTypes[i] );
}
Clothes c; // A reference to the item of clothing being created.
try {
// Read the type of clothing that the user wants to create.
String line = in.readLine();
int whichClothingType = Integer.parseInt( line );
// This is a nifty way to get an instance of what the user wants;
// you can read all about it in the Java APIs, linked from the
// main page.
c = (Clothes) ( Class.forName(
clothingTypes[whichClothingType] ).newInstance() );
} catch( Exception e ) {
e.printStackTrace();
}
Hopefully, that's enough to get you going. Let me know if this helps.