0

I am new to Java (and asking questions on this site.)and am trying to change the itemList variable from inputs.Items.itemlist1 every time the for loop runs from itemlist1 to itemList2 then itemList3. Here is my code:

items.java

public class Items {

public static Object[][] itemList1 = new Object[][]{
    {"book", 12.49f, "BOOK"},
    {"Music CD", 14.99f, "OTHERS"},
    {"chocolate bar", 0.85f, "FOOD"},};
public static Object[][] itemList2 = new Object[][]{
    {"imported box of chocolates", 10.00f, "IMPORTED_FOOD"},
    {"imported bottle of perfume", 47.50f, "IMPORTED_OTHER"},};
public static Object[][] itemList3 = new Object[][]{
    {"imported bottle of perfume", 27.99f, "IMPORTED_OTHER"},
    {"bottle of perfume", 18.99f, "OTHERS"},
    {"packet of headache pills", 9.75f, "MEDICAL"},
    {"box of imported chocolates", 11.25f, "IMPORTED_FOOD"}
};

public static Object[] possibleListLength = new Object[]{itemList1.length, itemList2.length, itemList3.length};
public static Object[] possibleList = new Object[][]{itemList1, itemList2, itemList3};

}

DetermineItem.java

package tax;
import java.util.Arrays;
import java.util.ArrayList;
public class DetermineItem {
    public static String itemPriceString = "";
    public static void main(String[] args) {
        //System.out.println(Arrays.deepToString(inputs.Items.itemList1));
        Object arrayLength = inputs.Items.possibleList.length;
        int arrayLengthInt = (Integer) arrayLength + 1;
        for (int j = 1; j < 4; j++) {
            System.out.println("Output " + j + ":");
            for (int i = 0; i < arrayLengthInt; i++) {
                Object[][] itemList = inputs.Items.itemList1;
                Object itemTypeObject = itemList[i][2];
                String itemTypeString = itemTypeObject.toString();
                if (itemTypeString.equals("BOOK") || itemTypeString.equals("FOOD") || itemTypeString.equals("MEDICAL")) {
                    System.out.println("1 " + itemList[i][0] + ": " + itemList[i][1]);
                } else if (itemTypeString.equals("IMPORTED_FOOD") || itemTypeString.equals("IMPORTED_OTHER")) {
                    Object itemPriceObject = itemList[i][1];
                    itemPriceString = itemPriceObject.toString();
                    CalculateImportTax.calculateImportTax();
                    System.out.println("1 " + itemList[i][0] + ": " + CalculateImportTax.price);
                } else {
                    Object itemPriceObject = itemList[i][1];
                    itemPriceString = itemPriceObject.toString();
                    CalculateSalesTax.calculateTax();
                    System.out.println("1 " + itemList[i][0] + ": " + CalculateSalesTax.price);
                }
            }
        }
    }
}

Thanks.

8
  • 2
    What is stopping you? What and where is your problem? Commented Dec 18, 2018 at 19:07
  • I am not sure how to make it automatically change names on every pass through the loop. Commented Dec 18, 2018 at 19:10
  • 2
    you can't change the names of class members dynamically Commented Dec 18, 2018 at 19:11
  • what would you do in this circumstance? I need to be able to hold information of an object, like its cost and name, and grab that information Commented Dec 18, 2018 at 19:12
  • If you really need to be able to change the names of things dynamically you can use the HashMap class. docs.oracle.com/javase/8/docs/api/java/util/HashMap.html Commented Dec 18, 2018 at 19:13

1 Answer 1

1

This is what methods are for. In outline, and with poorly-chosen method naming (!),

    private void doOneList(Object[][] itemList) {
        for (int j=1; j<4; j++) {
           ... do stuff with itemList ...
        }
     }

and then in main

    doOneList(itemList1);
    doOneList(itemList2);
    doOneList(itemList3);

I underscore my comment that this is not an off-the-shelf solution, it's just a sketch of how to proceed.

For what it's worth, I'd define a more specific type than arrays of 'Object'. Something like:

 static class Item {
     String description;
     Double tax;
     String type;
 }

and then use a single-dimensional Item[]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.