1

We need to add priority based programming in our application using java programming language. For example, there are three priorities that a phone can have: A1, A2 and A3. Also there can be three different types of phone numbers like home (H), office(O) and mobile(M). There have to be default priorities like:

M - A1

H - A2

O - A3

Now the priorities will also change based on user preferences. For example, if O comes with priority A1, M and H will change to A2 and A3.

What will be the best way to implement this logic without adding too much of if-else processing logic in the code?

6
  • 1
    Java does provide a PriorityQueue which would allow you to prioritize things as they come along, so basically you just provide a comparer, or does this not suit your needs?
    – npinti
    Commented May 27, 2015 at 9:35
  • It isn't clear why changing the priority of one number should change the priorities of the other numbers. Are there multiple numbers with types H, O, and M? Changing the priority of type O will modify the priorities of all numbers with type M and H or is the user only changing the priority of that particular number? Can two numbers with different number types ever share the same priority?
    – Neil
    Commented May 27, 2015 at 9:53
  • please don't cross-post: stackoverflow.com/questions/30458568/…
    – gnat
    Commented May 27, 2015 at 11:12
  • @Neil we are giving priority to user preference, so if the user decides that H has higher priority, we will set it accordingly and change the priorities for other types of phone numbers. Commented May 27, 2015 at 12:18
  • possible duplicate of Approaches to checking multiple conditions?
    – gnat
    Commented Jun 26, 2015 at 17:29

1 Answer 1

1

It seems to me that priority is not a property of a number type if priority is determined based on the priorities of the other number types, so I think the best approach would be to place the number types in a List.

In other words, you have a List of Lists. The outer List determines the priority of the numbers contained within while the inner List contains the numbers themselves.

Suppose the position 0 in your outer list corresponds to priority A1, position 1 corresponds to priority A2, and position 2 corresponds to priority A3. If you add more priorities later, you can add more, but suffice to say that the number of priorities corresponds to the number of possible number types.

Suppose the user wishes to change the priority of those of A1 to A3. Well according to our List, we'd simply need to remove the first list of numbers and append it to the end. The second item in the list now is the first and the third is now the second. The priority of each number therefore is determined by its position in the list, so you know the priority of a number based on which list you retrieved it from.

I hope that's clear. If not, let me know and I'll try to clarify in my answer.

3
  • thanks for the detailed explanation.. I'll check if this solution works out for us and get back to you if required! Commented May 29, 2015 at 6:59
  • @RachanaShah If you need to be able to know the priority from a number, you still don't have to add a property to Number that indicates priority. You can create other data structures that allow you to determine the inverse, without much additional memory cost. Depends on if you'll need to do that or not. If so, I'll add it to my answer.
    – Neil
    Commented May 29, 2015 at 7:30
  • A simplified summary is that to implement the UI handling of a user "dragging and dropping" an item from one place to another place, one would implement a remove item followed by an insert item.
    – rwong
    Commented Aug 25, 2015 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.