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.
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[]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.