Java Polymorphism Woes

Associate
Joined
8 Nov 2005
Posts
56
I understand the basic concept of polymorphism but i'm really struggling to implement a particular feature in my code! :confused:

My class structure is as follows:

abstract class Transaction
- Soap extends Transaction
* doTransaction (method within Soap)

class Till
- Contains array to store transactions

So for example I can manually enter a transaction by putting the following code in to the Till class.

Code:
array[1] =  new Soap( 10 );
(10 bars of soap)

The doTransaction method asks the user for example how many bars of soap the customer wants. My question is how do I get the information from my doTransaction method in to the array!

Hard to explain so perhaps it would be easier to explain over msn if anybody wouldn't mind adding me.

Details in trust.

Thanks!
 
I suppose that makes sense but perhaps it is the naming of my methods that are confusing things. The reason this code needs to use polymorphism is because each 'product' could have different properties.

For example, the soap might only ask the user how many they want, but another product might be a membership product, for example a season ticket entry to somewhere and instead of asking for amount it asks for the season ticket id.
 
This is where i'm getting confused, each of my extended classes need to contain information particular to that product. I need to collect this particular information from the user depending on which product they have selected. In my mind the questions can be stored within a method of the extended class, my current code:

Code:
class SwimEntryTicket extends Transaction {

	public int numberOfAdults;
	public int numberOfChildren;
	private String time;
	private float totalCost;

	CasualSwim ( int a, int c, String ti, float co ){

		super( "Swimming" );
		numberOfAdults = a;
		numberOfChildren = c;
		time = ti;
		totalCost = co;

	}

	public void doInput ( ) {

		System.out.print("\n\tNumber of adults: ");
		numberOfAdults = Input.readInt();
		System.out.print("\n\tNumber of children: ");
		numberOfChildren = Input.readInt();
		//System.out.print("\n\tTime: ");
		//int valCheck = Input.readInt();
		//System.out.print("\n\tCost: ");
		//int valCheck = Input.readInt();

 	}

}

Now I need to use the information gathered from that method to put it in an array declared within my Till class which handles payment and stuff.
 
Back
Top Bottom