Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hfdst5;
- interface Drank {
- public String getNaam();
- public double getPrijs();
- }
- class Koffie implements Drank {
- public String getNaam() {
- return "Koffie";
- }
- public double getPrijs() {
- return 1.0;
- }
- }
- class Chocolade implements Drank {
- public String getNaam() {
- return "Chocolade";
- }
- public double getPrijs() {
- return 1.3;
- }
- }
- class Tee implements Drank {
- public String getNaam() {
- return "Tee";
- }
- public double getPrijs() {
- return 1.5;
- }
- }
- // // //
- // decorator
- abstract class DrankAddOns implements Drank {
- protected Drank d;
- public DrankAddOns(Drank d) {
- this.d = d;
- }
- }
- class Melk extends DrankAddOns {
- public Melk(Drank d) {
- super(d);
- }
- @Override
- public String getNaam() {
- return "" + d.getNaam()+ " + Melk";
- }
- @Override
- public double getPrijs() {
- return d.getPrijs() + 0.25;
- }
- }
- class Suiker extends DrankAddOns {
- public Suiker(Drank d) {
- super(d);
- }
- @Override
- public String getNaam() {
- return "" + d.getNaam() + " + Suiker";
- }
- @Override
- public double getPrijs() {
- return d.getPrijs() + 0.1;
- }
- }
- // // //
- /*
- class KoffieMelk extends Koffie {
- public String getNaam() {
- return "Koffie + Melk";
- }
- public double getPrijs() {
- return 1.0 + 0.25;
- }
- }
- class ChocoladeMelk extends Chocolade {
- public String getNaam() {
- return "Chocolade + Melk";
- }
- public double getPrijs() {
- return 1.3 + 0.25;
- }
- }
- class TeeMelk extends Tee {
- public String getNaam() {
- return "Tee + Melk";
- }
- public double getPrijs() {
- return 1.5 + 0.25;
- }
- }
- class KoffieSuiker extends Koffie {
- public String getNaam() {
- return "Koffie + Suiker";
- }
- public double getPrijs() {
- return 1.0 + 0.1;
- }
- }
- class ChocoladeSuiker extends Chocolade {
- public String getNaam() {
- return "Chocolade + Suiker";
- }
- public double getPrijs() {
- return 1.3 + 0.1;
- }
- }
- class TeeSuiker extends Tee {
- public String getNaam() {
- return "Tee + Suiker";
- }
- public double getPrijs() {
- return 1.5 + 0.1;
- }
- }
- class KoffieMelkSuiker extends KoffieMelk {
- public String getNaam() {
- return "Koffie + Melk + Suiker";
- }
- public double getPrijs() {
- return 1.0 + 0.25 + 0.1;
- }
- }
- class ChocoladeMelkSuiker extends ChocoladeMelk {
- public String getNaam() {
- return "Chocolade + Melk + Suiker";
- }
- public double getPrijs() {
- return 1.3 + 0.25 + 0.1;
- }
- }
- class TeeMelkSuiker extends TeeMelk {
- public String getNaam() {
- return "Tee + Melk + Suiker";
- }
- public double getPrijs() {
- return 1.5 + 0.25 + 0.1;
- }
- }
- */
- public class KoffieAutomaatOvererving {
- public static void main(String[] args){
- // Drank[] d = {new Koffie(), new ChocoladeMelk(), new TeeMelkSuiker()};
- Drank[] d = {new Koffie(), new Melk(new Chocolade()), new Suiker(new Melk(new Tee()))};
- double p = 0.0;
- for(Drank i:d){
- System.out.println(i.getNaam() + " : " + i.getPrijs());
- p += i.getPrijs();
- }
- System.out.println("Prijs = "+p);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement