Question

In: Computer Science

Grocery Store using Java Arraylist Requirements: 1. Need Product Class and Stock Class 2. Use arraylist...

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

Solutions

Expert Solution

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

}


Related Solutions

JAVA In this PoD you will use an ArrayList to store different pet names (there are...
JAVA In this PoD you will use an ArrayList to store different pet names (there are no repeats in this list). This PoD can be done in your demo program (where your main method is) – you don’t have to create a separate class for today. Details Create an arraylist of Strings, then using a Scanner object you will first read in a number that will tell you how many pet names (one word) you will add to the arraylist....
Write a java class for a Priority Queue. Use an arraylist, and include enque, deque, and...
Write a java class for a Priority Queue. Use an arraylist, and include enque, deque, and a method to get all the values of the queue. (This is not writing a file implementing the java class PriorityQueue, but rather you are writing a program that is a priority queue).
how to use java ArrayList to get the result? removing consecutive duplicates eg [1 2 2...
how to use java ArrayList to get the result? removing consecutive duplicates eg [1 2 2 3 2 2 1] ------->[1 2 3 2 1]
for the following JAVA program Deals of the grocery store. The infomations on the flyer: 1.By...
for the following JAVA program Deals of the grocery store. The infomations on the flyer: 1.By buying  items under $250, the client will get a  10% off in total and get double point 2.By buying items between $250-500 (both included),the client will get 15% off in total and get double points. 3.By buying items over 500, the client will get 20% off in total and get triple points. Fish  is not included in the total discount price. The calculation of the points is...
CSC202-001 Clark Problem 1: For this class Java’s ArrayList is used to store a collection of...
CSC202-001 Clark Problem 1: For this class Java’s ArrayList is used to store a collection of different elements. Please design and implement your own array-based version of MyArrayList that stores items of type String and supports the following operations: +MyArrayList() //a constructor that creates an empty array list +emptyHere() : Boolean // Determines whether the list is empty or not +sizeHere() : integer // Returns the # of items stored in the array list +add(in item : String) : void...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task>...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task> list;//make private array public ToDoList() { //this keyword refers to the current object in a method or constructor this.list = new ArrayList<>(); } public Task[] getSortedList() { Task[] sortedList = new Task[this.list.size()];//.size: gives he number of elements contained in the array //fills array with given values by using a for loop for (int i = 0; i < this.list.size(); i++) { sortedList[i] = this.list.get(i);...
JAVA: when input is type ArrayList<ArrayList<Integer>> how to use java to get this solution [ [a,b,c]...
JAVA: when input is type ArrayList<ArrayList<Integer>> how to use java to get this solution [ [a,b,c] , [d,e], [f] ] ----> [ [a,d,f], [a,e,f], [b,d,f], [b,e,f], [c,d,f], [c,e,f]] [ [a,b], [a,b,c]] ----->[[a,a],[a,b],[a,c], [b,a],[b,b],[b,c] assuming abc are integer Thanks in advance!
A Java question. Write the class Staff. It contains methods that manipulate an ArrayList of Strings...
A Java question. Write the class Staff. It contains methods that manipulate an ArrayList of Strings representing the names of staff members. The constructor takes an ArrayList of String names as a parameter. In addition to the constructor, you need to implement the following methods The methods 1. public boolean equals(Staff other) - Determines if the other Staff contains all the same elements in the same order as this Staff 2. public boolean sameContents(Staff other) - Determines if the other...
in java please: Create an ArrayListReview class with one data field of ArrayList and one with...
in java please: Create an ArrayListReview class with one data field of ArrayList and one with LinkedList with the generic type passed to the class. (2 point) Create a constructor that populate an array list and the LinkedList filled with the generic type through inserting new elements into the specified location index-i in the list. (2 points)
In java, please Create an ArrayListReview class with one data field of ArrayList with the generic...
In java, please Create an ArrayListReview class with one data field of ArrayList with the generic type passed to the class. (1 point) Create a constructor that populate an array list filled with the generic type through inserting new elements into the specified location index-i in the list. (1 point) Implement mergeSort using a list and recursion. (2 points) Write the main method to test your program and use System.nanoTime() to find out the speed of each step of your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT