In: Computer Science
Grocery Store using Java Arraylist
Requirements:
1. Need Product Class and Stock Class
2. Use arraylist to store/ add product in stock
3. Product must have name, code number and quantity in stock
4. Print list of the products and their stock quantity
5. Search products based on their name 6. Remove product based on their code number
7. Sell product and deduct quantity from stock
8. Stock Class contains methods of search product, add product and check quantity of the product
9. Product Class contains method of get product details
please find the code for the same:
package store;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Stock {
public List<Product> productList=new
ArrayList<Product>();
public static void main(String[] args) {
Stock stock=new Stock();
for(int i=0;i<5;i++) {
Product product=new
Product("Product"+i,"CodeNumber"+i,500+i);
stock.productList.add(product);
}
stock.printProducts();
//add product
Product newproduct=new
Product("Product New","CodeNumber New",800);
stock.addProduct(newproduct);
System.out.println("printing after
adding new product");
stock.printProducts();
//searching
System.out.println("Searched
Product By Name:"+stock.searchProduct("Product
New").getName());
//sell product and deduct
quantity
stock.sellProductAndDeductQuantity(newproduct, 15);
//check quantity of product
System.out.println("Quantity of New
Product after
selling:"+stock.checkQuantityOfProduct(newproduct));
//remove product
stock.removeProduct(newproduct.getCodeNumber());
System.out.println("printing after
removing new product");
stock.printProducts();
}
public void printProducts() {
for(Product product:productList)
{
product.getProductdetail();
}
}
public Product searchProduct(String name) {
for(Product product:productList)
{
if(product.getName().equals(name)) {
return product;
}
}
return null;
}
public void removeProduct(String codeNumber)
{
Iterator<Product> itr
=productList.iterator();
while(itr.hasNext()) {
Product
pro=(Product)itr.next();
if(pro.getCodeNumber().equals(codeNumber)) {
itr.remove();
}
}
}
public void addProduct(Product pro) {
productList.add(pro);
}
public Integer checkQuantityOfProduct(Product pro)
{
for(Product product:productList)
{
if(product.getName().equals(pro.getName())&&product.getCodeNumber().equals(pro.getCodeNumber()))
{
return product.getStockQuantity();
}
}
return null;
}
public boolean sellProductAndDeductQuantity(Product
pro, Integer quantity) {
for(Product product:productList)
{
if(product.getName().equals(pro.getName())&&product.getCodeNumber().equals(pro.getCodeNumber()))
{
if(product.getStockQuantity()>=quantity)
{
product.setStockQuantity(product.getStockQuantity()-quantity);
return true;
}
}
}
return false;
}
}
package store;
public class Product {
private String name;
private String codeNumber;
private Integer stockQuantity;
public Product(String name,String codeNumber,Integer
stockQuantity) {
this.name=name;
this.codeNumber=codeNumber;
this.stockQuantity=stockQuantity;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCodeNumber() {
return codeNumber;
}
public void setCodeNumber(String codeNumber)
{
this.codeNumber = codeNumber;
}
public Integer getStockQuantity() {
return stockQuantity;
}
public void setStockQuantity(Integer stockQuantity)
{
this.stockQuantity =
stockQuantity;
}
public void getProductdetail() {
System.out.println("Product
Name:"+getName()+" |Product Code Number:"+getCodeNumber()+"
|Product Quantity:"+getStockQuantity());
}
}