Skip to content

Instantly share code, notes, and snippets.

@CaglarGonul
Last active December 24, 2015 00:09
Show Gist options
  • Save CaglarGonul/6715062 to your computer and use it in GitHub Desktop.
Save CaglarGonul/6715062 to your computer and use it in GitHub Desktop.
Beverage class which holds the abstract information of a beverage.
package com.v1;
import java.util.ArrayList;
import java.util.List;
public abstract class Beverage {
String description ;
List<ICondiment> condiments = new ArrayList<ICondiment>();
public void addCondiment(ICondiment condiment){
condiments.add(condiment);
}
public int getCondimentCost(){
int cost = 0;
for (ICondiment condCB : condiments) {
cost += condCB.cost();
}
return cost;
}
public String getDescription() {
return description;
}
public abstract int cost();
}
package com.v1;
public class HouseBlend extends Beverage {
public HouseBlend() {
super();
this.description = "This is house blend...";
}
@Override
public int cost() {
return 6 + getCondimentCost();
}
}
package com.v1;
public class DarkRoast extends Beverage {
public DarkRoast() {
super();
this.description = "This is dark roast...";
}
@Override
public int cost() {
return 7 + getCondimentCost();
}
}
package com.v1;
public class Decaf extends Beverage {
public Decaf() {
super();
this.description = "This is decaf. It is decafed...";
}
@Override
public int cost() {
return 8 + getCondimentCost();
}
}
package com.v1;
public class Espresso extends Beverage {
public Espresso() {
super();
this.description = "This is espresso. It is the most expensive...";
}
@Override
public int cost() {
return 10 + getCondimentCost();
}
}
package com.v1;
public interface ICondiment {
int cost();
}
package com.v1.condiments;
import com.v1.ICondiment;
public class Milk implements ICondiment {
@Override
public int cost() {
return 1;
}
}
package com.v1.condiments;
import com.v1.ICondiment;
public class Mocha implements ICondiment{
@Override
public int cost() {
return 3;
}
}
package com.v1.condiments;
import com.v1.ICondiment;
public class Soy implements ICondiment {
@Override
public int cost() {
return 2;
}
}
package com.v1;
import com.v1.condiments.Milk;
import com.v1.condiments.Mocha;
public class Impl {
public static void main(String[] args) {
HouseBlend hb = new HouseBlend();
System.out.println("Add three unit of milk and mocha.");
hb.addCondiment(new Milk());
hb.addCondiment(new Milk());
hb.addCondiment(new Milk());
hb.addCondiment(new Mocha());
System.out.println("House Blend : " + hb.getDescription() + " Cost : " + hb.cost() );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment