In: Computer Science
Required Names:
StoreProgram
This project will be a program that provides information about inventory in a grocery store. Use this codeas your starting point. This code already has a main method written for you. DO NOT modify the main method. If you modify the main method, you will lose points.
In the main method, you will notice three arrays:
Here is the code as seen in the file:
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 };
Each array provides information about a specific item by index. This means that each index is specific for the item in the store. For example, notice that "carrot" is at index 2 in the storeItems array. This means that the quantity of carrots is also located at index 2in the itemQuantities array (which is 7), and the price of carrots is located at index 2 in the itemPrices array (which is 0.70).
For the purpose of this assignment, you can assume that the three arrays are always of equal length. This means that if you refer to the length property on any of them (for example, itemPrices.length), then the length will be the same for the other two arrays as well.
Instructions
Your program will need to have:
To help runTransaction run properly without making it too bulky, you will be required to implement the following methods as well. Each of these methods should have three parameters (the three arrays), and they should not return any values.
Pleases if I can get help today with how to do these steps of coding ?
Ok, so the required code is attached below.
First, read the code and run it. and if you get any doubt feel free to drop that in the comment section.
And if you think anything is missing, do let me know.
Code:
import java.util.*;
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
};
runTransaction(storeItems,
itemQuantities, itemPrices);
}
private static void runTransaction(String[]
storeItems, int[] itemQuantities, double[] itemPrices) {
Scanner input=new
Scanner(System.in);
while(true) {
System.out.println("\nChoose option:\n\n"
+ "1. Display
Inventory\n"
+ "2. Display Low
Inventory\n"
+ "3. Display Highest
Lowest\n"
+ "4. Display Total
Inventory\n"
+ "5. Exit");
int
o=input.nextInt();
if(o==1) {
displayInventory(storeItems,itemQuantities,
itemPrices);
}
else if(o==2)
{
displayLowInventory(storeItems,itemQuantities,
itemPrices);
}
else if(o==3)
{
displayHighestLowest(storeItems,itemQuantities,
itemPrices);
}
else if(o==4)
{
displayTotalInventory(storeItems,itemQuantities,
itemPrices);
}
else if(o==5)
{
break;
}
else {
System.out.println("Invalid Input");
}
}
}
private static void displayTotalInventory(String[]
storeItems, int[] itemQuantities, double[] itemPrices) {
double sum=0;
for(int
i=0;i<storeItems.length;i++) {
sum=sum+(itemPrices[i] * itemQuantities[i]);
}
System.out.println(String.format("%.2f",sum));
}
private static void displayHighestLowest(String[]
storeItems, int[] itemQuantities, double[] itemPrices) {
String high=storeItems[0];
String low=storeItems[0];
double h_value=itemPrices[0] *
itemQuantities[0];
double l_value=itemPrices[0] *
itemQuantities[0];
for(int
i=0;i<storeItems.length;i++) {
double inventory
= itemPrices[i] * itemQuantities[i];
if(inventory
>h_value) {
h_value=inventory;
high=storeItems[i];
}
if(inventory
< l_value) {
l_value=inventory;
low=storeItems[i];
}
}
System.out.println("Highest
Inventory\n"+high+"\t"+h_value+"\n");
System.out.println("Lowest
Inventory\n"+low+"\t"+l_value+"\n");
}
private static void displayLowInventory(String[]
storeItems, int[] itemQuantities, double[] itemPrices) {
System.out.println();
for(int
i=9;i<storeItems.length;i++) {
if(itemQuantities[i]<5) {
System.out.println(storeItems[i]);
}
}
}
private static void displayInventory(String[]
storeItems, int[] itemQuantities, double[] itemPrices) {
System.out.println();
for(int
i=9;i<storeItems.length;i++) {
System.out.println(storeItems[i]);
}
}