Skip to content

Instantly share code, notes, and snippets.

@rohanjai777
Created November 8, 2022 19:43
Show Gist options
  • Save rohanjai777/61bb41058b29df87605a8c0f719f4a21 to your computer and use it in GitHub Desktop.
Save rohanjai777/61bb41058b29df87605a8c0f719f4a21 to your computer and use it in GitHub Desktop.
interface Icecream{
int getPrice();
void printComposition();
}
class ChocoWafer implements Icecream{ //make a ChocoWafer class
private Icecream icecream; //already made cone if any
private int price = 20;
ChocoWafer(){} //if no cone till now
ChocoWafer(Icecream icecream){ //if one cone is already there
if(icecream == null){
throw new NullPointerException();
}
this.icecream = icecream;
}
public int getPrice(){
int cost = price;
if(icecream!=null){
cost+=icecream.getPrice();
}
return cost;
}
public void printComposition(){
if(icecream != null){
icecream.printComposition();
}
System.out.println(" ChocoWafer ");
}
}
class VanilaWafer implements Icecream{ //make a ChocoWafer class
private Icecream icecream; //already made cone if any
private int price = 10;
VanilaWafer(){} //if no cone till now
VanilaWafer(Icecream icecream){ //if one cone is already there
if(icecream == null){
throw new NullPointerException();
}
this.icecream = icecream;
}
public int getPrice(){
int cost = price;
if(icecream!=null){
cost+=icecream.getPrice();
}
return cost;
}
public void printComposition(){
if(icecream != null){
icecream.printComposition();
}
System.out.println(" VanilaWafer ");
}
}
//----------------------------------------
class ChocoScoop implements Icecream{
private Icecream icecream; //already made cone if any
private int price = 30;
ChocoScoop(Icecream icecream){ //if one cone is already there
if(icecream == null){
throw new NullPointerException();
}
this.icecream = icecream;
}
public int getPrice(){
int cost = price;
if(icecream!=null){
cost+=icecream.getPrice();
}
return cost;
}
public void printComposition(){
if(icecream != null){
icecream.printComposition();
}
System.out.println(" ChocoScoop ");
}
}
class VanilaScoop implements Icecream{
private Icecream icecream; //already made cone if any
private int price = 20;
VanilaScoop(Icecream icecream){ //if one cone is already there
if(icecream == null){
throw new NullPointerException();
}
this.icecream = icecream;
}
public int getPrice(){
int cost = price;
if(icecream!=null){
cost+=icecream.getPrice();
}
return cost;
}
public void printComposition(){
if(icecream != null){
icecream.printComposition();
}
System.out.println(" VanilaScoop ");
}
}
//----------------------------------------
public class MakeIcecream{
public static void main(String[] args){
Icecream icecream = new VanilaScoop(new ChocoScoop(new VanilaWafer(new ChocoWafer())));
System.out.println(icecream.getPrice());
icecream.printComposition();
}
}
//Output
80
ChocoWafer
VanilaWafer
ChocoScoop
VanilaScoop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment