Write an inventory manager program in Java. The information about what stock you
ID: 3771045 • Letter: W
Question
Write an inventory manager program in Java. The information about what stock you have on hand is stored in a le. Your program will read the items you have in stock from a le. Each line read from the le will be an Item object. There will be 10 in total. The Item objects created MUST be stored in an Array of Item objects. The array size will be 10. Once your inventory is loaded your program will do the four following operations:
1 Print Inventory
2 Check for low Inventory
3 Print Current Value of Inventory
4 Exit Program.
Assignment Steps:
1 Setting up Project: 1.1 Create a Project named InventoryManager 1.2 Add a le named Item to your project. 1.3 Download the items le
File:
Paper 10 5.99
ColoredPaper 10 6.99
PaperClips 55 0.99
Staples 1000 0.10
Chairs 3 49.99
Pens 500 0.75
Pencils 500 .10
Highlighters 100 1.00
Toner 2 69.99
Post-its 100 1.99
1.4 Copy/Move the le to the project folder named InventoryManager. This should be inside your NetBeans Project Folder.
2 Implement Item.java Class: 2.1 Create three data elds : name , quantity, and price per unit. 2.2 Create a no-arg constructor that initializes NO data elds and a constructor that initializes ALL data elds. 2.3 Create all getters and setters for each data eld. 2.4 Implement a toString method that builds a nicely formatted string representing an Item object. YOU ARE NOT ALLOWED to print from this method. IT MUST return a String
3 Implement InventoryManager.java Class, do the following:
– Write a class method called printInventory which takes an Array of Items as its parameter. This method will print the entire inventory to the console.
– Write a class method called checkLowInventory which takes an Array of Items as its parameter. This method checks for items that have fewer quantity than 5, and prints them in the same format as printInventory does. If there is no such item then print an appropriate message.
– Write a class method called totalInventoryValue which takes an Array of Items as its parameter. This method computes the total value of the current inventory using the quantity and price per unit information, and prints that total value to the console.
In the main method:
3.1 Use File class and Scanner class to read the items and related information from the le provided with this assignment. It is available on ilearn. 3.2 For every item in this le create a new object of the Item class with appropriate values. 3.3 Let all the objects be a part of an array. There are 10 items in the le. 3.4 Then print the following menu to the console :
PrintInventory (p)
Check Low inventory (c)
Print Total Value of inventory (v)
Exit (e)
3.5 Prompt the user to enter an option. Read this option in as a char. You can assume they will enter a lower case character and that the input will be valid. 3.6 Depending on the option entered you will do the following:
If the user enters p, call printInventory method
If the user enters c, call checkLowInventory method
If the user enters v, call totalInventoryValue method
If the user enters e, Program Exits
3.7 Loop steps 3.4 to 3.6 until the user enters e as an option.
Explanation / Answer
Item.java
class Item
{
String name;
int quantity;
double price;
Item()
{
}
Item(String name,int quantity,double price)
{
this.name=name;
this.quantity=quantity;
this.price = price;
}
void setName(String name)
{
this.name=name;
}
String getName()
{
return name;
}
void setQuantity(int quantity)
{
this.quantity=quantity;
}
int getquantity()
{
return quantity;
}
void setPrice(double price)
{
this.price=price;
}
double getPrice()
{
return price;
}
}
-----------------------------------------------------------
Inventory.java
import java.io.*;
class Inventory
{
void printInventory(Item[] inventory)
{
int size;
size = inventory.length;
for(int i=0;i<size;i++)
{
System.out.println(inventory[i].getName()+" "+inventory[i].getquantity()
+" "+inventory[i].getPrice());
}
}
void checkLowInventory(Item[] inventory)
{
int size,cnt=0;
size =inventory.length;
for(int i=0;i<size;i++)
{
if(inventory[i].getquantity()<5)
{
System.out.println(inventory[i].getName()+" "+inventory[i].getquantity()
+" "+inventory[i].getPrice());
cnt++;
}
}
if(cnt==0)
{
System.out.println("NO item with quantity less than 5");
}
}
void totalInventoryValue(Item[] inventory)
{
int size,cnt=0;
size =inventory.length;
double totalValue=0;
for(int i=0;i<size;i++)
{
totalValue=totalValue+(inventory[i].getPrice()*inventory[i].getquantity());
}
System.out.println("Total inventory value is : "+totalValue);
}
}
---------------------------------------------
Compute.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
class Compute
{
public static void main(String[] args) throws FileNotFoundException
{
String line;
String[] temp;
File inpfile = new File("input.txt");
Scanner sc = new Scanner(inpfile);
Scanner sc1 = new Scanner(System.in);
Item[] itms = new Item[10];
Inventory iv = new Inventory();
for(int i=0;i<10;i++)
{
itms[i] = new Item();
itms[i].setName(sc.next());
itms[i].setQuantity(sc.nextInt());
itms[i].setPrice(sc.nextDouble());
}
while(true)
{
System.out.println("Enter choice an option :");
switch(sc1.next().charAt(0))
{
case 'p':iv.printInventory(itms);
break;
case 'c':iv.checkLowInventory(itms);
break;
case 'v':iv.totalInventoryValue(itms);
break;
case 'e':System.exit(0);
}
}
}
}
---------------
input.txt
Paper 10 5.99
ColoredPaper 10 6.99
PaperClips 55 0.99
Staples 1000 0.10
Chairs 3 49.99
Pens 500 0.75
Pencils 500 .10
Highlighters 100 1.00
Toner 2 69.99
Post-its 100 1.99
Output:
Enter choice an option :
p
Paper 10 5.99
ColoredPaper 10 6.99
PaperClips 55 0.99
Staples 1000 0.1
Chairs 3 49.99
Pens 500 0.75
Pencils 500 0.1
Highlighters 100 1.0
Toner 2 69.99
Post-its 100 1.99
Enter choice an option :
v
Total inventory value is : 1298.2
Enter choice an option :
c
Chairs 3 49.99
Toner 2 69.99
Enter choice an option :
e
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.