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?

Alec
  • 121
  • 5
  • 1
    Java does provide a [PriorityQueue](https://docs.oracle.com/javase/8/docs/api/java/util/PriorityQueue.html) 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 May 27 '15 at 09: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 May 27 '15 at 09:53
  • please don't **[cross-post](http://meta.stackexchange.com/tags/cross-posting/info "'Cross-posting is frowned upon...'")**: http://stackoverflow.com/questions/30458568/what-would-be-the-best-way-to-implement-priority-based-programming-without-addin – gnat May 27 '15 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. – Rachana Shah May 27 '15 at 12:18
  • possible duplicate of [Approaches to checking multiple conditions?](http://programmers.stackexchange.com/questions/191208/approaches-to-checking-multiple-conditions) – gnat Jun 26 '15 at 17:29
  • It appears to be an entirely UI design issue, relating to ways to allow users to indicate the relative priorities between pairs of objects at a time. The theoretical basis is https://en.wikipedia.org/wiki/Partially_ordered_set , meanwhile there are plenty of simple tricks that will do the same, but choosing the right code depends on the UI design. (This assumes the numbers of items will remain practically small.) – rwong Aug 25 '15 at 21:06

1 Answers1

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.

Neil
  • 22,670
  • 45
  • 76
  • thanks for the detailed explanation.. I'll check if this solution works out for us and get back to you if required! – Rachana Shah May 29 '15 at 06: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 May 29 '15 at 07: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 Aug 25 '15 at 21:07