In: Computer Science
The goal of Java program implemented as part of this assignment
is to build an
Inventory Management System, from here on referred to as IMS, for a
bookstore.
You will need to do the following tasks:
Requirement-1 – Create 3 different Arrays (15 pts)
(a) Create an array named items that will hold the following item
names (strings)
- Pen
- Pencil
- Eraser
- Marker
- Notepad
(b) Create a second array named quantity, that will hold
corresponding quantities
for items in the items array above. This will be populated through
user inputs.
For each input you get from the user, be sure to check it is a
number > 0. If
not, continue to ask the user for the right number.
Hint: User the handy-dandy Scanner to read user input, the if-else
construct to
validate user input and the loop to repetitively ask for user input
should you
get an unacceptable value
(c) Create a third array named price, that will hold price-per-item
for the items
stored in the first array. The price point for each item should
also come from
the user. For example, a notepad could be priced at $1.47, eraser
at $0.76 etc.
As in the above case, for each price point input you get from the
user, be sure
to check it is a number > 0. If not, continue to ask the user
for the right
number for the item price.
The message, for example, could look like “Please enter price for a
Notebook”
(d) Based on the type of data that needs to be stored in the
above-mentioned
arrays, be sure to use the correct data type
Requirement-2 –Create Operations for an Inventory Manager
role
(a) Show Functions List
Once the above data points have been correctly created, you will
now need to
build the functional points (operations) for this IMS. But before
we do that, we
need tell the user about operations that are available to be
invoked by the
user, as part of this IMS. The 5 operations of the IMS are as
follows. So, display
these operations to the user and ask the user to enter the letter
for the
operation that the user wants to run.
- Print Inventory (p)
- Check for Low Inventory (c)
- Highest/Lowest value inventory Items (h)
- Total Inventory Value (t)
- Exit System (e)
Sample output should look something like the following:
Welcome to IMS. Please select a function from the following:
Print Inventory (p)
Check for Low Inventory (c)
Highest/Lowest value inventory Items (h)
Total Inventory Value (t)
Exit System (e)
(b) PrintInventory (15 pts)
This operation should print inventory, starting with the header, in
the
following format
Item_Name Quantity Price Total_Inventory_Value
Eraser 14 $.95 $13.30
Notebook 10 $1.47 $14.70
….
Note:
The last column will show a computed value (price * quantity) for
the item
At the end of this operation, go back to displaying the 5
operations that user
can choose from, as in (a) above
(c) checkForInventory
This operation checks for items that have quantity less than 5, and
then prints
them in the same format as in (b) above. For example:
Items with quantity less that 5:
Item_Name Quantity Price Total_Inventory_Value
Notebook 7 $1.47 $10.29
Marker 4 $1.02 $4.08
….
….
At the end of this operation, go back to displaying the 5
operations that user
can choose from, as in (a) above
(d) highestAndLowestValueItems
This operation finds the highest, and lowest, value item(s) in
inventory,
computed by multiplying the quantity with the price for that item,
and then
prints them in the same format as in (b) above.
Highest Inventory Value Items:
Item_Name Quantity Price Total_Inventory_Value
Notebook 7 $1.47 $10.29
Marker 4 $1.02 $4.08
….
Lowest Inventory Value Items:
Item_Name Quantity Price Total_Inventory_Value
Pencil 4 $.74 $2.96
Marker 2 $1.02 $2.04
….
At the end of this operation, go back to displaying the 5
operations that user
can choose from, as in (a) above
(e) totalInventoryValue
This operation simply prints the Grand Total value of the entire
inventory. This
will essentially be the summation of all individual total values of
each item in
the inventory
At the end of this operation, go back to displaying the 5
operations that user
can choose from, as in (a) above
(f) exit
This operation simply exits the program
package com.example; import java.util.Scanner; import static java.lang.System.exit; public class IMS { String items[] = {"Pen", "Pencil", "Eraser", "Marker", "Notepad"}; int quantity[] = new int[5]; double price[] = new double[5]; void initialize() { for (int i = 0; i < 5; i++) { System.out.println("Please enter quantity for " + items[i]); quantity[i] = getValidQuantity(items[i]); } for (int i = 0; i < 5; i++) { System.out.println("Please enter price for " + items[i]); price[i] = getValidPrice(items[i]); } } int getValidQuantity(String str) { Scanner scanner = new Scanner(System.in); int count; while (true) { count = scanner.nextInt(); if (count > 0) break; System.out.println("Please enter valid quantity for " + str); } return count; } double getValidPrice(String str) { Scanner scanner = new Scanner(System.in); double amount; while (true) { amount = scanner.nextDouble(); if (amount > 0) break; System.out.println("Please enter valid price for " + str); } return amount; } void printInventoryIndex(boolean[] arr) { System.out.println("Item_name\tQuantity\tPrice\tTotal_Inventory_Value"); for (int i = 0; i < arr.length; i++) { if (arr[i]) { String str = String.format("%s\t%s\t$%s\t$%s", items[i], quantity[i], price[i], quantity[i] *price[i]); System.out.println(str); } } } void printInventory() { boolean[] arr = {true, true, true, true, true}; printInventoryIndex(arr); } void printInventoryLowQuantity() { boolean[] arr = {false, false, false, false, false}; for (int i = 0; i < 5; i++) { if (quantity[i] < 5) arr[i] = true; } printInventoryIndex(arr); } double getMedian () { // sort array and return median double[] value = new double[5]; for (int i = 0; i < 5; i++) { value[i] = quantity[i] * price[i]; } for (int i = 0; i < value.length; i++) { int maxI = i; for (int j = i; j < value.length; j++) { if (value[j] > value[maxI]) { maxI = j; } } // swap location double temp = value[maxI]; value[maxI] = value[i]; value[i] = temp; } return value[2]; } void highestAndLowestValueItems() { double[] value = new double[5]; for (int i = 0; i < 5; i++) { value[i] = quantity[i] * price[i]; } double mValue =getMedian(); // get highest value items boolean[] maxarr = {false, false, false, false, false}; for (int i = 0; i < 5; i++) { if (mValue <= value[i]) { maxarr[i] = true; } } System.out.println("Highest Inventory Value Items:"); printInventoryIndex(maxarr); // get lowest value items boolean[] minarr = {false, false, false, false, false}; for (int i = 0; i < 5; i++) { if (mValue >= value[i]) { minarr[i] = true; } } System.out.println("Lowest Inventory Value Items:"); printInventoryIndex(minarr); } void totalInventoryValue() { double value = 0; for (int i = 0; i < 5; i++) { value += quantity[i] * price[i]; } System.out.println("Total Inventory Value: $" + value); } public static void main(String[] args) { IMS ims = new IMS(); ims.initialize(); char input; Scanner scanner = new Scanner(System.in); while (true) { System.out.println("\nPrint Inventory (p)\n" + "Check for Low Inventory (c)\n" + "Highest/Lowest value inventory Items (h)\n" + "Total Inventory Value (t)\n" + "Exit System (e)"); input = scanner.nextLine().charAt(0); switch(input) { case 'p': ims.printInventory(); break; case 'c': ims.printInventoryLowQuantity(); break; case 'h': ims.highestAndLowestValueItems(); break; case 't': ims.totalInventoryValue(); break; case 'e': exit(0); break; default: continue; } } } }