Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your program will read the items you have in stock from a file. Each line read f

ID: 3772495 • Letter: Y

Question

Your program will read the items you have in stock from a file. Each line read from the file 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.

1 Setting up Project:

1.1 Create a Project named InventoryManager

1.2 Add a file named Item to your project.

1.3 Download the items file from ilearn under the assignment PDF.

1.4 Copy/Move the file to the project folder named InventoryManager. This should be inside your NetBeans Project Folder.

2 Implement Item.java Class:

2.1 Create three data fields : name , quantity, and price per unit. Make sure all data fields are private and have the appropriate type.

2.2 Create a no-arg constructor that initializes NO data fields and a constructor that initializes ALL data fields.

2.3 Create all getters and setters for each data field.

2.4 Implement a toString method that builds a nicely formatted string represent- ing an Item object. REMEMBER YOU ARE NOT ALLOWED to print from this method. IT MUST return a String.

1

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. It should print in the following way:

– 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 file provided with this assignment. It is available on ilearn.

3.2 For every item in this file create a new object of the Item class with appro- priate values.

3.3 Let all the objects be a part of an array. There are 10 items in the file.

3.4 Then print the following menu to the console :

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.


Items:

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

Explanation / Answer

Here is the complete JAVA code

import java.util.*;
import java.io.*;


class Item
{
   private String name;
   private int quantity;
   double price;
  
   Item()
   {
       name="";
       quantity=0;
       price=0;
   }
   Item(String name, int quantity, double price)
   {
       this.name=name;
       this.quantity=quantity;
       this.price=price;
   }
  
   public String getName()
   {
       return this.name;
   }
   public int getQuantity()
   {
       return this.quantity;
   }
   public double getPrice()
   {
       return this.price;
   }
   public void setName(String name)
   {
       this.name=name;
   }
   public void setQuantity(int quantity)
   {
       this.quantity=quantity;
   }
   public void setPrice(double price)
   {
       this.price=price;
   }
  
   public String toString(Item item[])
   {
       return this.name+" "+this.quantity+" "+this.price;
   }

}
class InventoryManager
{
  
   public void printInventory(Item list[])
   {
       if(list.length<1)
       System.out.println(" List is empty");
      
       else
       {
           System.out.println(" Name Quantity Price Per Unit ");
           for(int i=0;i<list.length;i++)
           {
               //System.out.println(String.format("%-10s:%10s", list[i].getName()+" "+list[i].getQuantity()+" "+list[i].getPrice()));
               System.out.printf("%-18s %-15d %3f",list[i].getName(), list[i].getQuantity(), list[i].getPrice() );
               System.out.println("");
           }
           System.out.println(" ");
       }
   }
  
   public void checkLowInventory(Item list[])
   {
       if(list.length<1)
       System.out.println(" List is empty");
      
       else
       {
           System.out.println(" Low Inventory ");
           System.out.println(" Name Quantity Price Per Unit ");
           for(int i=0;i<list.length;i++)
           {
               if(list[i].getQuantity()<5)
               {
                  
                   System.out.printf("%-18s %-15d %3f",list[i].getName(), list[i].getQuantity(), list[i].getPrice() );
                   System.out.println("");
               }
           }
          
       }
   }
   public void totalInventoryValue(Item list[])
   {
       if(list.length<1)
       System.out.println(" List is empty");
      
       else
       {
           double val=0;
           System.out.println(" Total Inventory Value");
           System.out.println("");
           for(int i=0;i<list.length;i++)
           {
               val=val+(list[i].getPrice()*list[i].getQuantity());
              
           }
           System.out.println("Total Value: "+val+" ");
       }
   }

}

public class InventorySystem
{
  
   public static void main(String args[])
   {
  
       String option;
      
       Item t;
       Item list[]=new Item[10];
       InventoryManager IM=new InventoryManager();
          try
          {
           File f=new File("items.txt");
           Scanner fr = new Scanner(f);
           int q; double p;
           int i=0;
           while (fr.hasNextLine())
           {
               String line = fr.nextLine();
               String ar[]=line.split(" ");
               q=Integer.parseInt(ar[1]);
               p=Double.valueOf(ar[2].trim()).doubleValue();
              
               t=new Item(ar[0],q,p);
               list[i]=t;
               i++;
               //System.out.println(ar[1]);
           }
              fr.close();
          }
          catch (Exception e)
          {
           System.out.println(e);
          }
      
       Scanner input = new Scanner(System.in);
       boolean flag=true;
       while(flag)
       {
           System.out.println("Enter Option: ");
           System.out.println(" PrintInventory (p)");
           System.out.println("Check Low inventory (c)");
           System.out.println("Print Total Value of inventory (v)");
           System.out.println("Exit (e) ");
           option=input.nextLine();
           option=option.toUpperCase();
           switch(option)
           {
               case "P":
               IM.printInventory(list);
               break;
              
               case "C":
               IM.checkLowInventory(list);
               break;
              
               case "V":
               IM.totalInventoryValue(list);
               break;
              
               case "E":
               flag=false;
               System.exit(0);
               break;
           }
       }
   }
}