added lab6 ex1

This commit is contained in:
Louis Heredero 2024-10-14 09:39:40 +02:00
parent 061a475a6b
commit 6acdcaffa7
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
4 changed files with 111 additions and 0 deletions

View File

@ -0,0 +1,32 @@
package lab6_template_method.ex1;
public class HellsKitchen extends Restaurant {
public HellsKitchen(String name) {
super("HK " + name);
}
@Override
public void prepareVegetables() {
log("Preparing nice salad");
}
@Override
public void prepareMainMenu() {
log("Creating flavorful dish");
}
@Override
public void prepareCheese() {
log("Preparing finest cheese");
}
@Override
public void prepareDessert() {
log("Giving birth to awesome dessert");
}
@Override
public void prepareCoffee() {
log("Brewing the best coffee");
}
}

View File

@ -0,0 +1,32 @@
package lab6_template_method.ex1;
public class MacDonalds extends Restaurant {
public MacDonalds(String name) {
super("McDonalds " + name);
}
@Override
public void prepareVegetables() {
log("Vegetables ? What's that ?");
}
@Override
public void prepareMainMenu() {
log("McChicken incoming");
}
@Override
public void prepareCheese() {
log("Hmmm, creamy cheese");
}
@Override
public void prepareDessert() {
log("Preparing McFlurry");
}
@Override
public void prepareCoffee() {
log("Preparing short and hot coffee");
}
}

View File

@ -0,0 +1,11 @@
package lab6_template_method.ex1;
public class Main {
public static void main(String[] args) {
Restaurant r1 = new MacDonalds("Sion");
Restaurant r2 = new HellsKitchen("London");
r1.prepareDinner();
r2.prepareDinner();
}
}

View File

@ -0,0 +1,36 @@
package lab6_template_method.ex1;
public abstract class Restaurant {
private String name;
public Restaurant(String name) {
this.name = name;
}
public final void prepareDinner() {
serveWater();
prepareVegetables();
prepareMainMenu();
prepareCheese();
prepareDessert();
prepareCoffee();
cleanTable();
}
protected void log(String msg) {
System.out.println("[" + name + "] " + msg);
}
protected final void serveWater() {
log("Serving water");
}
protected final void cleanTable() {
log("Cleaning table");
}
protected abstract void prepareVegetables();
protected abstract void prepareMainMenu();
protected abstract void prepareCheese();
protected abstract void prepareDessert();
protected abstract void prepareCoffee();
}