Facebook
From Krkr, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 155
  1. public class HelloWorld{
  2.  
  3.      public static void main(String []args){
  4.         System.out.println("Hello World");
  5.        
  6.         Product p1 = new Product(1, 50 , 9999);
  7.         Product p2 = new Product(2 , 990);
  8.         Product p3 = new Product(3 , 40 , 1);
  9.         System.out.println(p1);
  10.         System.out.println(p2.toString());
  11.         System.out.println(p3.toString());
  12.         p3.sell(50);
  13.         p1.sell(50);
  14.      }
  15. }
  16.  
  17. class Product{
  18.     private int ID;
  19.     private int price;
  20.     private int stock;
  21.     public Product(int ID, int price, int stock)
  22.     {
  23.         this.ID = ID ;
  24.         this.price = price;
  25.         this.stock = stock;
  26.  
  27.     }
  28.  
  29.     public Product(int ID, int price)
  30.     {
  31.         this.ID = ID ;
  32.         this.price = price ;
  33.         stock = 0;
  34.     }
  35.  
  36.     public String toString ()
  37.     {
  38.         return "This Products's ID is " + ID + ", Price is " + price + " , Stock is " + stock;
  39.     }
  40.    
  41.     public void sell (int a)
  42.     {
  43.         if (stock >= a)
  44.         {
  45.             System.out.println(a * price);
  46.         }
  47.         else
  48.         {
  49.             System.out.println("Insufficient Stock");
  50.         }
  51.         stock = stock - a;
  52.     }  
  53.    
  54. }