Question

In: Computer Science

Required Names: Main class: StoreProgram Java filename: StoreProgram.java StoreProgram This project will be a program that...

Required Names:

  • Main class: StoreProgram
  • Java filename: StoreProgram.java

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:

  • A String array representing the items in the store by name.
  • An integer array representing the quantities of each item.
  • A double array representing the price of each item.

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:

  • runTransaction -> This method is kind of like the "main method" for everything related to the store manager program. The program starts and ends with this method, so the entire flow of the program should be managed here.
    The user should be given the following five options to choose from:

    When each option is completed and executed, the user should be able to choose from the option again, with the exception of Option 5 (Exit) which will cause the program to end.

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.

  • displayInventory (Option 1)-> This method will display the entire inventory. For each item, display the item on a single line like we did in the video.
  • displayLowInventory (Option 2)-> This method will only display inventory if the quantity is less than 5.
  • displayHighestLowest (Option 3)-> This method will display the items with the highest inventory value and the lowest inventory value. The inventory value is the quantity multiplied by the price, which is basically what you would pay if you were to buy the entire stock of the item.
    When displaying the items, you only need to show the item's name and the inventory value of the item. Do not display any information about any other item. Only display the information about the item with the highest inventory value and the item with the lowest inventory value.
  • displayTotalInventory (Option 4)-> This method will calculate the inventory value for each item and display the sum total of all inventory values.

Pleases if I can get help today with how to do these steps of  coding ?

Solutions

Expert Solution

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]);
       }
      
   }


Related Solutions

Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list...
Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list of integers from use input and has the following methods: 1. min - Finds the smallest integer in the list. 2. max- Finds the largest integer in the list. 3. average - Computes the average of all the numbers. 4. main - method prompts the user for the inputs and ends when the user enter Q or q and then neatly outputs the entire...
Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list...
Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list of integers from use input and has the following methods: 1. min - Finds the smallest integer in the list. 2. max- Finds the largest integer in the list. 3. average - Computes the average of all the numbers. 4. main - method prompts the user for the inputs and ends when the user enter Q or q and then neatly outputs the entire...
Required components: A controller class with Java main() method and a PaymentCalculator class to hold the...
Required components: A controller class with Java main() method and a PaymentCalculator class to hold the data and methods for the application. Scenario/Information: If you carry a balance on a credit card, it can be nice to see how long it would take to payoff the card. For this scenario, you should design a Java application the prints a credit card payment schedule. Inputs for your program should include: Customer’s name (first and last), the account number (as an integer),...
Create a Java program. The class name for the program should be 'EncryptText'. In the main...
Create a Java program. The class name for the program should be 'EncryptText'. In the main method you should perform the following: You should read a string from the keyboard using the Scanner class object. You should then encrypt the text by reading each character from the string and adding 1 to the character resulting in a shift of the letter entered. You should output the string entered and the resulting encrypted string. Pseudo flowchart for additional code to be...
Java program. Need to create a class names gradesgraph which will represent the GradesGraphtest program. Assignment...
Java program. Need to create a class names gradesgraph which will represent the GradesGraphtest program. Assignment Create a class GradesGraph that represents a grade distribution for a given course. Write methods to perform the following tasks: • Set the number of each of the letter grades A, B, C, D, and F. • Read the number of each of the letter grades A, B, C, D, and F. • Return the total number of grades. • Return the percentage of...
: Create a Java program that will accept a regular expression and a filename for a...
: Create a Java program that will accept a regular expression and a filename for a text file. The program will process the file, looking at every line to find matches for the regular expression and display them. Regular Expression Format : The following operators need to be accepted: + - one or more of the following character (no groups) * - zero or more of the following character (no groups) [] – no negation, no character spans – the...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
Part A Java netbeans ☑ Create a project and in it a class with a main....
Part A Java netbeans ☑ Create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class , after the package statement, paste import java.util.Scanner; As the first line inside your main method, paste Scanner scan = new Scanner(System.in); Remember when you are getting a value from the user, first print a request, then use the Scanner to read the right type...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class,...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. -Add a method addToCollection. In this method, add one to collected...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT