Reputation: 71
Can someone tell me why I can't get the highest value and minimum of value of multiplying both the price and the quantity of these items?
public class StoreProgram {
public static void main(String[] args) {
String[] storeItems = {
"broccoli", "onion", "carrot", "turnip", "mango",
"bread", "garlic", "celery", "apple", "banana",
"raisins", "grapes", "lemon", "orange", "potato"};
int[] itemQuantities = {
23, 5, 7, 15, 2,
13, 13, 8, 20, 30,
3, 25, 10, 9, 1};
double[] itemPrices = {
2.0, 0.89, 0.70, 1.50, 2.99,
3.45, 1.45, 1.12, 3.99, 0.25,
4.99, 7.00, 1.75, 1.80, 3.25};
double max = itemQuantities[0] * itemPrices[0];
double min = itemQuantities[0] * itemPrices[0];
for (int i = 1; i < storeItems.length; i++) {
if (max > itemQuantities[i] * itemPrices[i]) {
max = itemQuantities[i] * itemPrices[i];
System.out.println("HIGHEST:\n\tItem: " + storeItems[i]
+ ",\t" + "Inventory Value: $" + max);
}
if (min < itemQuantities[i] * itemPrices[i]) {
min = itemQuantities[i] * itemPrices[i];
System.out.println("Lowest:\n\tItem: " + storeItems[i]
+ ",\t" + "Inventory Value: $" + min);
}
}
}
}
It prints out the following:
HIGHEST: Item: onion, Inventory Value: $4.45
Lowest: Item: apple, Inventory Value: $79.80000000000001
Lowest: Item: grapes, Inventory Value: $175.0
HIGHEST: Item: potato, Inventory Value: $3.25
Upvotes: 0
Views: 187
Reputation: 549
Since you have not provided an expected output I'll just state the logical error and my quick fix.
if (max > itemQuantities[i] * itemPrices[i]) {
max = itemQuantities[i] * itemPrices[i];
System.out.println("HIGHEST:\n\tItem: " + storeItems[i]
+ ",\t" + "Inventory Value: $" + max);
}
is wrong since you are comparing the possible highest value and then assign it to the maximum value if the maximum value is higher which doesn't make sense the same logical error goes to min.
Secondly, you are printing out the value every time you change. Since you are asking for the highest and the lowest values there should only be one for each.
This is what i do
public class StoreProgram {
public static void main(String[] args) {
String[] storeItems = {
"broccoli", "onion", "carrot", "turnip", "mango",
"bread", "garlic", "celery", "apple", "banana",
"raisins", "grapes", "lemon", "orange", "potato"};
int[] itemQuantities = {
23, 5, 7, 15, 2,
13, 13, 8, 20, 30,
3, 25, 10, 9, 1};
double[] itemPrices = {
2.0, 0.89, 0.70, 1.50, 2.99,
3.45, 1.45, 1.12, 3.99, 0.25,
4.99, 7.00, 1.75, 1.80, 3.25};
double max = itemQuantities[0] * itemPrices[0];
double min = itemQuantities[0] * itemPrices[0];
int highindex, lowindex;
highindex = lowindex = 0;
for (int i = 1; i < storeItems.length; i++) {
if (max < itemQuantities[i] * itemPrices[i]) {
max = itemQuantities[i] * itemPrices[i];
highindex = i;
}
if (min > itemQuantities[i] * itemPrices[i]) {
min = itemQuantities[i] * itemPrices[i];
lowindex = i;
}
}
System.out.println("HIGHEST:\n\tItem: " + storeItems[highindex]
+ ",\t" + "Inventory Value: $" + max);
System.out.println("Lowest:\n\tItem: " + storeItems[lowindex]
+ ",\t" + "Inventory Value: $" + min);
}
}
Which outputs the following
HIGHEST:
Item: grapes, Inventory Value: $175.0
Lowest:
Item: potato, Inventory Value: $3.25
Upvotes: 1