Java Program of Retail Shop

Question:

A Retail shop wants to maintain the product availability in their shop.

Create a Class Shop with the private attributes shop name, shop address, products(string array). Include Constructor to initialize the value for these attributes and appropriate getter and setter method if needed. 

Write the following method in the class:

public boolean checkProductAvailability(String productname) – this method should take the product name as argument and check whether that product is available in the shop or not (Product name to be searched is case insensitive). If the product is available, function should return true, else return false.

Write the main method to test the application.

Note: Always number of products should be greater than zero. 

Sample Input 1:

Enter the shopname:
TMD
Enter the address:
Chennai
Enter number of products:

4
Laptop
Camera
Pendrive
Mobile
Enter the product to be searched:

Camera

Sample Output 1:

Product is available at TMD, Chennai.

Sample Input 2:
Enter the shopname:
TMD
Enter the address:
Chennai
Enter no of products:
4
Laptop
Camera
Pendrive
Mobile
Enter the product to be searched:
Telephone

Sample Output 2:

Product is not available at TMD, Chennai.

Code:

Shop.java

import java.util.*;
public class Shop 
{   
    private String shopName;
    private String shopAddress;
    private String[] products;
    
    public Shop(String shopName, String shopAddress, String[] products)
    {
        this.shopName=shopName;
        this.shopAddress=shopAddress;
        this.products=products;
    }
    
    public boolean checkProductAvailability(String productname)
    {int len=products.length;
        for(int i=0;i<len;i++)
        {
            if(productname.equalsIgnoreCase(products[i]))
            {
                return true;
            }
        }
        return false;
    }
    
    public String getShopName()
    {
        return this.shopName;
    }
    
    public String getShopAddress()
    {
        return this.shopAddress;
    }
    
    public String[] getProducts()
    {
        return this.products;
    }

    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
        
        System.out.println("Enter the shopname:");
        String shop_name=sc.nextLine();
        
        System.out.println("Enter the address:");
        String shop_address=sc.nextLine();
        
        System.out.println("Enter number of products:");
        int no_products=sc.nextInt();
        sc.nextLine();
        String[] products=new String[no_products];
        for(int i=0;i<no_products;i++)
        {
            products[i]=sc.nextLine();
        }
        
        Shop obj=new Shop(shop_name, shop_address, products);
        
        System.out.println("Enter the product to be searched:");
        String srch=sc.nextLine();
        
        if(obj.checkProductAvailability(srch))
        {
            System.out.println("Product is available at "+obj.getShopName()+", "+obj.getShopAddress());
        }
        else
        {
            System.out.println("Product is not available at "+obj.getShopName()+", "+obj.getShopAddress());
        }
    }
}

Recommended:

  1. Array square
  2. Generate number using odd digits
  3. Alternate Number Difference
  4. Next Greatest number
  5. Mark Comparison
  6. Print the characters in descending order
  7. Vowels in a fishBowl
  8. Least offer
  9. Ascending and descending order
  10. Mail Domain
  11. Count repeating words
  12. Sentence – Convert to upper and lower
  13. Count consecutive repeating characters
  14. Zig zag Array
  15. Pass and Fail Count
  16. Search a Course
  17. Average and Grade Calculation
  18. String – Find and replace the character (first occurrence)
  19. Sort the first and second half of an array
  20. Retail Shop
  21. Palindrome
  22. Numerology
  23. InitCap
  24. Array Compatiblilty
  25. Sum of the maximum and the minimum element
  26. String Concatenation
  27. Find Average Age
  28. Login
  29.  

3 thoughts on “Java Program of Retail Shop”

Leave a Comment

Your email address will not be published. Required fields are marked *

x