This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Question related to Emumeration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question related to Emumeration" Watch "Question related to Emumeration" New topic
Author

Question related to Emumeration

Liam Smith
Greenhorn

Joined: Oct 23, 2012
Posts: 8
I am having issue with the following code. I need to return the description from enumeration to the customerType.toString. Yet, when I declare the variable in the main app, then I will get error when I use cta.setDiscount(CustomerType);

Am I missing something here? Here's my code.

Main App


Enumeration code

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37923
    
  22
Welcome to the Ranch

That is not an Enumeration, but an enumerated type. Enumeration is an old legacy interface which you ought not to use in new code.
What you are doing is getting confused about what you can use for the switch‑case statement. You ought to be using the members of that enum, bvut you have managed to write it taking Strings. Change the set discount method parameter to take the customer type enum as an argument, and it should work better.
Various things could be tidied up:-
  • I don’t like the name of the set discount method. Maybe setDiscountFromCustomerType???
  • You are calling the getDiscount method twice, but only using its return value once.
  • Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7495
        
      18

    Liam Smith wrote:I am having issue with the following code. I need to return the description from enumeration...

    1. Be careful when using the term "enumeration" to Java programmers - especially with a capital 'E' - because there is already a legacy class called Enumeration, and it's NOT the same as an enum.

    2. Seems to me that you could put your discounts in your enum, viz:and save yourself a pile of problems.

    HIH

    Winston


    Isn't it funny how there's always time and money enough to do it WRONG?
    Articles by Winston can be found here
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7495
        
      18

    Liam Smith wrote:I am having issue with the following code...

    One other non-Java thing:
    ALL your customer types get discounts, so isn't it a bit of a misnomer? Why not just set your prices lower?

    Either that, or break up "RETAIL" into something like "NORMAL" (0%) and "PREFERRED" (10%).

    Winston
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37923
        
      22
    Winston Gutkowski wrote: . . . Why not just set your prices lower? . . .
    Liam Smith
    Greenhorn

    Joined: Oct 23, 2012
    Posts: 8
    Winston Gutkowski wrote:
    Liam Smith wrote:I am having issue with the following code. I need to return the description from enumeration...

    1. Be careful when using the term "enumeration" to Java programmers - especially with a capital 'E' - because there is already a legacy class called Enumeration, and it's NOT the same as an enum.

    2. Seems to me that you could put your discounts in your enum, viz:and save yourself a pile of problems.

    HIH

    Winston


    Thank you for clearing up difference between enum and Enumeration.

    I would put discount in the enum, but unfortunately text call for method to be used in the main method not in enum.
    Liam Smith
    Greenhorn

    Joined: Oct 23, 2012
    Posts: 8
    Campbell Ritchie wrote:Welcome to the Ranch

    That is not an Enumeration, but an enumerated type. Enumeration is an old legacy interface which you ought not to use in new code.
    What you are doing is getting confused about what you can use for the switch‑case statement. You ought to be using the members of that enum, bvut you have managed to write it taking Strings. Change the set discount method parameter to take the customer type enum as an argument, and it should work better.
    Various things could be tidied up:-
  • I don’t like the name of the set discount method. Maybe setDiscountFromCustomerType???
  • You are calling the getDiscount method twice, but only using its return value once.


  • Thank you for a warm welcome! Ok, good to know about Enumeration and enumerated type.

    I have tried to change set discount method parameter, and I am not sure how? I am getting errors.

    Thank you for other suggestions, I have applied the changes and as soon I figure out how to make set discount method to take enum, I will re-posted the code.
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7495
        
      18

    Campbell Ritchie wrote:
    Winston Gutkowski wrote: . . . Why not just set your prices lower? . . .

    Cynic. Mind you, I was made privy (a while ago and, I'm sure, by mistake) to the list of "discounts" available, even to proles like me, at places like Curry's.

    Basically, it works like this:
    If you moan about the price, the floor 'sales lackey' has about 5% discretion to try and get a sale if they want. For anything between about 5-15%, a "manager" has to be called (just so you know you're really getting a good deal).
    After that, you'll need to produce proof that you can still get it cheaper elsewhere - and watch out for "added extras" (like delivery charges, or non-requested insurance or extended guarantees) on your bill.
    I recently discovered (only once I got home unfortunately) that a local appliance store had charged me 8 Euros "service" for taking a freezer I'd bought out to my car (all of 20 yards), because I'd blagged a good price out of them. I couldn't be fagged to go back and complain because the one I got was 70 Euros cheaper than I could've got elsewhere (which, I suspect, is what they gamble on).

    It seems like everything works on the "car sale" paradigm these days <sigh>.

    Winston
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37923
        
      22
    About 25 years ago somebody bought a microwave at Currys’ … and took it back because there were missing segments on the LED display.
    Then I bought a microwave at Currys’ and took it back a few days later because there were missing segments on the LED display. Yes, it was the same microwave. They were falling over themselves to give me a new microwave and discounts.
    I still have that second microwave, and it still works.
    Liam Smith
    Greenhorn

    Joined: Oct 23, 2012
    Posts: 8
    I am still struggling on this issue. Is there any link or information where I can look up more about how to call enum as argument in method?

    Thanks!
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Liam Smith wrote:I am still struggling on this issue. Is there any link or information where I can look up more about how to call enum as argument in method?

    Thanks!


    You can certainly google for something like java enum tutorial. If you want help here though, you'll need to provide more details about what you tried and what problems you're having with it.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37923
        
      22
    Once we start chatting about prices and microwaves, you have had it. The thread has gone for a Burton. But this is probably the tutorial Jeff V mentioned. If you go to the index page ad look for “branching statements”, you will probably find about the switch-case statement.

    Actually, you won’t find it there. Try here.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Question related to Emumeration
     
    Similar Threads
    Instantiating an enum
    Enums and the toString method
    Class and Object Problem
    Exception in thread "main" java.lang.NullPointerException
    question about adding to a vector