• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to design the classes for a simple shopping cart example using Strategy Design Patterns  RSS feed

 
Tom Bradely
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First year of Software Engineering, and we're learning OOP within Java. Came across an extension task to gain more credits. But could not think of what to do:

First Step: Design a checkout system

Build a checkout system for a shop which sells 3 products say Bread, Milk, and Bananas. Costs of the products are : Bread - $1, Milk - $0.60 and Banana - $0.40 Build a system to checkout a shopping cart that can have multiples of these 3 products and displays the order total. Examples are: Bread, Milk, Banana = $2.00 Bread, Bread, Milk, Banana = $3.00 Milk, Banana, Milk, Banana = $2.00

As a next step: enhance the code to apply the below discounts and offers

Buy one bread and get another bread for free Buy 3 Milk for the price of 2 Buy 2 Bananas and get one free

First part was rather straightforward, i went on and completed this by doing:



But now when it comes to implementing the second part of the challenge. I have no idea where to start and what to do. I'm sure this problem is probably fairly standard in terms of implementing rules for things like discounts etc. I just wanna know what my next step(s) is/should be & where possible to start if i wanted to go further with the theory behind this.
 
Rajith Pemabandu
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suppose this is a good code example of strategy pattern Shopping cart example to start on.



 
Rajith Pemabandu
Greenhorn
Posts: 23
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I would have thought you can guess how to apply a discount, but I suggest you start by writing down on paper what you want your app to do. That will probably concentrate your thinking.
Why did you ask about the strategy pattern? You don't seem to mention it in the body of your post.
 
Tom Bradely
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

I would have thought you can guess how to apply a discount, but I suggest you start by writing down on paper what you want your app to do. That will probably concentrate your thinking.
Why did you ask about the strategy pattern? You don't seem to mention it in the body of your post.


Yes that's what i went and did. But apparently applying discounts is not as easy as I thought. Basically my application is as it says in my first post. Want to create a checkout where i am able to process items. However I will need to have discounts and offers. So how do I implement that into the class that I have already created?

I involved Strategy Design Patterns because apparently thats the direction i need to be looking into for something like this

Let me know what you think!
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you will have to go back to the pencil and paper. Please find whoever told you that requires the strategy pattern and ask them to explain why. Write down how you think you might calculate such discounts. Only when you have an algorithm on paper shou‍ld you try to create any code.
 
Tom Bradely
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you've misunderstood me. The question is working out how to calculate offers within some code. For example, in the above post i gave the example that Milk might cost $2.00 however if you buy 4 of them they will cost you $7.00  as opposed to costing you $8.00. I'm not sure it needs an algorithm but rather a plan/way to implement this idea into code.

Hope that clears things up
 
William Ng
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Who is teaching you? This is not proper OO programming! you should really be creating more classes to handle the problem, not just using the main class, and some language defined classes. If done in proper OO fashion, this should be pretty trivial. Are you really sure this is how the Prof wants it?
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For your basic application, you should need at least a Product class. Presumably you will also want to be able to have a number of each item, so List is not a good structure to internally represent ShoppingCart with.

After you've rewritten your basic application, you can design a Discount interface. A discount takes a shopping cart and determines whether it applies, and if so, how large the discount is.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Ng wrote:Who is teaching you? This is not proper OO programming! You should really be creating more classes to handle the problem, not just using the main class, and some language defined classes. If done in proper OO fashion, this should be pretty trivial. Are you really sure this is how the Prof wants it?

Let's not jump to any conclusions about who designed what or specified anything. Keep in mind that programming is a learning process and that most, if not all, initial code/designs will be flawed in some way or another. Putting your ideas down in code is the best way to see just what mistakes you made.

This was an ok first iteration. The method to calculate the total needs to be reconsidered though. It is, in fact not a good implementation and is not object-oriented.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object-oriented design often requires you to reverse the way you think about things. In the real world, you take items out of a cart, figure out what the price is either by scanning the item and looking up its price or just looking at its price label, then adding the price to the total amount.

This conceptualization doesn't work well if you want to stick with OOP principles like encapsulation.

It is often said that getter methods break an object's encapsulation when you reach into an object and pull out the value of a field that is private to that object.

Inversion can often help improve an OO design. That is, instead of pulling out data from a cart item and using it in an external calculation, encapsulate the calculation in an object. Then pass that object to each cart item. The cart item will then pass it's price and quantity to the calculation object, which then uses that information to do what it needs to do.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is what the inverted approach would look like.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once you invert the calls and reassign responsibilities accordingly, it becomes easier to see how discounts can be applied using strategy objects. You just pass any number of strategy objects to the CartTotal object to say "use this strategy to calculate so-and-so discount promo". The CartTotal object can then delegate calculations to any applicable strategy objects based on the item code being totaled.
 
Tom Bradely
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry William, i'm not sure I came here to question who is teaching me and who is not. My question was rather straightforward. Yes it does require OOP, and yes i am sure it is fairly straightforward.

The code above was something I decided was ok to start with until I could think of a design. As Stephan and Junilu kindly advised, the next step is to think about how to start creating different classes to deal with certain parts of the program.

What i have in mind is, a base class of Item from which my items for sale derives from. A Cart will want to hold a list of Item's. I think i'll also want to have a separate PriceList that holds the item types and their related prices.I could decide to combine the special offers/discounts into the PriceList as another attribute, or have them in a separate data structure.

Is this the right direction to be headed into?

Thanks again
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You certainly could experiment with the approach you described. As you did before, put the idea down in code. That's the best way to see if it's a good way to go. My sense is that you'll see that it is still going to hard to work with it. At this point, I'm basing my opinion on heuristics that I have iny head, gained from experience. In other words, my instincts. I could still be wrong though. Write the code. The code doesn't lie. If the design is not good, the code will show its flaws.
 
Norm Radder
Rancher
Posts: 2240
28
 
Tom Bradely
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right I see

What other way would you advise that is different to my approach? Possibly better to find a way which i won't break my back for something that could be done easier.

Also Norm that was my post last night on dream in code, where i got the idea for the base item class
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!