views:

226

answers:

3
String [] A = {"High","Medium","Low"};
String [] B = {"High","Medium","Low"};
String [] C = {"High","Medium","Low"};
String [] D = {"High","Medium","Low"};
String [] E = {"High","Medium","Low"};
String [] F = {"High","Medium","Low"};

JComboBox Ai = new JComboBox(A); JComboBox Bi = new JComboBox(B);
JComboBox Ci = new JComboBox(C); JComboBox Di = new JComboBox(C);
JComboBox Ei = new JComboBox(E); JComboBox Fi = new JComboBox(F);

....

//add the user choice in arrayList
ArrayList<String> a = new ArrayList<String>();
a.add((String) Ai.getSelectedItem());
a.add((String) Bi.getSelectedItem());
a.add((String) Ci.getSelectedItem());
a.add((String) Di.getSelectedItem());
a.add((String) Ei.getSelectedItem());
a.add((String) Fi.getSelectedItem());

EDITED:
Scenario: There are 6 groups (Ai,Bi,Ci,Di,Ei,Fi) of choice. On each group, there are 3 sub choice (High(H),Medium(M),Low(L)).The user need to choose one on each of the 6 groups

The choice could be e.g. "HHHLLL" or "MMMLLM" or "HHLLMM" etc.

What is the best way to check and match the user choice without writing many else if ? e.g.

if(Ai=="High" && Bi=="High" && Ci=="Low" && Di=="High" && Ei=="Low" && Fi=="Medium") {
    System.out.println("Good Choice"); 
}

Thank you.

A: 

Assign integer to each choice and compute some evaluation function of use choice.

For instance: Ai * 3^0 + Bi * 3^1 + ... + Fi * 3^5

Then you would have user choice mapped to certain integer interval and could do something with it easier.

But, what do you actually want to do with it?

pajton
my math is so weak :-(
Jessy
easier to just append all answers in a canonical order to a string. That way there is no possible int overflow if the number of questions grows to a few dozen.
tucuxi
+2  A: 

First off, you don't need to give a new list of choices to each JComboBox.

String[] choices = {"High", "Medium", "Low"};

JComboBox ai = new JComboBox(choices);
JComboBox bi = new JComboBox(choices);
JComboBox ci = new JComboBox(choices);
JComboBox di = new JComboBox(choices);
JComboBox ei = new JComboBox(choices);
JComboBox fi = new JComboBox(choices);

(Variables in Java usually start with a lowercase letter, so I changed the variable names to lowercase.)


Next, you can put all six JComboBoxes in an array. You'll see why this is useful in a moment.

JComboBox[] boxes = {ai, bi, ci, di, ei, fi};

Now, you can make your user choice string like this:

// Create an ArrayList of Strings, where each string is either "H", "M", or "L"
ArrayList<String> userChoice = new ArrayList<String>()

for (JComboBox box : boxes) {
    // Go through this code once for each JComboBox in boxes
    // The first time through, "box" means the first JComboBox
    // The second time through, "box" is the second JComboBox, etc.
    if (box.getValue().equals("High")) {
         userChoice.add("H");
    } else if (box.getValue().equals("Medium")) {
         userChoice.add("M");
    } else if (box.getValue().equals("Low")) {
         userChoice.add("L")
    }
}

That for-each loop might not be familiar to you. It means "go through this code once for each something in array of somethings."


If you use this code, you'll end up with an ArrayList called userChoice that has something like ["H", "H", "M", "M", "L", "L"].

Wesley
Thank you. What is the different with "append" and "add" ?
Jessy
add is on ArrayList, append is on JComboBox. Two completely different classes. Isn't it obvious? :) They both add stuff to the end of their respective lists of data (although add is from a collections base type, so works differently on different types of collection as its implemented differently -- i.e., Set)
Chris Dennett
Thanks for commenting, Jessy - that was a mistake on my part. ArrayList has an "add" method; it does NOT have an "append" method. I've edited the answer so that it works.
Wesley
A: 

First of all, you should use an enum to represent the 3 choices. The == (that you used on strings) is safe on enum constants, because they're guaranteed to be singletons. An enum constant can be converted to and from strings easily (toString and valueOf).

So now you have 6 variables, and each can have one of 3 values, so there are 3^6 possible scenarios. What's the best way to handle them?

In the most general case, you have to be prepared to handle all 3^6 values independently of each other. That is, I'm assuming you're not doing grouped handling like "as long as there's more Highs than Lows, do X".

I recommend using a Map to map a setting combination to an action object.

enum Setting {
   High, Medium, Low;
}

class SettingCombination {
  List<Setting> combo = ...;
  // should be immutable, with proper equals and hashCode @Override
  // should also have named getters for each of the 6 settings
}

interface SettingCombinationHandler {
   void handle(SettingCombination settings);      
}

class SettingCombinationHandlingService {
   Map<SettingCombination, SettingCombinationHandler> handlers = ...;
   // perhaps provide a default handler as well
}

The idea is that for each valid combination, you map a specific handler for it. The mapping need not be one-to-one -- several combinations can be mapped to the same handler if this suits your need (the actual combination is passed as parameter to handle so it can differentiate them if it needs to).

Now, note that I haven't even addressed how you're going to display these setting options in the JComboBox or whatever else you may be using. This is intentional. You should separate your business logic from your UI; your data model from your view model.

polygenelubricants