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

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...
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....
Define a class Product to hold the name and price of items in a grocery store....
Define a class Product to hold the name and price of items in a grocery store. Encapsulate the fields and provide getters and setters. Create an application for a grocery store to calculate the total bill for each customer. Such program will read a list of products purchased and the quantity of each item. Each line in the bill consists of: ProductName ProductPrice Quantity/Weight The list of items will be terminated by the “end” keyword. The data will look like...
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]
USE JAVA Develop the classes for the following requirements: 1. A class named Employee (general, for...
USE JAVA Develop the classes for the following requirements: 1. A class named Employee (general, for college) 2. A class named Instructor (more specific, for college) 3. A class named Staff (more specific, for college, HR officer, Marking staff) Tasks: 1. Figure out the relationships among the classes; 2. Determine the abstract class, and the child classes; 3. For the abstract class, determine at least one abstract method; 4. Each class should at least two data members and one extra...
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!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT