Java - How can objects have other objects?

Caporegime
Joined
12 Mar 2004
Posts
29,962
Location
England
Java - object interaction

How would I be able to say create many ticket machines, each with many tickets that can be chosen?

I've created the classes "TicketMachine" and "Ticket". I want the ticketmachine to be able to work out if the customers puts enough money in for a ticket but to do that they need to be able to select what ticket they want, and then the ticketmachine needs to get the tickets price from the ticket object.
 
Last edited:
Depending on how you want to handle the Ticket objects, you'd use either an array or an array list. An array should do fine for you though :)

Read about them here:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html

You'd use an array list when you don't initially know the number of objects you want to add to it, but just want to be able to add and remove items as you please.
 
If its non-important why not use a (Scoped) Vector?

Then can store ticket objects in that and use ticketVector.get(index i)
 
If its non-important why not use a (Scoped) Vector?

Then can store ticket objects in that and use ticketVector.get(index i)

A Vector is a bad choice compaired to a LinkedList/ArrayList in this case. No point in having a synchronized data structure unless you got multiple threads accessing it. Though does not really matter for a trival app like this.
 
It sounds to me like you want to create multiple types of tickets each with differing prices and not just a collection of objects. For this you would need to use inheritance, you could have an abstract base class called 'Ticket' then extend it with all of the different types of ticket you require such as 'DayTicket' for example. This way you could create an abstract method in the base class 'Ticket' such as 'getPrice()' and then depending on the ticket the customer chooses the method will be called on the correct concerete class.
 
Last edited:
It sounds to me like you want to create multiple types of tickets each with differing prices and not just a collection of objects. For this you would need to use inheritance, you could have an abstract base class called 'Ticket' then extend it with all of the different types of ticket you require such as 'DayTicket' for example. This way you could create an abstract method in the base class 'Ticket' such as 'getPrice()' and then depending on the ticket the customer chooses the method will be called on the correct concerete class.

Actually, you should generally favour composition over inheritance in simple situations like this. Introducing inheritance makes it far more rigid and a lot more complex, for no real gain.

Inheritance should be used in this case if there is a set number of ticket types which are unlikely to change often, and which each have their own properties. Composition should be used when all tickets have the same properties, just with different values, and tickets are likely to be changed or added to.

It's important to understand when and when not to use inheritance; don't use it just because it's the Java way of doing things. Just use it when it makes sense to do so :)
 
Last edited:
Back
Top Bottom