3
\$\begingroup\$

Alright so this isn't an issue with my program not working, it's more of a fact that I wanna learn to code better.

This is one of 6 if else statements that I use inside of my program. What I'm trying to do is make it shorter (easier) to do. I am learning AS3 with Flex 4.6 (started with Flex 4).

If I left something out that is important let me know I'll get it for you. All the .text's can be changed at any time the user wants.

Thanks In advance for any help!

            if (ft.text == "Woven Acrylic" && at.text == "Motorized" && asf.text == "10"){
                bap.text = String(Number(1147))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized" && asf.text == "11"){
                bap.text = String(Number(1217))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized" && asf.text == "12"){
                bap.text = String(Number(1297))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized" && asf.text == "13"){
                bap.text = String(Number(1343))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized" && asf.text == "14"){
                bap.text = String(Number(1407))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized" && asf.text == "15"){
                bap.text = String(Number(1467))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized" && asf.text == "16"){
                bap.text = String(Number(1530))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized" && asf.text == "17"){
                bap.text = String(Number(1587))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized" && asf.text == "18"){
                bap.text = String(Number(1632))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized" && asf.text == "19"){
                bap.text = String(Number(1709))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized" && asf.text == "20"){
                bap.text = String(Number(1754))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized XL" && asf.text == "14"){
                bap.text = String(Number(1490))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized XL" && asf.text == "15"){
                bap.text = String(Number(1560))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized XL" && asf.text == "16"){
                bap.text = String(Number(1636))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized XL" && asf.text == "17"){
                bap.text = String(Number(1703))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized XL" && asf.text == "18"){
                bap.text = String(Number(1758))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized XL" && asf.text == "19"){
                bap.text = String(Number(1848))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized XL" && asf.text == "20"){
                bap.text = String(Number(1903))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized Pro" && asf.text == "10"){
                bap.text = String(Number(1247))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized Pro" && asf.text == "11"){
                bap.text = String(Number(1317))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized Pro" && asf.text == "12"){
                bap.text = String(Number(1397))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized Pro" && asf.text == "13"){
                bap.text = String(Number(1443))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized Pro" && asf.text == "14"){
                bap.text = String(Number(1507))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized Pro" && asf.text == "15"){
                bap.text = String(Number(1567))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized Pro" && asf.text == "16"){
                bap.text = String(Number(1603))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized Pro" && asf.text == "17"){
                bap.text = String(Number(1687))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized Pro" && asf.text == "18"){
                bap.text = String(Number(1732))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized Pro" && asf.text == "19"){
                bap.text = String(Number(1823))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized Pro" && asf.text == "20"){
                bap.text = String(Number(1878))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized Pro XL" && asf.text == "14"){
                bap.text = String(Number(1509))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized Pro XL" && asf.text == "15"){
                bap.text = String(Number(1660))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized Pro XL" && asf.text == "16"){
                bap.text = String(Number(1736))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized Pro XL" && asf.text == "17"){
                bap.text = String(Number(1803))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized Pro XL" && asf.text == "18"){
                bap.text = String(Number(1858))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized Pro XL" && asf.text == "19"){
                bap.text = String(Number(1962))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Motorized Pro XL" && asf.text == "20"){
                bap.text = String(Number(2027))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Oasis" && asf.text == "10 x 16"){
                bap.text = String(Number(1305))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Vista" && asf.text == "10"){
                bap.text = String(Number(927))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Vista" && asf.text == "11"){
                bap.text = String(Number(997))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Vista" && asf.text == "12"){
                bap.text = String(Number(1077))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Vista" && asf.text == "13"){
                bap.text = String(Number(1123))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Vista" && asf.text == "14"){
                bap.text = String(Number(1187))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Vista" && asf.text == "15"){
                bap.text = String(Number(1247))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Vista" && asf.text == "16"){
                bap.text = String(Number(1310))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Vista" && asf.text == "17"){
                bap.text = String(Number(1367))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Woven Acrylic" && at.text == "Vista" && asf.text == "18"){
                bap.text = String(Number(1412))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Motorized" && asf.text == "10"){
                bap.text = String(Number(1047))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Motorized" && asf.text == "11"){
                bap.text = String(Number(1107))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Motorized" && asf.text == "12"){
                bap.text = String(Number(1167))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Motorized" && asf.text == "13"){
                bap.text = String(Number(1213))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Motorized" && asf.text == "14"){
                bap.text = String(Number(1257))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Motorized" && asf.text == "15"){
                bap.text = String(Number(1307))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Motorized" && asf.text == "16"){
                bap.text = String(Number(1360))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Motorized" && asf.text == "17"){
                bap.text = String(Number(1407))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Motorized" && asf.text == "18"){
                bap.text = String(Number(1452))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Motorized Pro" && asf.text == "10"){
                bap.text = String(Number(1147))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Motorized Pro" && asf.text == "11"){
                bap.text = String(Number(1207))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Motorized Pro" && asf.text == "12"){
                bap.text = String(Number(1267))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Motorized Pro" && asf.text == "13"){
                bap.text = String(Number(1313))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Motorized Pro" && asf.text == "14"){
                bap.text = String(Number(1357))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Motorized Pro" && asf.text == "15"){
                bap.text = String(Number(1407))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Motorized Pro" && asf.text == "16"){
                bap.text = String(Number(1460))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Motorized Pro" && asf.text == "17"){
                bap.text = String(Number(1507))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Motorized Pro" && asf.text == "18"){
                bap.text = String(Number(1552))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Vista" && asf.text == "10"){
                bap.text = String(Number(827))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Vista" && asf.text == "11"){
                bap.text = String(Number(887))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Vista" && asf.text == "12"){
                bap.text = String(Number(947))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Vista" && asf.text == "13"){
                bap.text = String(Number(993))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Vista" && asf.text == "14"){
                bap.text = String(Number(1037))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Vista" && asf.text == "15"){
                bap.text = String(Number(1087))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Vista" && asf.text == "16"){
                bap.text = String(Number(1140))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Vista" && asf.text == "17"){
                bap.text = String(Number(1187))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else if (ft.text == "Traditonal Laminated" && at.text == "Vista" && asf.text == "18"){
                bap.text = String(Number(1232))
                tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
            }
            else (bap.text = "0");
// TODO Auto-generated method stub
            tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text));


        }
\$\endgroup\$

migrated from stackoverflow.com May 24 '12 at 12:45

This question came from our site for professional and enthusiast programmers.

10 Answers 10

7
\$\begingroup\$

Since you want to learn to code properly, my advice would be to create a model for your application (read up on the model-view-controller pattern if you haven't already). I'll try to get you started with this example.

For starters, your variable names don't speak for themselves, which leaves me (or any other developer) guessing as to what their value is. I'll guess a bit and I seem to see a combination of 3 properties that make up one unique ID. We'll create a model class for that item; let's call it ShopItem.

[Bindable]
public class ShopItem {

    public var id:int;
    public var material:Material;
    public var type:Type;
    public var mysteriousId:int;

}

As you see it has an ID property, plus the three other properties that make up a unique combination. The last property is just an int as I have no more information about it than that, but it could be a class just as the others. Also, I have made the class Bindable, so we can use it for data binding later on. Now lets create the other two classes.

[Bindable]
public class Material {

    public var id:int;
    public var label:String;

}

[Bindable]
public class Type {

    public var id:int;
    public var label:String;

}

We give these an ID and a label. This has two main advantages:

  • if you want to change the labels, or translate your application, the logic doesn't break (as it would with your code)
  • you can now use this class to store this data in a database

Now we can create a collection of ShopItems:

    var items:IList = new ArrayCollection();

    var material:Material = new Material();
    material.id = 123;
    material.label = 'Woven Acrylic';

    var type:Type = new Type();
    type.id = 456;
    type.label = 'Motorized Pro';

    var item:ShopItem = new ShopItem();
    item.id = 1;
    item.material = material;
    item.type = type;
    item.mysteriousId = 789;

    items.addItem(item);

We can now create the following method to replace your code:

private function findShopItem(material:Material, type:Type, mysteriousId:int):ShopItem {
    for each (var item:ShopItem in items) {
        if (item.material == material &&
            item.type == type &&
            item.mysteriousId == mysteriousId)
                return item;
    }

    return null;
}

Edit: I understand that this is the most difficult answer for you to get your head around, because it has a certain level of abstraction you're not accustomed to yet. It is also just meant to confront you with the concept of a data model and get you started. I think from here on you can do a few things:

  • A data model and data binding between the view and the model is at the core of every Flex application. You should be able to find some decent tutorials on this concept.
  • Or just read an entry-level book on Flex development; the first chapters usually cover data binding.
  • Take a step back and think about your application only in terms of what it's supposed to do; then functionally design the views you will need to accomplish that functionality; create a data model; create the views (usually pure MXML); try to bind the data model to your views; feel free to ask questions as you go either here on StackOverflow
\$\endgroup\$
  • 1
    \$\begingroup\$ I just noticed that Mark's and Maheshes answers are fairly similar in approach, except mine takes the abstraction one step further. Consider this situtation: you want to change the name 'Woven Acrylic' into something else at some point. With my approach you'll have to change that value once. With theirs, you may have to replace hundreds of values, which is more time consuming and more prone to errors. \$\endgroup\$ – RIAstar May 24 '12 at 7:58
  • \$\begingroup\$ Hi thanks, I am going to read the link you posted, so you can understand a bit better, its an awning, the type is for the motor the awning uses the material is the fabric, and the mysteriousId is actually the size of the awning. The text boxs are were the user inputs what there looking for and then when they hit the button to get a price it runs that if statement. \$\endgroup\$ – Joshua Miller May 25 '12 at 2:19
  • \$\begingroup\$ I guess my issue with your code is I have no idea how to make that work, for each and every combination, am i typing more code and its just making it more stable? I really dont understand at all but it def sounds like the best way to go \$\endgroup\$ – Joshua Miller May 25 '12 at 2:42
  • \$\begingroup\$ @JoshuaMiller I've added some "where to go from here?" info to my answer. \$\endgroup\$ – RIAstar May 25 '12 at 9:00
4
\$\begingroup\$

Maybe this will help

if (ft.text == "Woven Acrylic" && at.text == "Motorized"){
    switch (asf.text) {
        case "10":
            bap.text = String(Number(1147))
            break ;
        case "11":
            bap.text = String(Number(1217))
            break ;
        ...
    }
    tc_v.text = '$' + String(Number(bap.text) + Number(ac_.text))
}
\$\endgroup\$
  • \$\begingroup\$ I think this is still not better, it is just the same, but formatted in a different way. You still need lots of conditions in this case. \$\endgroup\$ – Mark Knol May 24 '12 at 11:01
  • \$\begingroup\$ @MarkKnol I think this is an acceptable answer to the OP's question. It's not great since it only removes some of the extraneous conditionals, but it does help the OP see other ways to write logic. Based on the OP's code, he's seems very new to programming. He needs practice simplifying if-blocks. I think he has a while before he'll be able to appreciate the data driven answers people are giving. \$\endgroup\$ – Walter Stabosz Jul 10 '12 at 14:18
4
\$\begingroup\$

I would create an XML or other file to look into, instead of writing all those content related conditions yourself. I think its better to separate content and logic in this case.

var xml:XML = 
    <items>
      <item>
       <ft>Woven Acrylic</ft>
       <at>Motorized Pro XL</at>
       <asf>20</asf>
       <bap>1509</bap>
     </item>
      <item>
       <ft>Traditonal Laminated</ft>
       <at>Motorized</at>
       <asf>10</asf>
       <bap>1509</bap>
     </item>
      <item>
       <ft>Woven Acrylic</ft>
       <at>Motorized Pro XL</at>
       <asf>10</asf>
       <bap>1047</bap>
     </item>
     <item>
       <ft>a</ft>
       <at>b</at>
       <asf>c</asf>
       <bap>1047</bap>
     </item>
    </items>;
// etc..

Then I would find a node matching all content using a E4X expression. Basically there is one condition; just match 3 fields in the XML.

// convert to variables 
var ftText:String = ft.text;
var atText:String = at.text;
var asfText:String = asf.text;

// filter/find nodes
var resultNodes:XMLList = xml.item.(ft == ftText && at == atText && asf == asfText);
if (resultNodes.length() == 1)
{

   bap.text = resultNodes.bap.text();
   trace("Match found: " + resultNodes.toXMLString());
}
else if (resultNodes.length() > 1)
{
    trace("Multiple matches found: " + resultNodes.toXMLString());
}
else
{
  trace("No matching node found");
}

In lot of cases it would be better to load the XML external, but for the example this will work too.

Side note: you should refine the variable names.

More on E4X:
http://joshblog.net/2007/05/08/methods-to-filter-data-with-e4x-in-flash-9/

\$\endgroup\$
  • \$\begingroup\$ spend few minutes to write an answer like yours but i wasn't fast enough : ( \$\endgroup\$ – OXMO456 May 24 '12 at 8:17
2
\$\begingroup\$

Put your numbers in an array. Then your code could look like:

if (ft.text == "Woven Acrylic" && at.text == "Motorized"){
    bap.text = String(array[Number(asf.text)])
    tc_v.text = '$' + String(array[Number(asf.text)]) + Number(ac_.text))
}
\$\endgroup\$
2
\$\begingroup\$

You need to store data properly to be able to use it efficiently. What you're doing there is storing all the data within an endless amount of if() statements.

I would suggest storing everything as XML, but not knowing your experience level I'll avoid that solution.

Something purely ActionScript that you can do is store your data in objects which are listed in an array, example:

// Define the array containing all data.
var dataArray:Array = [];

// Add some objects to the array.
dataArray.push({ ft: "Woven Acrylic", at: "Motorized", asf: "14", bap: "1047", ac: "more data" });
dataArray.push({ ft: "Traditonal Laminated", at: "Motorized", asf: "15", bap: "1307", ac: "more data" });

Now you can create a function that finds and uses objects with relevant data:

function find(ft:String, at:String, asf:String):Object
{
    for each(var i:Object in dataArray)
    {
        if(i.ft == ft && i.at == at && i.asf == asf)
        {
            return i;
        }
    }

    return null;
}

And another function that will set the text fields like you're doing:

function setText(basedOn:Object):void
{
    bap.text = basedOn.bap;
    tc_v.text = '$' + bap.text + basedOn.ac;
}

Now you can simply do this for example:

setText(find("Traditonal Laminated", "Motorized", "15"));
\$\endgroup\$
  • \$\begingroup\$ +1 Yeah, this works, too. But I still think this is too complicated. Check my answer - it can be done with a simple two-dimensional array, if you concatenate the strings. \$\endgroup\$ – weltraumpirat May 24 '12 at 8:48
  • \$\begingroup\$ Side note: This could give a null reference error if no text is found, so an extra condition would be nice. \$\endgroup\$ – Mark Knol May 24 '12 at 10:52
2
\$\begingroup\$

Since your if-else statements follow a simple logic (ALL the strings are expected EQUAL to something you know), you can simply concatenate the values and use them as keys in a lookup table (for convenience, I created one for you, but this can and should be done at runtime):

    private var items:Object = {"Woven AcrylicMotorized10": 1147, "Woven AcrylicMotorized11": 1217, "Woven AcrylicMotorized12": 1297, "Woven AcrylicMotorized13": 1343, "Woven AcrylicMotorized14": 1407, "Woven AcrylicMotorized15": 1467, "Woven AcrylicMotorized16": 1530, "Woven AcrylicMotorized17": 1587, "Woven AcrylicMotorized18": 1632, "Woven AcrylicMotorized19": 1709, "Woven AcrylicMotorized20": 1754, "Woven AcrylicMotorized XL14": 1490, "Woven AcrylicMotorized XL15": 1560, "Woven AcrylicMotorized XL16": 1636, "Woven AcrylicMotorized XL17": 1703, "Woven AcrylicMotorized XL18": 1758, "Woven AcrylicMotorized XL19": 1848, "Woven AcrylicMotorized XL20": 1903, "Woven AcrylicMotorized Pro10": 1247, "Woven AcrylicMotorized Pro11": 1317, "Woven AcrylicMotorized Pro12": 1397, "Woven AcrylicMotorized Pro13": 1443, "Woven AcrylicMotorized Pro14": 1507, "Woven AcrylicMotorized Pro15": 1567, "Woven AcrylicMotorized Pro16": 1603, "Woven AcrylicMotorized Pro17": 1687, "Woven AcrylicMotorized Pro18": 1732, "Woven AcrylicMotorized Pro19": 1823, "Woven AcrylicMotorized Pro20": 1878, "Woven AcrylicMotorized Pro XL14": 1509, "Woven AcrylicMotorized Pro XL15": 1660, "Woven AcrylicMotorized Pro XL16": 1736, "Woven AcrylicMotorized Pro XL17": 1803, "Woven AcrylicMotorized Pro XL18": 1858, "Woven AcrylicMotorized Pro XL19": 1962, "Woven AcrylicMotorized Pro XL20": 2027, "Woven AcrylicOasis10 x 16": 1305, "Woven AcrylicVista10": 927, "Woven AcrylicVista11": 997, "Woven AcrylicVista12": 1077, "Woven AcrylicVista13": 1123, "Woven AcrylicVista14": 1187, "Woven AcrylicVista15": 1247, "Woven AcrylicVista16": 1310, "Woven AcrylicVista17": 1367, "Woven AcrylicVista18": 1412, "Traditonal LaminatedMotorized10": 1047, "Traditonal LaminatedMotorized11": 1107, "Traditonal LaminatedMotorized12": 1167, "Traditonal LaminatedMotorized13": 1213, "Traditonal LaminatedMotorized14": 1257, "Traditonal LaminatedMotorized15": 1307, "Traditonal LaminatedMotorized16": 1360, "Traditonal LaminatedMotorized17": 1407, "Traditonal LaminatedMotorized18": 1452, "Traditonal LaminatedMotorized Pro10": 1147, "Traditonal LaminatedMotorized Pro11": 1207, "Traditonal LaminatedMotorized Pro12": 1267, "Traditonal LaminatedMotorized Pro13": 1313, "Traditonal LaminatedMotorized Pro14": 1357, "Traditonal LaminatedMotorized Pro15": 1407, "Traditonal LaminatedMotorized Pro16": 1460, "Traditonal LaminatedMotorized Pro17": 1507, "Traditonal LaminatedMotorized Pro18": 1552, "Traditonal LaminatedVista10": 827, "Traditonal LaminatedVista11": 887, "Traditonal LaminatedVista12": 947, "Traditonal LaminatedVista13": 993, "Traditonal LaminatedVista14": 1037, "Traditonal LaminatedVista15": 1087, "Traditonal LaminatedVista16": 1140, "Traditonal LaminatedVista17": 1187, "Traditonal LaminatedVista18": 1232};

    public function getUniqueId( ft:String, at:String, asf:String ):String {
        var searchString:String = ft+""+at+""+asf;
        return items.hasOwnProperty(searchString)? items[searchString] : "0";
    }  

Just apply like this:

tc_v.text = '$' + getUniqueId( ft.text, at.text, asf.text) + Number(ac_.text))    
\$\endgroup\$
  • \$\begingroup\$ I agree this is very concise, but I don't think it teaches good coding practice. \$\endgroup\$ – RIAstar May 24 '12 at 10:57
  • \$\begingroup\$ What do you consider good coding practice? It works, it's very fast and short, and it doesn't require any additional class compilation. You should not overdo abstractions, unless they are useful - in this case, the problem is simple enough to just work with primitive values. \$\endgroup\$ – weltraumpirat May 24 '12 at 11:01
  • \$\begingroup\$ I agree with you, though, that the variables names should speak for themselves - I only used OP's names to show how the solution relates to the original code. \$\endgroup\$ – weltraumpirat May 24 '12 at 11:02
  • \$\begingroup\$ To make a point: en.wikipedia.org/wiki/YAGNI \$\endgroup\$ – weltraumpirat May 24 '12 at 11:04
  • \$\begingroup\$ I understand your YAGNI point. Whether you're going to need it depends on the requirements of the application though. If we limit ourselves to just this example it is indeed a good solution. However if we imagine this is going to be part of a shopping application (which we could assume, because you wouldn't use Flex to make a simple form), then you are going to need the abstraction. \$\endgroup\$ – RIAstar May 24 '12 at 11:16
1
\$\begingroup\$

This should be done with a function that gets values for "ft", "at" and "asf" based on each consecutive selections starting from "ft". If your code explained the logic behind the numbers "1217", "1418" being assigned to "bap.text", it would be way much better than what I'm writing below:

public function generateText(ft:String, at:String, asf:String):void
{
    var concatenated:String = ft +"_" + at + "_" + asf;
    switch(concatenated)
    {
        case "Woven Acrylic_Motorized_10":
            bap.text = String(1147);
            tc_v.text = '$' + bap.text + parseFloat(ac_.text);
            break;
        case "Woven Acrylic_Motorized_11":
            bap.text = String(1217);
            tc_v.text = '$' + bap.text + parseFloat(ac_.text);
            break;
        default:
            bap.text = 0;
            break; 

    }
}

You also do not need to cast numbers into strings to assign them to a textfield. You can remove much of the explicit casting you're doing in your code.

\$\endgroup\$
  • 1
    \$\begingroup\$ You do need to cast numbers to strings before assigning them to TextField.text or you'll get an implicit coercion error - luckily appending a string to a number does this for you. Your default: will break though. \$\endgroup\$ – Marty May 24 '12 at 7:34
  • \$\begingroup\$ I think this isn't better, i don't see why you merge all text into one. What if an extra parameter is required? What if you miss and underscore in the case (like you already did). It's less readable and not maintainable on long term and with lots of data. \$\endgroup\$ – Mark Knol May 24 '12 at 11:04
  • \$\begingroup\$ The approach requires concatenating strings to build associative keys. I didn't say the switch statement is good, there's no need for that. Interestingly weltraumpirat down below this page has given a better solution to what I tried to envision here. \$\endgroup\$ – mhaqs May 24 '12 at 12:04
1
\$\begingroup\$

Store your data in a XML, then query for a result.

var data:XML = <data>
            <item attr="Woven Acrylic">
                <item attr="Motorized">
                    <item attr="10" value="1147" />
                    <item attr="11" value="1148" />
                    <item attr="12" value="1149" />
                </item>
                <item attr="Motorized XL">
                    <item attr="16" value="1140"/>
                    <item attr="17" value="1141"/>
                    <item attr="18" value="1142"/>
                </item>
                </item>
                <item attr="Traditonal Laminated">
                    <item attr="Motorized">
                    <item attr="17" value="1143"/>
                    <item attr="18" value="1144"/>
                    <item attr="19" value="1145"/>
                </item>
            </item>
        </data>;


function extractValue(a:String, b:String, c:String):String {
    var queryRes:XMLList = data.item.(@attr == a).item.(@attr == b).item.(@attr == c).@value;
    if (queryRes.length() == 1) {
        return queryRes.toString();
    }
    return null;
}

trace(extractValue("Woven Acrylic", "Motorized", "10")); //1147
trace(extractValue("Woven Acrylic", "", "10")); //null
trace(extractValue("Woven Acrylic", "Motorized XL", "18")); //1142
\$\endgroup\$
1
\$\begingroup\$

You can use if-else or switch-case statement but switch case method is faster than if-else. But if-else which i would specify is faster than the code u have written here as it optimizes the code like

if(ft.text == "Woven Acrylic") {
    if(at.text == "Motorized") {
         if(asf.text == "10") {
             // Statement
         }
         // More If Statements
    }
    // More If Statements each containing if statements
} else if(ft.text == "Laminated") {
    // More If Statements each containing if statements that contain if statements
}
// More If Statements Et Cetera

second method taking switch for woven acrylic and laminated and invoke a function which includes another switch case that will be faster

\$\endgroup\$
  • \$\begingroup\$ dude, please format your code. This is painful to look at. stackoverflow.com/editing-help \$\endgroup\$ – weltraumpirat May 24 '12 at 8:16
  • \$\begingroup\$ I think this is still not better, it is just the same, but formatted in a different way. You still need lots of conditions in this case. \$\endgroup\$ – Mark Knol May 24 '12 at 11:05
0
\$\begingroup\$

Might be this may help you: -

As you know all values you can do it by other way as below: - as all values are static: - or some thing like below....

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;

            //ft.text == "Woven Acrylic" && at.text == "Motorized" && asf.text == "10"
            //ft.text == "Traditonal Laminated" && at.text == "Motorized" && asf.text == "13"
            private var oneOf6:ArrayCollection = new ArrayCollection
                (
                    [{ftValue:"Woven Acrylic",atValue:"Motorized",asfValue:"10", bapValue:1147},
                    {ftValue:"Woven Acrylic",atValue:"Motorized",asfValue:"11", bapValue:1217},
                    {ftValue:"Woven Acrylic",atValue:"Motorized",asfValue:"12", bapValue:1297},
                    {ftValue:"Woven Acrylic",atValue:"Motorized",asfValue:"13", bapValue:1343},
                    {ftValue:"Woven Acrylic",atValue:"Motorized",asfValue:"14", bapValue:1407},
                    {ftValue:"Woven Acrylic",atValue:"Motorized",asfValue:"15", bapValue:1467},
                    {ftValue:"Traditonal Laminated",atValue:"Motorized",asfValue:"13", bapValue:1213}
                    .
                    .

                ]);

            private function getValueDisplay():void
            {
                for(var i:int; i<oneOf6.length;i++)
                {
                    if(oneOf6[i].ftValue == ft.text && oneOf6[i].atValue == at.text && oneOf6[i].asfValue == asf.text)
                    {
                        tc_v.text = '$' +oneOf6[i].bapValue + Number(ac_.text))
                        break;
                    }
                }
            }

        ]]>
    </fx:Script>
    <mx:VBox>
        <s:TextInput id="ft" text="Woven Acrylic"/>
        <s:TextInput id="at" text="Motorized"/>
        <s:TextInput id="asf" text="10"/>
        <s:TextInput id="tc_v"/>
        <s:TextInput id="bap"/>
        <s:TextInput id="ac_"/>
        <s:Button label="click" click="getValueDisplay()"/>
    </mx:VBox>

</s:Application>
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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