wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Hashtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hashtable" Watch "Hashtable" New topic
Author

Hashtable

sumit sharmast
Greenhorn

Joined: Jul 29, 2012
Posts: 15
Dear All,

I tried to use Hastable in Cart.Java class insted of given arraylist but not able to do it. I tried a lot but not successful. How can I do it, please guide me.

Regards,

Sumit.



-------------------------------------------------------------------------------------------------------------------------------------------
Below is the arraylist code of this class
--------------------------------------------------------------------------------------------------------------------------------------------
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Don’t use Hashtable; use HashMap instead.

Why are you trying to substitute a Map for a List? The two are different.
I tried to use Hastable in Cart.Java class insted of given arraylist . . .
That is rather like saying, “I tried using a screwdriver instead of a pair of scissors.” Maps and Lists are as different as scissors and screwdrivers, or chalk and cheese. If you need a List, you need a List. Go through the Java Tutorials, and look particularly at the sections about “the Map interface” and “the Map interface”, which should explain how Maps differ from Lists. Only when you understand how they differ will you know which to use. If there is anything you don’t understand, be sure to ask again.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7554
    
  18

sumit sharmast wrote:I tried to use Hastable in Cart.Java class insted of given arraylist but not able to do it. I tried a lot but not successful. How can I do it, please guide me.

Well, a few things:

1. Your LineItem class isn't very normalized. Specifically:
  • It contains a totalQuantity field, which is redundant - it's either derived data (ie, price * quantity), or it properly belongs in your Cart class.
  • It doesn't appear to reference an Item, which is where I would have my price, description and code fields. That doesn't mean that it's not also a good idea to have a price in your LineItem class, but I think I'd call it something like 'salePrice': The one in an Item class might be the MRRP, and the one in the LineItem class the "actual selling price", which would include any discounts or promos.

  • 2. I wouldn't supply setters for every field - or at least I certainly wouldn't make them all public - unless it's required because it's some type of Bean.

    3. If you're going to use a HashMap for the LineItems in your Cart, I'd suggest a LinkedHashMap, because they're likely to need a predictable order. But to be honest, an ArrayList seems fine to me, unless you're expecting thousands of LineItems to a Cart.

    HIH

    Winston
    Seetharaman Venkatasamy
    Ranch Hand

    Joined: Jan 28, 2008
    Posts: 5575

    welcome to JavaRanch sumit sharmast
    sumit sharmast
    Greenhorn

    Joined: Jul 29, 2012
    Posts: 15
    Thank you everyone for the suggestions.
    sumit sharmast
    Greenhorn

    Joined: Jul 29, 2012
    Posts: 15
    Dear All,

    I tried to change from arraylist to hashtable. i changed because I want to see how the same thing is possible with hastable. I am a beginner and I am learning how to do same thing using different collections.

    I am getting the following error in the SERVLET CODE. Please help.

    ERROR MESSAGE:

    HTTP Status 500 -

    --------------------------------------------------------------------------------

    type Exception report

    message

    description The server encountered an internal error () that prevented it from fulfilling this request.

    exception

    javax.servlet.ServletException: Servlet execution threw an exception


    root cause

    java.lang.Error: Unresolved compilation problems:
    The method addItem(String, LineItem) in the type Cart is not applicable for the arguments (LineItem)
    The method removeItem(String, LineItem) in the type Cart is not applicable for the arguments (LineItem)

    music.cart.DisplayCartServlet.doPost(DisplayCartServlet.java:87)
    music.cart.DisplayCartServlet.doGet(DisplayCartServlet.java:114)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.20 logs.

    Apache Tomcat/6.0.20
    --------------------------------------------------------------------------------
    LineItem.java Code



    Cart.Java Code


    ---------------------------------------------------------------------

    SERVLET CODE:


    -----------------------------------------------------------------------------------


    JDBC CODE:

    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38107
        
      22
    If you have an unresolved compilation problem, you should go back to Eclipse and over your mouse on the little red mark there. It will give an explanation of the problem and suggest some possible solutions.
    You should never have tried to run that code; Eclipse will have give you a warning that there are errors, and you ought to have corrected them then.
    sumit sharmast
    Greenhorn

    Joined: Jul 29, 2012
    Posts: 15
    Campbell Ritchie wrote:If you have an unresolved compilation problem, you should go back to Eclipse and over your mouse on the little red mark there. It will give an explanation of the problem and suggest some possible solutions.
    You should never have tried to run that code; Eclipse will have give you a warning that there are errors, and you ought to have corrected them then.



    What will be the correct argument in the Servlet code inside the addItem method, in place of addItem(t) ???
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7554
        
      18

    sumit sharmast wrote:Dear All...

    Sumit. Please don't put such long lines in your code blocks. I've broken them up this time, but please re-read the UseCodeTags page. Thoroughly.

    Thanks

    Winston
     
    wood burning stoves
     
    subject: Hashtable
     
    Similar Threads
    Reiterating tags
    problem updating quantity field
    static utility class from servlet
    problems when using a datable within a form: currently my code is clunky
    JList set element visible