Advertisement
timdebuyst

KoffieAutomaatOvererving.java

Mar 21st, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1. package hfdst5;
  2.  
  3. interface Drank {
  4.     public String getNaam();
  5.  
  6.     public double getPrijs();
  7. }
  8.  
  9. class Koffie implements Drank {
  10.     public String getNaam() {
  11.         return "Koffie";
  12.     }
  13.     public double getPrijs() {
  14.         return 1.0;
  15.     }
  16. }
  17.  
  18. class Chocolade implements Drank {
  19.     public String getNaam() {
  20.         return "Chocolade";
  21.     }
  22.  
  23.     public double getPrijs() {
  24.         return 1.3;
  25.     }
  26. }
  27.  
  28. class Tee implements Drank {
  29.     public String getNaam() {
  30.         return "Tee";
  31.     }
  32.  
  33.     public double getPrijs() {
  34.         return 1.5;
  35.     }
  36. }
  37.  
  38. // // //
  39. // decorator
  40. abstract class DrankAddOns implements Drank {
  41.     protected Drank d;
  42.     public DrankAddOns(Drank d) {
  43.         this.d = d;
  44.     }
  45. }
  46.  
  47. class Melk extends DrankAddOns {
  48.     public Melk(Drank d) {
  49.         super(d);
  50.     }
  51.     @Override
  52.     public String getNaam() {
  53.         return "" + d.getNaam()+ " + Melk";
  54.     }
  55.     @Override
  56.     public double getPrijs() {
  57.         return d.getPrijs() + 0.25;
  58.     }
  59. }
  60.  
  61. class Suiker extends DrankAddOns {
  62.     public Suiker(Drank d) {
  63.         super(d);
  64.     }
  65.     @Override
  66.     public String getNaam() {
  67.         return "" + d.getNaam() + " + Suiker";
  68.     }
  69.     @Override
  70.     public double getPrijs() {
  71.         return d.getPrijs() + 0.1;
  72.     }
  73. }
  74.  
  75. // // //
  76. /*
  77. class KoffieMelk extends Koffie {
  78.     public String getNaam() {
  79.         return "Koffie + Melk";
  80.     }
  81.     public double getPrijs() {
  82.         return 1.0 + 0.25;
  83.     }
  84. }
  85.  
  86. class ChocoladeMelk extends Chocolade {
  87.     public String getNaam() {
  88.         return "Chocolade + Melk";
  89.     }
  90.     public double getPrijs() {
  91.         return 1.3 + 0.25;
  92.     }
  93. }
  94.  
  95. class TeeMelk extends Tee {
  96.     public String getNaam() {
  97.         return "Tee + Melk";
  98.     }
  99.     public double getPrijs() {
  100.         return 1.5 + 0.25;
  101.     }
  102. }
  103.  
  104. class KoffieSuiker extends Koffie {
  105.     public String getNaam() {
  106.         return "Koffie + Suiker";
  107.     }
  108.     public double getPrijs() {
  109.         return 1.0 + 0.1;
  110.     }
  111. }
  112.  
  113. class ChocoladeSuiker extends Chocolade {
  114.     public String getNaam() {
  115.         return "Chocolade + Suiker";
  116.     }
  117.     public double getPrijs() {
  118.         return 1.3 + 0.1;
  119.     }
  120. }
  121.  
  122. class TeeSuiker extends Tee {
  123.     public String getNaam() {
  124.         return "Tee + Suiker";
  125.     }
  126.     public double getPrijs() {
  127.         return 1.5 + 0.1;
  128.     }
  129. }
  130.  
  131. class KoffieMelkSuiker extends KoffieMelk {
  132.     public String getNaam() {
  133.         return "Koffie + Melk + Suiker";
  134.     }
  135.     public double getPrijs() {
  136.         return 1.0 + 0.25 + 0.1;
  137.     }
  138. }
  139.  
  140. class ChocoladeMelkSuiker extends ChocoladeMelk {
  141.     public String getNaam() {
  142.         return "Chocolade + Melk + Suiker";
  143.     }
  144.     public double getPrijs() {
  145.         return 1.3 + 0.25 + 0.1;
  146.     }
  147. }
  148.  
  149. class TeeMelkSuiker extends TeeMelk {
  150.     public String getNaam() {
  151.         return "Tee + Melk + Suiker";
  152.     }
  153.     public double getPrijs() {
  154.         return 1.5 + 0.25 + 0.1;
  155.     }
  156. }
  157. */
  158.  
  159. public class KoffieAutomaatOvererving {
  160.     public static void main(String[] args){
  161.         // Drank[] d = {new Koffie(), new ChocoladeMelk(), new TeeMelkSuiker()};
  162.         Drank[] d = {new Koffie(), new Melk(new Chocolade()), new Suiker(new Melk(new Tee()))};
  163.         double p = 0.0;
  164.         for(Drank i:d){
  165.             System.out.println(i.getNaam() +  " : " + i.getPrijs());
  166.             p += i.getPrijs();
  167.         }
  168.         System.out.println("Prijs = "+p);
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement