Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.3 kB
7
Indexable
Never
package shop;
import java.util.*;

class Product
{
  String pName;
  int pId;
  int quantity;
  double price;
 
  Product(String pName, int pId, int quantity, double price)
  {
    this.pName = pName;
    this.pId = pId;
    this.quantity = quantity;
    this.price = price;
  }
}
 
class OnlineShop 
{
  public static void main(String[] args) 
  {
    Product[] products = new Product[4];
    Product[] cart = new Product[4];
    String name="";
    int id, quantity;
    double price=0;
 
    String choice;
    int index = 0;
    java.util.Scanner sc = new Scanner(System.in);
 
    System.out.println("As a shopkeeper add products to your store :");
    while (true) 
    {
      if (index > 3)
        break;
      System.out.println();
      System.out.print("Add Product (y/n): ");
      choice = sc.next();
 
      if (choice.equals("n"))
        break;
      else 
      {
        System.out.print("Product Name: ");
        name = sc.next();
        System.out.print("Product Id: ");
        id = sc.nextInt();
        System.out.print("Quantity: ");
        quantity = sc.nextInt();
        System.out.print("Price of item: ");
        price = sc.nextDouble();
      }
      products[index] = new Product(name, id, quantity, price);
      index++;
    }
    
    System.out.println("All available products are :");
    for(int i=0;i<index;i++) 
    {
    	System.out.println(products[i].pId+"\t"+products[i].pName+"\t"+products[i].quantity+"\t"+products[i].price);
    }
    
    System.out.println("As a buyer add products to your cart :");
    int cartIndex=0;
    while(true) 
    {
    	if(cartIndex>3) 
    		break;

    	System.out.println();
    	System.out.println("Add products to cart(y/n);");
    	choice=sc.next();
    	
    	if(choice.equals("n"))
    		break;
    	else 
    	{
    		System.out.println("Product Id :");
    		id=sc.nextInt();
    		
    		boolean itemAvailable =false;
    		for(int i=0;i<index;i++)
    			if(products[i].pId==id) 
    			{
    				name=products[i].pName;
    				price=products[i].price;
    				itemAvailable=true;
    			}
    		
    		if(!itemAvailable) 
    		{
    			System.out.println("Invalid product id, Try again!!!");
    			continue;
    		}
    		
    		System.out.println("Quantity :");
    		quantity=sc.nextInt();
    		
    		boolean quantityAvailable=false;
    		for(int i=0;i<index;i++)
    		
    		
    			if(products[i].quantity>=quantity)
    				quantityAvailable=true;
    		
    		if(!quantityAvailable) 
    		{
    			System.out.println("This amount is unavailable, Try again!!!");
    			continue;
    		}
    		cart[cartIndex]=new Product(name,id,quantity,price);
    	}
    	cartIndex++;
    }
    	
    sc.close();
    
    double totalPrice = 0;
 
    System.out.println();
    System.out.println("Id\t Name\t Quantity\t Price");
    for (int i = 0; i < cartIndex; i++)
    {
      totalPrice = totalPrice + (cart[i].price * cart[i].quantity);
      System.out.println(cart[i].pId +"\t"+ cart[i].pName +"\t"+ cart[i].quantity +"\t"+ cart[i].price);
    }
    
   
    
    
    System.out.println();
    System.out.println("Total Price: " + totalPrice);
  }
}