You are on page 1of 7

Student ID:

Programming Example code in ShopApp.java Comment


Concept(1
mark each=8
marks)
Variables public String getName() { Created Getter & Setter method for each
return name; variable.
}
Declared a constructor to initialize the values of
public void setName(String name) { the variables.
this.name = name;
}

public int getStockLevel() {


return stockLevel;
}

public void setStockLevel(int stockLevel) {


this.stockLevel = stockLevel;
}

public double getPrice() {


return price;
}

public void setPrice(double price) {


this.price = price;
}

public Product(String aName, int aStockLevel,


double aPrice ){
this.name=aName;
this.stockLevel=aStockLevel;
this.price=aPrice;
}

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.

public boolean sell(int NoOfItemSold){ Similarly, sell(int NoOfItemSold) method


if(this.stockLevel>0){ receives the numer of items sold, reduces the
this.stockLevel=this.stockLevel- stock level accordingly(first checking to see if
NoOfItemSold; enough items are in stock) and returns true if the
return true; sale was successful and false otherwise.
}
else{ Likewise set_Price(double newPrice) method
System.out.println("Not Enough Items are in receives and sets a new price for the product.
stock");
return false; Lastly total_value() method displays the total
} value of all stocks in the shop
}

public double set_Price(double newPrice ){


this.price=newPrice;
return price;
}
public double total_value(double price,int
stockLevel){
return price*stockLevel;
}

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();

} catch (Exception e1) {


JOptionPane.showMessageDialog(f, "Error,
enter a number.");
}
}
});
menuItemSell.addActionListener(new
ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String
valueSell=JOptionPane.showInputDialog(f, "Enter
stock sold quantity: ");
System.out.println(valueSell);
if (valueSell==null){return;}
int noOfStockSold =
Integer.parseInt(valueSell);
System.out.println("no of stock sold" +
noOfStockSold);

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()))});
}

Methods public double total_value(double price,int In total_value(double price,int stockLevel)


stockLevel){ method price and the stock level is received as
return price*stockLevel; parameter and we return the multiplication of
} price and stock level in data type as double.
Arrays Allocating memory for 5 objects of products.
product_arr = new Product[5]; Initializing the elements in the product_arr
array.

product_arr[0] = new Product("Ariel Complete


Detergent 1 Kg", 100, 10.10);
product_arr[1] = new Product("Sensodyne
Fresh Mint Tooth Paste 70G", 80, 184);
product_arr[2] = new Product("MamyPoko
Pants Baby Diaper L44 ", 100, 1098);
product_arr[3] = new Product("CG Sarvottam
Premium Lito Apple 400G", 50, 240);
product_arr[4] = new Product("Sensitive Baby
Wipes", 90, 230);
Classes/ public class Product { We have created a class named Product. It
Objects private String name; contains fields which are name,stockLevel
private int stockLevel; ,price with their getter and setter methods as
private double price; well as constructors and also other methods like
buyStock(),sellStock(),set_Price() and
public String getName() { totalvalue() respecticvely.
return name;
} We have used the new keyword and array along
with the constructor Product() of the class
public void setName(String name) { Product to create the objects. Here product_arr
this.name = name; and allProducts are the name of the objects. We
} used them to create 5 objects of the class
Product.
public int getStockLevel() {
return stockLevel;
}

public void setStockLevel(int stockLevel) {


this.stockLevel = stockLevel;
}

public double getPrice() {


return price;
}

public void setPrice(double price) {


this.price = price;
}

public Product(String aName, int aStockLevel,


double aPrice ){
this.name=aName;
this.stockLevel=aStockLevel;
this.price=aPrice;
}

public double buyStock(int NoOfStockBought ){

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;
}
}

public double set_Price(double newPrice ){


this.price=newPrice;
return price;
}
public double total_value(double price,int
stockLevel){
return price*stockLevel;
}

Product[] product_arr = new Product[5];


Product[] allProducts = GUI.product_arr;

You might also like