I have the following code that I am working on for my college assignment. I am asked to use arrays, which I am using. I am asked to use for-loops and if-statement that I am doing already. I have come up with the following code:

class HardwareStore2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.printf("%55s", "**WELCOME TO THE HARDWARE STORE**\n");
        System.out.printf("%55s", "=================================\n");

        String [] codes = {"G22", "K13", "S21", "I30"};
        String [] description = {"STICKY Construction Glue", "CAR-LO Key Ring", "SCREW-DUP Screwy Screws", "LET-IT-RAIN Padlock"};
        List<String> codeList = Arrays.asList(codes);
        String output = "";
        int i = 1000;
        String [] userCode = new String[i];
        char dolSymb = '$';
        int [] pricesAndTax = {10989, 5655, 1099, 4005, 20};
        int [] weight = {};
        int [] userQuantity = {1};
        int [] userPrice = new int[i];
        int userStickyPrice = 0;
        int userKeyringPrice = 0;
        int userScrewyPrice = 0;
        int userPadlockPrice = 0;
        int userPreWithTax = 0;
        int userTotal = 0;
        int userPreTotal = 0;
        int userShipping = 0;

        System.out.printf("%-10s%-40s%-15s%-10s\n", "CODE", "DESCRIPTION", "WEIGHT", "PRICE\n");
        System.out.printf("%-10s%-55s%s%d.%02d\n", codes[0], description[0], dolSymb, pricesAndTax[0]/100, pricesAndTax[0]%100);
        System.out.printf("%-10s%-55s%s%d.%02d\n", codes[1], description[1], dolSymb, pricesAndTax[1]/100, pricesAndTax[1]%100);
        System.out.printf("%-10s%-55s%s%d.%02d\n", codes[2], description[2], dolSymb, pricesAndTax[2]/100, pricesAndTax[2]%100);
        System.out.printf("%-10s%-55s%s%d.%02d\n", codes[3], description[3], dolSymb, pricesAndTax[3]/100, pricesAndTax[3]%100);

        System.out.println("PLEASE ENTER YOUR ORDER:");
        System.out.print("NAME: ");
        String username = in.nextLine();
        System.out.print("ADDRESS Line 1: ");
        String address1 = in.nextLine();
        System.out.print("ADDRESS Line 2: ");
        String address2 = in.nextLine();
        System.out.print("POSTAL CODE: ");
        String postalcode = in.nextLine();

        for (i = 0;; i++) {
            System.out.print("CODE (X to QUIT):");
            userCode[i] = in.nextLine();

            if (userCode[i].equalsIgnoreCase("x")) {
                break;
            }

            System.out.print("QUANTITY: ");
            userQuantity[i] = in.nextInt();
            in.nextLine();

            if (userCode[i].equalsIgnoreCase(codes[0])) {
                userStickyPrice += userQuantity[i]*pricesAndTax[0];

            }
            else if (userCode[i].equalsIgnoreCase(codes[1])) {
                userKeyringPrice += userQuantity[i]*pricesAndTax[1];

            }
            else if (userCode[i].equalsIgnoreCase(codes[2])) {
                userScrewyPrice += userQuantity[i]*pricesAndTax[2];

            }
            else if (userCode[i].equalsIgnoreCase(codes[3])) {
                userPadlockPrice += userQuantity[i]*pricesAndTax[3];

            }
            else if (!codeList.contains(userCode)) {
                i = i - 1;
            }
        }
     }
}

Now, everything is working seamlessly with a million ifs, and elses but I want to know if there is a way to replace all the if-else-if statements wirht one or two that do something like this:

if (userCode[i].contains(codes[0], codes[1], codes[2], codes[3] {}

Or maybe something better like:

if (userCode.contains(any.one.item.in.codeList) {
    then.get.the.price.of.that.item.and.do.item.specific.operations
    }

Please let me know if the question is not clear enough. And once again, this is a college assignment so I would appreciate explanations.

share|improve this question
I have upvoted all the answers because they have all been helpful. I am accepting @dasblinkenlights 's answer because Hashmaps are something I haven't been taught about in my college and I would like to see if I can work with them. If I am unsuccessful with Hashmaps, then I will use Switch statements for now or put up a new question. Thanks everyone. – nickecarlo Jan 23 '12 at 2:45
feedback

4 Answers

up vote 4 down vote accepted

Without changing the rest of your data structures for something more efficient (say, a Map) you could achieve the same effect with a single if and a nested loop:

boolean found = false;
for (int j = 0 ; !found && j != codes.length ; j++) {
    if (userCode[i].equalsIgnoreCase(codes[j])) {
        userScrewyPrice += userQuantity[i]*pricesAndTax[j];
        found = true;
    }
}
if (!found) {
    i--;
}
share|improve this answer
If "Map"s are better then I do have both the time and energy to look in that direction. Since I am a newbie with Java, do you mean Hashmaps or am I completely off here? – nickecarlo Jan 23 '12 at 2:13
1  
@nickecarlo Hashmap is correct, just make sure that you use a generic version Hashmap<K,T> (it gives you a better type safety). You will define a local variable, like this: Map<String,Integer> codeToPriceAndTax = new HashMap<String,Integer>; You will then add code/price+tax pairs to that map during initialization. Inside your main loop, you will use int priceAndTax = codeToPriceAndTax.get(userCode[i]) to loop up the price+tax by their code. – dasblinkenlight Jan 23 '12 at 2:19
I will check out Hashmaps, thanks. – nickecarlo Jan 23 '12 at 2:45
feedback

The variable codeList is a List and it has a contains function that could be helpful if I understood what you're trying to do.

Also, since Java 7 you can use strings as an argument of switch statements and that would make your code look nicer.

share|improve this answer
feedback

I didn't read the full question so this may only be a part answer... but you ask if you could use:

if (userCode[i].contains(codes[0], codes[1], codes[2], codes[3] {}

You may be able to use something like...

if(new string[]{"a","b","c"}.Contains("a")) {}

or to put this into custom array types...

arrayType[] a = new arrayType[]{item1, item2, item3}
if (arrayType.Contains(searchItem)) {}

Basically - you can do what you asked, just need to change the order of the syntax. But this, I'm sure, is only a part answer to get you thinking.

share|improve this answer
feedback

Anytime that you have multiple "if" statements you should also look at using a "switch" statement instead.

Looks like a switch statement would save quite a bit of lines here.

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

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