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

  • Home
  • SEARCH
  • 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 7934595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:41:19+00:00 2026-06-03T21:41:19+00:00

I’d like some advice on refactoring the following method: public boolean makeDecision(String group, int

  • 0

I’d like some advice on refactoring the following method:

public boolean makeDecision(String group, int level, int primaryAmount, int secondaryAmount)
{
    if (group.equals("A"))
    {
        switch (level)
        {
            case 0 : return primaryAmount > 10000;break;
            case 1 : return primaryAmount > 20000;break;
            default : return secondaryAmount > 30000; break;
        }
    }
    else if (group.equals("B"))
    {
         switch (level)
         {
              case 0 : return primaryAmount > 40000;break;
              case 1 : return primaryAmount > 50000;break;
              default : return secondaryAmount > 60000; break;
         }

    }
    else if (group.equals("C"))
    {
        switch(level)
        {
            case 0 : return primaryAmount > 70000;break;
            case 1 : return primaryAmount > 80000;break;
            default : return secondaryAmount > 90000; break;
        }

    }
    return false;
} 

What I’d like to achieve:

  • Allow the code to follow the open/closed principle as there will be more groups / levels in time.
  • Remove the duplication in the ‘level’ switch statement.
  • Ideally remove the ‘group’ top level switch statement.
  • 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-03T21:41:20+00:00Added an answer on June 3, 2026 at 9:41 pm

    Groups look like they have behaviour. You could promote the string group to a first class type. You can also put methods on the class to represent the logic you have got. I’ve put daft names on the variables, they could do with much better ones.

    public class Group {
        public static Group A = new Group(10000,20000,30000);
        public static Group B = new Group(40000,50000,60000);
        public static Group C = new Group(70000,80000,90000);
    
        private int primaryMin;
        private int primaryMid;
        private int secondaryMax;
    
        private Group(int min, int mid, int max) {
            primaryMin = min;
            primaryMid = mid;
            secondaryMax = max;
        }
    
        public boolean getLevel(int level, int primaryAmount, int secondaryAmount) {
           if (level == 0)
             return primaryAmount > primaryMin;
           else if (level == 1) 
             return primaryAmount > primaryMid;
           else 
             return secondaryAmount > secondaryMax;
        }
    }
    

    So now you can reduce your top level statement down to

    public boolean makeDecision(Group group, int level, int primaryAmount, int secondaryAmount) {
      return group.getLevel(level, primaryAmount, secondaryAmount);
    }
    

    You may wish to consider using the null object pattern to handle an unknown group.

    If you say that the levels will grow in time, then I would consider doing almost exactly the same again to introduce the Level class and push the if/else chain in there as another layer of polymorpshim. This would then become the double dispatch pattern as first you’d dispatch on the type of Group and then on the type of Level. This should mean that you can add new code without needing to modify the existing ones.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I have some data like this: 1 2 3 4 5 9 2 6
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
Specifically, suppose I start with the string string =hello \'i am \' me And

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.