this program helps me to understand Object, method, constructor.
class Dmart{
String product_name;
int product_price;
int product_discountpercentage;
int product_discountprice;
int finalprice;
public Dmart(String name ,int price ,int discountpercentage){
this.product_name=name;
this.product_price=price;
this.product_discountpercentage=discountpercentage;
}
public static void main(String[] args){
Dmart product1=new Dmart("shop" , 50 , 8);
Dmart product2=new Dmart("chips" , 30 , 5);
Dmart product3=new Dmart("Choclate" , 200 , 2);
Dmart product4=new Dmart("Biscut" , 15 , 7);
Dmart product5=new Dmart("HotChips" , 25 , 12);
product1.buy();
product2.buy();
product3.buy();
product4.buy();
product5.buy();
}
public void buy(){
this.product_discountprice = (this.product_price * this.product_discountpercentage) / 100;
this.finalprice=this.product_price-this.product_discountprice;
System.out.println("productname => "+ this.product_name);
System.out.println("proprice => " + this.product_price);
System.out.println("prodiscountpercentage => " + this.product_discountpercentage);
System.out.println("discountprice => " + this.product_discountprice);
System.out.println("finalprice => " + this.finalprice);
}
}
Top comments (0)