I want to build a simple application for a bookstore. There are a few types of things on sale: books, movies, and magazines. I was thinking to of building one abstract class with a toString() method, and 3 subclasses – books, movies, magazines (there will be more later). Then in the program I want to operate on these objects and was thinking I would keep them all in an ArrayList<ParentClass>. Is this a good choice of data structure?
I want to build a simple application for a bookstore. There are a few
Share
First, I’d recommend you to use generics. If for example you are going to define abstract class (or probably better interface
Item) so createnew ArrayList<Item>, so you will not be able to add there anything exceptItem.Second, use interface. You definition should look like:
List<? extends Item> lll = new ArrayList<Item>();The choice of the main data structure depends on your needs. I believe that you are going to perform some kind of search mechanisms. In this case probably you can use
Mapor combine list where you store all objects sequentially and several maps that help you to perform search by different parameters.Anyway you should wrap your data structure by some class (model) that provides basic functionality of your business logic, so if you want to change something you can do it within this class without any changes in other parts of your application.