-4

I should design a system which accepts two parameter sender and receiver and this two parameters check against set of rules and return true if they match any. These two parameter can accept wild characters like %% and _ which acts like query in sql server.

For example :

Input: London, Australia

Rules : 1- %IR%, Australia 2- London,US 3- UK,Canada

This returns false

If we add rule London,Austral% or rule Lon%,Australia% ,... this return true

How can I achieve this? Is Drool right tools for doing this task?

Thanks in advance.

Vahid Vakily
  • 306
  • 1
  • 7
  • 2
    Please provide the code you got so far with an [mcve]. Without it we will struggle to give you an answer that will help you. Questions without code examples and clear questions tend to get some downvotes. – izlin Jul 10 '18 at 09:13
  • I add more explanation for problem @izlin – Vahid Vakily Jul 10 '18 at 09:17
  • What should happen if someone types `1` or `US` ? Will return true or false? And I can't understand your rule: how is that separated? If i use comma as separator, I see a rule like **Australia 2- London**, pretty strange – Leviand Jul 10 '18 at 09:29
  • there are set of rules which are applied in order. – Vahid Vakily Jul 10 '18 at 09:32
  • 1- %IR% , Australia is the First rule . 2- London,Us is the second rule and so on – Vahid Vakily Jul 10 '18 at 09:33
  • rule are seperate each by new line . and in each rule first and second argument are seperated by , – Vahid Vakily Jul 10 '18 at 09:36
  • There is no logic in result : when passing **London, Australia** you get false, but when typing **London,Austral%** you get true. In first example typing a complete city/state name return false, in the second is correct... how is that possible? The logic is broken imho – Leviand Jul 10 '18 at 09:41
  • Maybe i explain my problem very bad. Imagine you have a table with two column , SenderCountry and ReceiverCountry. You want return true if your two arguments (X,Y) is found in table in the samerow. but cells you cannot simply select where SenderCountry=X and ReceiverCountry=Y because the senderCountry and receiverCountry may have wildcharacter. For example, if you type lon% in senderCountry, it acts the same as like in sql. – Vahid Vakily Jul 10 '18 at 09:47
  • Ok now I've understand, I've posted an answer, see if it's correct :) – Leviand Jul 10 '18 at 10:39

1 Answers1

1

Ok, now that I've understand what you need , here's the solution: I've written alot of stuff and prints so you can easly understand the code.

Basically first I've putted your rules into a map, and based on user input I'm separating sender from receiver, and if the input ends with char "%", then it is used as wildcard.

You can test differente outputs by commenting / uncommenting the differente input that I've put in the code.

Let me know :)

HashMap<String,String> map = new HashMap<>();
    map.put("IR", "Australia");
    map.put("London", "US");
    map.put("UK", "CANADA");

    String input = "London, Australia"; //false
//        String input = "Lon%, US"; //true
//        String input = "Lon%, U%"; //true
//        String input = "U%, CANADA"; //true

    StringTokenizer token = new StringTokenizer(input,",");
    String senderInput = token.nextToken();

    System.out.println("Sender:"+senderInput);

    for(String senderMap : map.keySet()){
        boolean firstCondition;
        if(senderInput.endsWith("%")) {
            String senderMatch = senderInput.replace("%", "");
            firstCondition = senderMap.startsWith(senderMatch);
        }else {
            firstCondition = senderMap.equals(senderInput);
        }

        if(firstCondition){
            System.out.println("Sender matched with "+senderMap);
            String receiverInput = token.nextToken().replaceAll(" ","");
            System.out.println("Receiver:"+receiverInput);
            String receiverMap = map.get(senderMap);

            boolean secondCondition;
            if(receiverInput.endsWith("%")) {
                String receiverMatch = receiverInput.replace("%", "");
                secondCondition = receiverMap.startsWith(receiverMatch);
            }else {
                secondCondition = receiverMap.equals(receiverInput);
            }

            if(secondCondition){
                System.out.println("MATCHING!");
            }else{
                System.out.println("NOT MATCHING!");
            }
        }
    }
Leviand
  • 2,745
  • 4
  • 29
  • 43