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

Build an interactive program that reads in the information about the Inventory o

ID: 3534700 • Letter: B

Question

Build an interactive program that reads in the information about the Inventory objects from the file Inventory.txt into an array of Inventory objects

Provide a Panel on the screen that has a TextField for the product code and a Button called “Lookupâ€. The user should be able to type in a Product code into the JTextField, and hit the button. Your program should then do a Binary Search of the array of Inventory objects to find the appropriate inventory object.

Build a second JPanel on the screen to show the results of the lookup. This panel should have:

A JTextField showing the Price of the Item

A JTextField showing the Quantity on Hand of the Item

A JTextField showing the Status of the lookup

If the item is found, the Price and Quantity on Hand should be displayed. If not found, the Price and Quantity on Hand should be blank, and the Status should say “Item Not Foundâ€

This program can be done with a simple grid layout on the screen, but a more elegant solution would be to have a JPanel on the top of the screen with a Label saying “Product Codeâ€, a JTextField for the user entry, and a JButton. Then, a JPanel with the GridLayout could be built for the bottom of the screen with three rows and Labels on each row such as “Priceâ€, “Qty on Handâ€, and “Statusâ€.

The JPanel class is not mentioned in the book, but provides a way to divide the screen up. Simply create a Panel with “JPanel p = new JPanel ( );†You can then add a Panel to the Frame with the “add†method, followed by the desired section of the screen: BorderLayout.NORTH for the top, or BorderLayout.CENTER for the rest of the screen. Items can then be added to the JPanel just like the JFrame, and they will go to the section of the JFrame given to the JPanel. You can also change the layout of the JPanel with the same setLayout( ) method as the JFrame, but you’re just changing one part of the screen.

Here's sample code for the Panel:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PanelSample extends JFrame
{

public PanelSample()
{
setTitle("INVEVTORY REPORT JAVA 205");
setLayout(new BorderLayout());
setSize(400,150);
Container contentPane = getContentPane();

topPanel= new JPanel(new FlowLayout());
lookUp = new JButton("Look Up");
productCode = new JTextField("Enter product code here");
topPanel.add(new JLabel("Product Code:"));
topPanel.add(productCode);
topPanel.add(lookUp);

bottomPanel = new JPanel(new GridLayout(0,2));
price = new JTextField(" ");
quantityOnHand = new JTextField(" ");
status = new JTextField(" ");
bottomPanel.add(new JLabel("Price: " ));
bottomPanel.add(price);
bottomPanel.add(new JLabel("Quantity On Hand : "));
bottomPanel.add(quantityOnHand);
bottomPanel.add(new JLabel("Status: "));
bottomPanel.add(status);

contentPane.add(topPanel, BorderLayout.NORTH);
contentPane.add(bottomPanel, BorderLayout.SOUTH);

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
} // windowClosing

}); // WindowListener

} // end of constructor
static JPanel topPanel;
static JPanel bottomPanel;
static JButton lookUp;
static JTextField productCode;
static JTextField price;
static JTextField quantityOnHand;
static JTextField status;

public static void main(String [] args) {
JFrame frame = new PanelSample();
frame.show();

} // main

} // PanelSample

Here's the values from Inventory.txt file:

1168,4.25,10
1305,1.80,42
1345,12.56,16
1388,7.42,30
1480,6.54,80
1495,8.36,48
1560,15.27,65

And lastly here's a sample Inventory Class code that was created on another previous project:

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

public class Inventory {
//Declare variables
int productCode;
double price;
int quantityOnHand;

//Create args-constructor for Inventory objects
public Inventory(String InventoryRecord) {
String Data[];
Data=InventoryRecord.split(",");
setCode(Integer.parseInt(Data[0]));
setPrice(Double.parseDouble(Data[1]));
setInStock(Integer.parseInt(Data[2]));
}

//set method for product code
public void setCode(int code) {
productCode = code;
}

//set method for price
public void setPrice(double price) {
this.price = price;
}

//set method for quantity on hand
public void setInStock(int inStock) {
quantityOnHand = inStock;
}

//method that returns product code
public int getCode() {
return productCode;
}

//method that returns price
public double getPrice() {
return price;
}

//method that returns quantity on hand
public int getInStock() {
return quantityOnHand;
}

//main method

public static void main(String[] arg) throws IOException {
//Declare variable
String line;
boolean found = true;
int tempQty = 0;

//Create FileReader for inventory data
FileReader file = new FileReader("Inventory.txt");
Inventory iv[] = new Inventory[100];
BufferedReader f = new BufferedReader(file);
int j = 0;

//loop through file strings and create inventory objects
while((line = f.readLine()) != null) {
iv[j] = new Inventory(line);
j++;
}
}
}

Explanation / Answer

Please rate with 5 stars :)


ExampleInventoryRead retrieves inventory information from our entity store and displays it. When it displays each inventory item, it also displays the related vendor contact information.

ExampleInventoryRead can do one of two things. If you provide no search criteria, it displays all of the inventory items in the store. If you provide an item name (using the -s command line switch), then just those inventory items using that name are displayed.

The beginning of our example is almost identical to our ExampleDatabasePut example program. We repeat that example code here for the sake of completeness. For a complete walk-through of it, see the previous section (ExampleDatabasePut.java).

The first method that we provide is used to show inventory items related to a given inventory name. This method is called only if an inventory name is passed to ExampleInventoryRead via the -s option. Given the sample data that we provide with this example, each matching inventory name will result in the display of three inventory objects.

To display these objects we use the Inventory class' inventoryByName secondary index to retrieve an EntityCursor, and then we iterate over the resulting objects using the cursor.

Notice that this method calls displayInventoryRecord() to display each individual object. We show this method a little later in the example.

Next we implement showAllInventory(), which shows all of the Inventory objects in the store. To do this, we obtain an EntityCursor from the Inventory class' primary index and, again, we iterate using that cursor.

Now we implement displayInventoryRecord(). This uses the getter methods on theInventory class to obtain the information that we want to display. The only thing interesting about this method is that we obtain Vendor objects within. The vendor objects are retrieved Vendor objects using their primary index. We get the key for the retrieval from the Inventory object that we are displaying at the time.

The last remaining parts of the example are used to parse the command line. This is not very interesting for our purposes here, but we show it anyway for the sake of completeness.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote