Professional Documents
Culture Documents
Input/ Output String Provides a standard input dialog boxes for Buy,
valueBuy=JOptionPane.showInputDialog(f,"Enter Sell and Reprice respectively.
stock bought quantity: ");
int noOfStockBought = Integer.parseInt(valueBuy); Accquire input from the user and then updates
if (valueBuy==null){return;} the information accordingly in the Jtable.
allProducts[selectedRow].buyStock(noOfStockBou
ght);
DefaultTableModel m =
(DefaultTableModel) jt.getModel();
m.setValueAt(allProducts[selectedRow].getStockLe
vel(), selectedRow, 1);
m.fireTableDataChanged();
jt.repaint();allProducts[selectedRow].buyStock(noO
fStockBought);
DefaultTableModel m =
(DefaultTableModel) jt.getModel();
m.setValueAt(allProducts[selectedRow].getStockLe
vel(), selectedRow, 1);
m.fireTableDataChanged();
jt.repaint();
String valueSell=JOptionPane.showInputDialog(f,
"Enter stock sold quantity: ");
int noOfStockSold = Integer.parseInt(valueSell);
allProducts[selectedRow].sell(noOfStockSold);
DefaultTableModel m = (DefaultTableModel)
jt.getModel();
m.setValueAt(allProducts[selectedRow].getStockLe
vel(), selectedRow, 1);
m.fireTableDataChanged();
jt.repaint();
String alueReprice=JOptionPane.showInputDialog
(f, "Enter new price ");
double reprice =Double.parseDouble(valueReprice);
allProducts[selectedRow].set_Price(reprice);
DefaultTableModel m = (DefaultTableModel)
jt.getModel();
System.out.println(allProducts[selectedRow].getPric
e());
m.setValueAt(allProducts[selectedRow].getPrice(),
selectedRow, 2);
m.fireTableDataChanged();
jt.repaint();
Choices public double buyStock(int NoOfStockBought ){ Here, the buyStock(int NoOfStockBought)
method recieves number of new items of stock
this.stockLevel=this.stockLevel+NoOfStockBought; bought in integer , adds the number to the stock
return total_value(this.price,NoOfStockBought); level , updates the total value of stock and at
} last returns the value of stock bought.
Input menuItemBuy.addActionListener(new If the user enters value any other than integer in
Validation ActionListener() { the Input Dialog Box then the parseInt method
@Override won’t return the integer value hence it would go
public void actionPerformed(ActionEvent e) { to catch and sends “Error, enter a number”. It is
try { same for Sell and Re-price methods
String respectively.
valueBuy=JOptionPane.showInputDialog(f,"Enter
stock bought quantity: ");
System.out.println(valueBuy);
if (valueBuy==null){return;}
int noOfStockBought =
Integer.parseInt(valueBuy);
System.out.println("no of stock bought" +
noOfStockBought);
allProducts[selectedRow].buyStock(noOfStockBou
ght);
DefaultTableModel m =
(DefaultTableModel) jt.getModel();
m.setValueAt(allProducts[selectedRow].getStockLe
vel(), selectedRow, 1);
m.fireTableDataChanged();
jt.repaint();
allProducts[selectedRow].sell(noOfStockSold);
DefaultTableModel m =
(DefaultTableModel) jt.getModel();
m.setValueAt(allProducts[selectedRow].getStockLe
vel(), selectedRow, 1);
m.fireTableDataChanged();
jt.repaint();
} catch (Exception e1) {
JOptionPane.showMessageDialog(f, "Error,
enter a number.", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
});
menuItemReprice.addActionListener(new
ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String
valueReprice=JOptionPane.showInputDialog(f,
"Enter new price ");
System.out.println(valueReprice);
if(valueReprice==null){return;}
double reprice =
Double.parseDouble(valueReprice);
System.out.println("New Price" + reprice);
allProducts[selectedRow].set_Price(reprice);
DefaultTableModel m =
(DefaultTableModel) jt.getModel();
System.out.println(allProducts[selectedRow].getPric
e());
m.setValueAt(allProducts[selectedRow].getPrice(),
selectedRow, 2);
m.fireTableDataChanged();
jt.repaint();
} catch (Exception e2) {
JOptionPane.showMessageDialog(f, "Error,
enter price in double");
}
}
});
1. For-each 1. for (String c : column) { For displaying the column in the Jtable we used
Loop model.addColumn(c); for-each loop to iterate though the elements of
2. For Loop } arrays and ArrayList of column[].Likewise, We
used for-loop inorder to display the objects we
2. for (int i = 0; i < allProducts.length; i++) { created from Product class in the Jtable.
model.addRow(
new String[]{allProducts[i].getName(),
String.valueOf(allProducts[i].getStockLevel()),
String.valueOf(allProducts[i].getPrice()),String.valu
eOf(allProducts[i].total_value(allProducts[i].getPric
e(),allProducts[i].getStockLevel()))});
}
this.stockLevel=this.stockLevel+NoOfStockBought;
return
total_value(this.price,NoOfStockBought);
}
public boolean sell(int NoOfItemSold){
if(this.stockLevel>0){
this.stockLevel=this.stockLevel-
NoOfItemSold;
return true;
}
else{
System.out.println("Not Enough Items are
in stock");
return false;
}
}