DEV Community

Divya Divya
Divya Divya

Posted on

Java Constructor Program

Product Program

class Dmart{ 

String productname;
int proprice;
boolean prodiscount;
int prodiscountpercentage;
int discountprice;
int finalprice;


public Dmart(String name ,int price ,boolean discount,int discountpercentage){

this.productname=name;
this.proprice=price;
this.prodiscount=discount;
this.prodiscountpercentage=discountpercentage;
}


public static void main(String[] args){

Dmart product1=new Dmart("Mobile" , 70000 ,true , 8);
Dmart product2=new Dmart("Watch" , 300 , true , 5);
Dmart product3=new Dmart("Choclate" , 450 , true , 2);
Dmart product4=new Dmart("Biscut"  ,  30 , true , 7);
Dmart product5=new Dmart("HotChips" , 25 , true ,12);

product1.buy();
product2.buy();
product3.buy();
product4.buy();
product5.buy();

}
public void buy(){

this.discountprice = (this.proprice * this.prodiscountpercentage) / 100;
this.finalprice=this.proprice-this.discountprice;

System.out.println("productname => "+ this.productname);
System.out.println("proprice => " + this.proprice);
System.out.println("prodiscount => "  + this. prodiscount);
System.out.println("prodiscountpercentage => " + this.prodiscountpercentage);
System.out.println("discountprice => " + this.discountprice);
System.out.println("finalprice => " + this.finalprice);

}

}

Enter fullscreen mode Exit fullscreen mode

Output:

Top comments (0)