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

FOR JAVA I have 2 functions that kind of work off the other, one takes an item a

ID: 654469 • Letter: F

Question

FOR JAVA

I have 2 functions that kind of work off the other, one takes an item and stores it in the inventory while the other takes the item and uses it in the program, I just want to make sure that these two functions work they way i need, here is what I have:


//Add the item to our inventory, if it is in the room.
   public static void get(String item){
      String[] inventory = new String[6];
      for(int i=0; i <= item.length(); i++) {
         inventory[i] = item + inventory;
      }    
    
   }
  
   //Remove the item from our inventory
   public static void put(String item){
      String[] inventory = new String[6];
      for(int i=0; i >= item.length(); i--) {
         inventory[i] = item + inventory;
      }
   }

Explanation / Answer

//Sample inventory class for driver program
//Inventory.java
public class Inventory
{
   private String itemName;
   private String sellerName;
   private double sellerPrice;
  
   //default constructor
   public Inventory() {
       itemName="";
       sellerName="";
       sellerPrice=0;
   }
   //parameterized constructor that takes the item name, seller name
   //and seller price of item
   public Inventory(String itemName, String sellerName, double sellerPrice) {
       this.itemName=itemName;
       this.sellerName=sellerName;
       this.sellerPrice=sellerPrice;
   }
  
  
   //Getter method that returns the item name
   public String getItemName()
   {
       return itemName;
   }
  
   //Getter method that returns the item name
   public String getSellerName()
   {
       return sellerName;
   }
  
   //Getter method that returns the item name
   public String getSellerPrice()
   {
       return itemName;
   }
  
   //Override the toString method
   @Override
   public String toString() {
      
       return "Item Name : "+itemName+" Seller Name : "+sellerName+
               " Seller Price : "+sellerPrice;
   }
}

---------------------------------------------------------------------------------------------------------

//Driver proram that demonstrates the Inventory array using add and remove methods that add items to
//inventory array usinng Inventory class as input argumet to the add and remove method and print
//the items in the inventory using printInventory method
//InventoryDriver.java
public class InventoryDriver
{
   //declare a Inventory array of static type
   private static Inventory[] inventory;
   //static counter variable to keep track of number of items in the inventory
   private static int counter=0;

   public static void main(String[] args)
   {
       //create size of STOCK=10 in inventory
       final int STOCK=10;
       //create an array of Inventory of fixed size
       inventory=new Inventory[STOCK];
      
       //adding items to the inventory array
       add(new Inventory("LO-TV", "LG", 500000));
       add(new Inventory("SONA-TV", "SONA", 600000));
       add(new Inventory("SIMSING-TV", "SIMSING", 700000));
       add(new Inventory("BPL-TV", "BPL", 1000000));
      
       System.out.println("ITEMS IN INVENTORY STORE");
       //print items in inventory array
       printInventory(inventory);
      
       System.out.println("Remove an item LO-TV from inventory");
       //Remove item from inventory
       remove("LO-TV");
      
       System.out.println("ITEMS IN INVENTORY STORE");
       //print items in inventory array
       printInventory(inventory);
      
       System.out.println("Remove an item BPL-TV from inventory");
       //Remove item from inventory
       remove("BPL-TV");
      
       System.out.println("ITEMS IN INVENTORY STORE");
       printInventory(inventory);
      
      
   }

   //The method printInventory that takes the inventory as input argumet that prints
   //the items in the inventory array
   private static void printInventory(Inventory[] inventory)
   {      
       for (int index = 0; index < counter; index++)
       System.out.println(inventory[index].toString());  
   }

   //Add the item to our inventory, if it is in the room.
   public static void add(Inventory inventoryItem)
   {

       if(counter<inventory.length)      
       {
           inventory[counter]=inventoryItem;
           counter++;
       }
       else
           System.out.println("Inventory is Full");

   }

   //Remove the item from our inventory
   public static void remove(String itemName)
   {
       boolean found=false;
       int postion=-1;
       //check if item is in inventory
       for (int index = 0; index < counter && ! found; index++)
       {
           if(inventory[index].getItemName().equalsIgnoreCase(itemName))
           {
               found=true;
               postion=index;
           }
       }
              
       //Remove items from inventory by moving items
      
       for (int index = postion; index < counter; index++)
           inventory[index]=inventory[index+1];
      
       //Decrease the value of the counter value by one
       counter--;
   }
}
----------------------------------------------------------------

sample output:


ITEMS IN INVENTORY STORE
Item Name : LO-TV Seller Name : LG Seller Price : 500000.0
Item Name : SONA-TV Seller Name : SONA Seller Price : 600000.0
Item Name : SIMSING-TV Seller Name : SIMSING Seller Price : 700000.0
Item Name : BPL-TV Seller Name : BPL Seller Price : 1000000.0
Remove an item LO-TV from inventory
ITEMS IN INVENTORY STORE
Item Name : SONA-TV Seller Name : SONA Seller Price : 600000.0
Item Name : SIMSING-TV Seller Name : SIMSING Seller Price : 700000.0
Item Name : BPL-TV Seller Name : BPL Seller Price : 1000000.0
Remove an item BPL-TV from inventory
ITEMS IN INVENTORY STORE
Item Name : SONA-TV Seller Name : SONA Seller Price : 600000.0
Item Name : SIMSING-TV Seller Name : SIMSING Seller Price : 700000.0


Note : Creating Inventory class makes program easier to manipulate
the items stored in the Inventory array.
Provide code in the question has some errors that why written new
complete code. As you not provide, Inventory description , assumed
an inventory with item name, seller name and seller price as members
of class.


Hope this helps you