Build an interactive program that reads in the information about the Inventory o
ID: 3556532 • 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
Explanation / Answer
DO RATE if satisfied
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class PanelSample extends JFrame
{
Inventory [] iv;
int inventoryCount;
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);
//add actionListener for Lookup button;
lookUp.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String code = productCode.getText();
int codeValue = 0;
try{
codeValue = Integer.parseInt(code);
} catch ( NumberFormatException ex){
status.setText("Invalid Input");
return;
}
status.setText("Searcing ... ");
//Binary Search
int lo = 0;
int hi = inventoryCount-1;
int mid = 0;
boolean found = false;
while( lo <= hi){
mid = (lo+hi)/2;
if( iv[mid].productCode == codeValue ){
found = true;
break;
}else if( iv[mid].productCode > codeValue){
hi = mid-1;
} else if( iv[mid].productCode < codeValue)
lo = mid+1;
}
if( found == true){
price.setText( Double.toString(iv[mid].price));
quantityOnHand.setText( Double.toString(iv[mid].quantityOnHand));
status.setText("Product Found");
} else {
price.setText("");
quantityOnHand.setText("");
status.setText("Product Not Found");
}
}
});
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
} // windowClosing
}); // WindowListener
String line;
FileReader file;
try {
file = new FileReader("Inventory.txt");
iv = new Inventory[100];
BufferedReader f = new BufferedReader(file);
int j = 0;
//loop through file strings and create inventory objects
try {
while((line = f.readLine()) != null) {
iv[j] = new Inventory(line);
j++;
}
} catch (IOException e1) {
status.setText("IOException Raised while reading file: " + file);
}
inventoryCount = j;
} catch (FileNotFoundException e1) {
status.setText("File Not Found: ");
}
status.setText("File Loaded Sucesfully");
} // 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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.