In this post, we will learn Retail Shop Program in java Programming language.
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:–
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()); } } }
public class Student { private int id; private String name; private int[] marks; private float average; private char grade; public void setId(int id) { this.id=id; } public int getId() { return this.id; } public void setName(String name) { this.name=name; } public String getName() { return this.name; } public void setMarks(int[] marks) { this.marks=marks; } public int[] getMarks() { return this.marks; } public void calculateAvg() { float sum=0; for(int i=0;i<this.marks.length;i++) { sum+=this.marks[i]; } average=(float)(sum/(this.marks.length)); setAverage(average); } public void findGrade() {int flag=0; for(int i=0;i<this.marks.length;i++) { if(this.marks[i]<50) { flag++; } } if(flag>0) { grade='F'; } else { if(this.average>=80 && this.average<=100) { grade='O'; } else if(this.average>=50 && this.average<=79) { grade='A'; } else { grade='F'; } } setGrade(grade); } public void setAverage(float average) { this.average=average; } public void setGrade(char grade) { this.grade=grade; } public float getAverage() { return this.average; } public char getGrade() { return this.grade; } }