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:
- Array square
- Generate number using odd digits
- Alternate Number Difference
- Next Greatest number
- Mark Comparison
- Print the characters in descending order
- Vowels in a fishBowl
- Least offer
- Ascending and descending order
- Mail Domain
- Count repeating words
- Sentence – Convert to upper and lower
- Count consecutive repeating characters
- Zig zag Array
- Pass and Fail Count
- Search a Course
- Average and Grade Calculation
- String – Find and replace the character (first occurrence)
- Sort the first and second half of an array
- Retail Shop
- Palindrome
- Numerology
- InitCap
- Array Compatiblilty
- Sum of the maximum and the minimum element
- String Concatenation
- Find Average Age
- Login
Yo también he pasado por numerosos MLM