• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Hashtable

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
--------------------------------------------------------------------------------------------------------------------------------------------
 
Marshal
Posts: 80463
453
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
     
    Ranch Hand
    Posts: 5575
    Eclipse IDE Windows XP Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    welcome to JavaRanch sumit sharmast
     
    sumit sharmast
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you everyone for the suggestions.
     
    sumit sharmast
    Greenhorn
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Marshal
    Posts: 80463
    453
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 15
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic