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

Warehouse Inventories using Java Objective: Work with multiple objects and revie

ID: 3858983 • Letter: W

Question

Warehouse Inventories using Java Objective: Work with multiple objects and review reading data files. Description: A wholesale distributor has six warehouses (Atlanta, Baltimore, Chicago, Denver, Ely and Fargo) and sells five different items (identified by part number: 102, 215, 410, 525 and 711). Each warehouse may stock any or all of the five items. The company buys and sells these items constantly. Company transaction records contain a transaction code (‘P’ for a purchase or ‘S’ for a sale) followed by an item number and the quantity (bought or sold). The transaction records are contained in a transaction data file named Transactions.txt. Sample transaction records: Transactions.txt P, 410, 1000 S, 215, 120 S, 711, 300 P, 215, 500 P, 102, 400 P, 711, 1000 S, 410, 200 P, 525, 100 S, 102, 80 S, 525, 75 S, 215, 300 S, 711, 650 P, 102, 150 P, 410, 400 S, 525, 50 S, 711, 100 P, 215, 325 P, 215, 100 S, 410, 225 S, 215, 200 | A separate data file contains the initial status of the six warehouses at the beginning of the day (i.e., the ending status from the night before). This data file has only six records (lines). Each record (line) contains five numbers that show the quantity on hand for the five items in that warehouse. This file is named Inventory.txt. Sample status data file: Inventory.txt 500, 120, 60, 0, 350 100, 230, 0, 50, 0 0, 75, 0, 0, 220 3000, 400, 600, 35, 0 350, 200, 100, 80, 250 0, 285, 325, 150, 75 | The status data file is updated by processing the transaction records in the transaction data file according to these rules: 1 – For a sale (‘S’) – subtract the quantity sold from the warehouse that has the largest supply of that item on hand. 2 – For a purchase (‘P’) – add the quantity purchased to the warehouse that has the lowest supply of that item on hand. Instructions: Write an object-oriented Java program to do the above inventory warehouse processing. Each of the six warehouses should be treated as an individual object. For example, Atlanta would be an object with each of the five part numbers as instance fields. Each of the other warehouses should also be objects with the five part numbers as instance fields. Of course, there would be one class which would be the main (driver) class from which these 6 objects would be created. In the beginning of the program, the status data file (Inventory.txt) should be read and an object for each warehouse created. The Inventory.txt data file is in the following order: the first line is the Atlanta warehouse, the second line is the Baltimore warehouse, third Chicago, then Denver, Ely and Fargo. After the objects are created, the transactions data file (Transactions.txt) are read and processed. The objects should be updated as the transaction records are read and processed. The program should: 1 – Display the initial (beginning-of-day) status for all warehouses. 2 – Process each transaction from the transaction data file and show which warehouse’s inventory was updated to reflect that transaction. 3 – Display the final (end-of-day) status for all warehouses. Requirements: The class must be named Inventory. The main program (driver) must be named Warehouses. A driver (main class) as well as a programmer-defined class must be used

Explanation / Answer

Given below is the code and output. Please rate the answer if it helped. Thank you.

Item.java

public class Item {

  

   private int itemNo;

   private int quantity;

   public Item(int pnum)

   {

       this.itemNo = pnum;

   }

   public int getQuantity() {

       return quantity;

   }

   public void setQuantity(int quantity) {

       this.quantity = quantity;

   }

   public int getItemNo() {

       return itemNo;

   }

  

}

Warehouse.java

public class Warehouse {

   private String name;

   private Item[] items;

   public Warehouse(String name)

   {

       this.name = name;

       items = new Item[5]; //each warehouse has 5 different items

       items[0] = new Item(102);

       items[1] = new Item(215);

       items[2] = new Item(410);

       items[3] = new Item(525);

       items[4] = new Item(711);

      

   }

  

   public String getName() {

       return name;

   }

  

   public void purchase(int itemNo, int qty) {

       Item item = getItem(itemNo);

       if(item != null)

           item.setQuantity(item.getQuantity() + qty);  

          

   }

   public void sell(int itemNo, int qty)

   {

       Item item = getItem(itemNo);

       if(item != null)

       {

           if(item.getQuantity() >= qty)

               item.setQuantity(item.getQuantity() - qty);

       }

   }

  

   public int getQuantity(int itemNo)

   {

       Item item = getItem(itemNo);

       if(item == null)

           return 0;

       else

           return item.getQuantity();

   }

   //searches the list of items and returns an item with matching itemno

   private Item getItem(int itemNo)

   {

       for(int i = 0; i < items.length; i++)

           if(items[i].getItemNo() == itemNo)

               return items[i];

       return null;//when not found

   }

   public void display()

   {

       System.out.println("Warehouse: " + name );

       for(int i = 0; i < items.length; i++)

           System.out.println(" Item: "+ items[i].getItemNo() + ", Quantity: " + items[i].getQuantity() + " nos ");

       System.out.println();

   }

}

Inventory.java

public class Inventory {

   private Warehouse[] warehouses; // an array of warehouses

   public Inventory()

   {

       //create and initialize each of the warehouse

       warehouses = new Warehouse[6]; // 6 warehouses

       warehouses[0] = new Warehouse("Atlanta");

       warehouses[1] = new Warehouse("Baltimore");

       warehouses[2] = new Warehouse("Chicago");

       warehouses[3] = new Warehouse("Denver");

       warehouses[4] = new Warehouse("Ely");

       warehouses[5] = new Warehouse("Fargo");

   }

  

   //returns the warehouse with largest quantity for the specified itemno

   public Warehouse getLargestWarehouseForItem(int itemNo)

   {

       Warehouse largest = warehouses[0];

       int largestQty = largest.getQuantity(itemNo);

      

       for(int i = 1; i < warehouses.length; i++)

       {

           if(warehouses[i].getQuantity(itemNo) > largestQty)

           {

               largestQty = warehouses[i].getQuantity(itemNo);

               largest = warehouses[i];

           }

       }

      

       return largest;

   }

  

   //returns the warehouse with lowest quantity for the specified itemno

       public Warehouse getLowestWarehouseForItem(int itemNo)

       {

           Warehouse lowest = warehouses[0];

           int lowestQty = lowest.getQuantity(itemNo);

          

           for(int i = 1; i < warehouses.length; i++)

           {

               if(warehouses[i].getQuantity(itemNo) < lowestQty)

               {

                   lowestQty = warehouses[i].getQuantity(itemNo);

                   lowest = warehouses[i];

               }

           }

          

           return lowest;

       }

      

       //return warehouse object for the specified warehouse name

       public Warehouse getWarehouse(String name)

       {

           for(int i = 0; i < warehouses.length; i++)

           {

               if(warehouses[i].getName().equalsIgnoreCase(name))

                   return warehouses[i];

           }

           return null;

       }

      

       public void display()

       {

           System.out.println("=================== Inventory =====================");

           for(int i = 0; i < warehouses.length; i++)

               warehouses[i].display();

           System.out.println("===================================================");

       }

}

Warehouses.java

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

import java.util.StringTokenizer;

public class Warehouses {

   private static void load_inventory_data(String filename, Inventory inventory) throws FileNotFoundException

   {

       String[] names = {"Atlanta", "Baltimore", "Chicago", "Denver", "Ely", "Fargo"};

       int[] items = {102, 215, 410, 525, 711};

       int i = 0;

       Scanner infile = new Scanner(new File(filename));

       while(infile.hasNextLine())

       {

           String line = infile.nextLine();

           if(line.trim().equals("")) //skip any empty lines

               continue;

           String tokens[] = line.split(",");

  

           Warehouse wh = inventory.getWarehouse(names[i]);

           for(int j = 0; j < tokens.length; j++)

               wh.purchase(items[j], Integer.parseInt(tokens[j].trim()));

          

          

           i++;

       }

       infile.close();

   }

  

   private static void process_transactions(String filename, Inventory inventory) throws FileNotFoundException

   {

       Scanner infile = new Scanner(new File(filename));

       while(infile.hasNextLine())

       {

           String line = infile.nextLine();

           if(line.trim().equals("")) //skip any empty lines

               continue;

           //now scan through the line and get transaction details

          

           String tokens[] = line.split(",");

          

           String type = tokens[0].trim();

           int item = Integer.parseInt(tokens[1].trim());

           int qty = Integer.parseInt(tokens[2].trim());

           Warehouse wh;

           if(type.equalsIgnoreCase("P"))

           {

               wh = inventory.getLowestWarehouseForItem(item);

               wh.purchase(item, qty);

               System.out.println("Purchasing " + qty + " nos. of " + item + " for " + wh.getName());

           }

           else if (type.equalsIgnoreCase("S"))

           {

               wh = inventory.getLargestWarehouseForItem(item);

               wh.sell(item, qty);

               System.out.println("Selling " + qty + " nos. of " + item + " from " + wh.getName());

           }

          

             

       }

       infile.close();

   }

  

   public static void main(String[] args) {

       String transactionFile = "Transactions.txt";

       String inventoryFile = "Inventory.txt";

       Inventory inventory = new Inventory();

      

       try {

           load_inventory_data(inventoryFile, inventory);

           System.out.println("Loaded the inventory...");

           inventory.display();

           process_transactions(transactionFile, inventory);

           System.out.println("End of day inventory");

           inventory.display();

       } catch (FileNotFoundException e) {

           System.out.println(e.getMessage());

       }

      

      

   }

}

input file : Inventory.txt

500, 120, 60, 0, 350
100, 230, 0, 50, 0
0, 75, 0, 0, 220
3000, 400, 600, 35, 0
350, 200, 100, 80, 250
0, 285, 325, 150, 75

input file : Transactions.txt

P, 410, 1000
S, 215, 120
S, 711, 300
P, 215, 500
P, 102, 400
P, 711, 1000
S, 410, 200
P, 525, 100
S, 102, 80
S, 525, 75
S, 215, 300
S, 711, 650
P, 102, 150
P, 410, 400
S, 525, 50
S, 711, 100
P, 215, 325
P, 215, 100
S, 410, 225
S, 215, 200

output

Loaded the inventory...

=================== Inventory =====================

Warehouse: Atlanta

   Item: 102 Quantity: 500 nos

   Item: 215 Quantity: 120 nos

   Item: 410 Quantity: 60 nos

   Item: 525 Quantity: 0 nos

   Item: 711 Quantity: 350 nos

Warehouse: Baltimore

   Item: 102 Quantity: 100 nos

   Item: 215 Quantity: 230 nos

   Item: 410 Quantity: 0 nos

   Item: 525 Quantity: 50 nos

   Item: 711 Quantity: 0 nos

Warehouse: Chicago

   Item: 102 Quantity: 0 nos

   Item: 215 Quantity: 75 nos

   Item: 410 Quantity: 0 nos

   Item: 525 Quantity: 0 nos

   Item: 711 Quantity: 220 nos

Warehouse: Denver

   Item: 102 Quantity: 3000 nos

   Item: 215 Quantity: 400 nos

   Item: 410 Quantity: 600 nos

   Item: 525 Quantity: 35 nos

   Item: 711 Quantity: 0 nos

Warehouse: Ely

   Item: 102 Quantity: 350 nos

   Item: 215 Quantity: 200 nos

   Item: 410 Quantity: 100 nos

   Item: 525 Quantity: 80 nos

   Item: 711 Quantity: 250 nos

Warehouse: Fargo

   Item: 102 Quantity: 0 nos

   Item: 215 Quantity: 285 nos

   Item: 410 Quantity: 325 nos

   Item: 525 Quantity: 150 nos

   Item: 711 Quantity: 75 nos

===================================================

Purchasing 1000 nos. of 410 for Baltimore

Selling 120 nos. of 215 from Denver

Selling 300 nos. of 711 from Atlanta

Purchasing 500 nos. of 215 for Chicago

Purchasing 400 nos. of 102 for Chicago

Purchasing 1000 nos. of 711 for Baltimore

Selling 200 nos. of 410 from Baltimore

Purchasing 100 nos. of 525 for Atlanta

Selling 80 nos. of 102 from Denver

Selling 75 nos. of 525 from Fargo

Selling 300 nos. of 215 from Chicago

Selling 650 nos. of 711 from Baltimore

Purchasing 150 nos. of 102 for Fargo

Purchasing 400 nos. of 410 for Chicago

Selling 50 nos. of 525 from Atlanta

Selling 100 nos. of 711 from Baltimore

Purchasing 325 nos. of 215 for Atlanta

Purchasing 100 nos. of 215 for Ely

Selling 225 nos. of 410 from Baltimore

Selling 200 nos. of 215 from Atlanta

End of day inventory

=================== Inventory =====================

Warehouse: Atlanta

   Item: 102 Quantity: 500 nos

   Item: 215 Quantity: 245 nos

   Item: 410 Quantity: 60 nos

   Item: 525 Quantity: 50 nos

   Item: 711 Quantity: 50 nos

Warehouse: Baltimore

   Item: 102 Quantity: 100 nos

   Item: 215 Quantity: 230 nos

   Item: 410 Quantity: 575 nos

   Item: 525 Quantity: 50 nos

   Item: 711 Quantity: 250 nos

Warehouse: Chicago

   Item: 102 Quantity: 400 nos

   Item: 215 Quantity: 275 nos

   Item: 410 Quantity: 400 nos

   Item: 525 Quantity: 0 nos

   Item: 711 Quantity: 220 nos

Warehouse: Denver

   Item: 102 Quantity: 2920 nos

   Item: 215 Quantity: 280 nos

   Item: 410 Quantity: 600 nos

   Item: 525 Quantity: 35 nos

   Item: 711 Quantity: 0 nos

Warehouse: Ely

   Item: 102 Quantity: 350 nos

   Item: 215 Quantity: 300 nos

   Item: 410 Quantity: 100 nos

   Item: 525 Quantity: 80 nos

   Item: 711 Quantity: 250 nos

Warehouse: Fargo

   Item: 102 Quantity: 150 nos

   Item: 215 Quantity: 285 nos

   Item: 410 Quantity: 325 nos

   Item: 525 Quantity: 75 nos

   Item: 711 Quantity: 75 nos

===================================================