2

I'm currently making an easy webshop application. The webshop is required to add three kinds of objects to the shoping cart. A CD, a Book or a Game. I made a class for every object which all have a toString() method.

Now I have to make a method add(..) which needs to add the specified object to an ArrayList called shoppingcart. This method needs to be called within the class Webshop which in itself has the objects that are created.

I know how to do this with multiple add methods but it's required to do it with a single method.

5 Answers 5

4

You will probably want to create a new type called something like AbstractItem and all your other types extend from this. It is also a good practice to hide this abstract class behind an interface. So, the AbstraxtItem class could implement an Item interface that would define the public APIs.

The AbstractItem class would define some abstract methods that its subtypes should implement like getPrice() and possibly other concrete methods that would be the common behavior for all subclasses. The shopping cart will be an ArrayList<Item> and that would be populated by the add(Item) method.

Sign up to request clarification or add additional context in comments.

5 Comments

Inheritance is today often not the ideal way to go. I consider the Interface approach more suitable. (Remember favor composition to inheritance). And dont forget the testability, programm to interfaces.
You're right. I edited my answer. If you are still in the academia, I wish you will get to work for a company where all developers do respect the principles you enumerated. If not then I have a question: do all your colleagues respect these rules?
I removed my downvote, But with abstract classes, you have to be carefull, too. In our company, in our department we have two teams: One Back offixe, one embedded team. Back office team follow that Interface principle, but introduced some dessign flaws with Abstract classes, and huge inheritance chains (violating principles) . The Embedded (java) Team: 60% does follow the interfaces, 50% the unit test.
Nice groups, you don't get to such high percentages usually :)
Yes all are verry experienced with at least 10 years of SW experience, we developp devices with related Server used in some hundreds thousands vehicles. Privatly I dveelop an ios Application, in Objectiv-c i am less expereinced, and there I dont follow the interface principle, good question why. In java thats all a bit more confortable.
3

Each object in your shop which can be add should implement an interface IProduct. You then have a list of IProduct, which you can add.

ArrayList<IProduct> shoppingBasket = new ArrayList<IProduct>();

1 Comment

Ok, I remember now,. why I have downvoted, I will add to your answer as comment.
1

You could let the classes CD, Book and Game implement an Interface, e.g. ShopObject. Then you can have your shoppinCart like this:

ArrayList<ShopObject> shoppingCart = new ArrayList<ShopObject>();

Comments

1

OOP can help you solve this problem :

  1. If you find CD, Book or Game has some common attribute then you can use a Base class with common features and let all these extend that class.

    So CD, Book and Game extends a class say Product and you provide a generic add method which takes Product as parameter.

    add(Product product);

  2. If CD, Book and Game don't share any common features then also you can use OOPs feature to add all these in more simpler way; but here you need to use method overloading

    add(CD cd);

    add(Book book);

So will suggest, think over your application design and then take a call on either of the approaches.

Comments

0

You can use aggregate all objects in one using a discriminant to indicate which object is really being represented pretty much like variant records in Ada or unions in C.

You have a full example here: http://lambda-the-ultimate.org/node/2694#comment-40453

1 Comment

It not neccessary to use an overkill approach: Java has interfaces. An object can implemnt multiple Interfaces. Especially because he wants an "easy webshop application"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.