Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8371875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:16:00+00:00 2026-06-09T14:16:00+00:00

How to avoid too many if condition when the return type is dynamic? I

  • 0

How to avoid too many if condition when the return type is dynamic?

I understand that we can create a hashmap with key and value if the condition and the value is static too avoid too many if conditions.

In the below example the price for each month is different from customer to
customer…i need to add 12 if statements for the same…assume if the field is something
other than the month…they i may endup with more than 100 or 200 if’s…ex: for year…1900 to 2000

Example,

public String getPrice(int Month) {
   String price = "";
   switch (month) {
        case 1:
          price = customerTable.getJanPrice();
          break;
        case 2:
          price = customerTable.getFebPrice();
          break;
         ...........
    }
    return price;
}

Could you please help me out with your suggestions/comments?

Thanks,
Kathir

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:16:01+00:00Added an answer on June 9, 2026 at 2:16 pm

    Create an Enum with the prices that you want.

    public enum Month 
    {
        SEPTEMBER (3.33),
        OCTOBER (4.85),
        NOVEMBER (5.98),
        etc ;
    
        public final double Price;
    
        Month(double monthPrice)
        {
            this.Price = monthPrice;
        }        
    }
    

    Then to access that price:

    Month.SEPTEMBER.Price
    

    EDIT
    Because the data is changed at runtime and their are an ‘infinite’ amount of possibilities using Classes would be a more manageable option.

    public class Year
    {   
        public final List<Month> Months;
    
        public Year(List<Month> months)
        {
            Months = months;
        }
    }
    
    public class Month
    {
        public final double Price;
    
        public Month(double price)
        {
            Price = price;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I avoid NullPointerExceptions ? I have tried using try-catch blocks but that
Routines can have parameters, that's no news. You can define as many parameters as
I have a maven multi-module project (boy, I've written that opening way too many
I'd like to avoid this scary messages when users install my application: I understand
in order to avoid copy/paste, i can use a unique view for different actions,
How can I avoid the document text and the header overlapping?
Possible Duplicate: How many files in a directory is too many? I was told
Sometimes I start a MATLAB script and realize too late that it is going
I got this hint from mssqlcity.com . However, I cannot understand its explanation. Avoid
I have a folder with far too many files in, and I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.