• Post Reply Bookmark Topic Watch Topic
  • New Topic

class and object problem  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem:
Create a class Item which refers to an item in the shopping cart of an online shopping site like Amazon. The class Item has the fields name (of type String), productId (of type String), price (of type double), quantity (of type int), amount (of type double). The field amount is calculated as price * quantity.
Also define a class ShoppingCart which refers to the shopping cart of a customer at Amazon. The class ShoppingCart has the fields items (of type Item[]), totalAmount (of type double).
Also define a class TestCart which has a function makeCart(String[] itemData) which takes the information about the shopping cart of a customer as input and returns an object of type ShoppingCart.The input String[] items, consists of an array of String where each String provides information on an item in the manner “name,id,price, quantity”. For e.g. it can be “Colgate,CP10023,54.50,3″ where Colgate is the name, CP10023 is the product id, 54.50 is the price and 3 is the quantity. Note that the ShoppingCart object returned should have the correct totalAmount and each Item in it should the correct amount.


My Solution:





I'm really not getting this:
Also define a class TestCart which has a function makeCart(String[] itemData) which takes the information about the shopping cart of a customer as input and returns an object of type ShoppingCart.The input String[] items, consists of an array of String where each String provides information on an item in the manner “name,id,price, quantity”. For e.g. it can be “Colgate,CP10023,54.50,3″ where Colgate is the name, CP10023 is the product id, 54.50 is the price and 3 is the quantity. Note that the ShoppingCart object returned should have the correct totalAmount and each Item in it should the correct amount.


trying to code like this but its not working. I think I'm doing it wrong
 
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget about the implementation of the makeCart() method for the moment.

The requirement says the makeCart() method takes a String array, which you declared correctly.


But you not calling this method correctly. Currently you passing a Item array when it should be a String array.Also not that the array only pass in 4 parameters. The amount is calculated/ generated.

Now the next step is how to convert this so-called String array to your Item object and put this into your ShoppingCart?

Hint: use the List interface then the toArray() method.
 
Abhinav Pande
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:

Hint: use the List interface then the toArray() method.


I have to do without List Interface and toArray() method
 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are required to do is create a factory... The TestCart class is going to be the factory that creates ShoppingCart objects using the method makeCart(String[])... The heavy work in doing this is creating an algorithm to satisfy the requirements.

What I can do is give you a push to get you going... So here I go:

1. Create an array of type Item in the method and instantiate it to the same length of the argument passed in.
2. Create a loop to iterate over the String array argument and tokenize each String object in the String array argument using the split() method. (This is to create the fields such as name, productId, etc. which will then be used to create items)
3. Create an Item object using the fields created from splitting the String object and add it to the Item array.
4. After the loop you will have an Item array full of items. Create and return a new ShoppingCart object using the Items array.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote:2. Create a loop to iterate over the String array argument and tokenize each String object in the String array argument using the split() method. (This is to create the fields such as name, productId, etc. which will then be used to create items)
3. Create an Item object using the fields created from splitting the String object and add it to the Item array.

@Abhinav: And I'd go one further and say that both (2) and (3) should be part of your Item class - ie, add a:
public Item(String s) { ...
constructor to it. Or possibly even a:
public Item(Scanner scanner) { ...
one (although it would probably be better to make either one a static factory method).

In general, classes should contain everything that pertains to them; and that includes constructors/factories to create themselves in whatever way is required.

You may find the FirstClasses page worth reading on this subject, but I warn you: it's not short, as it includes quite a lot of other stuff which you may or may not find useful at this stage.

HIH

Winston
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To give you some more motivation, have a look at the output of what your implementation should look like...
Screenshot-from-2014-04-19-16-44-50.png
[Thumbnail for Screenshot-from-2014-04-19-16-44-50.png]
Online Shopping Cart
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!