Skip to content

Commit

Permalink
Visitor Design Pattern - Visitor Cook party project
Browse files Browse the repository at this point in the history
Cook visits at party location for cooking
  • Loading branch information
premaseem committed Mar 13, 2018
1 parent 05e7f51 commit 72268f6
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 26 deletions.
13 changes: 0 additions & 13 deletions pattern/src/com/premaseem/Client.java

This file was deleted.

13 changes: 0 additions & 13 deletions patternBonus/src/com/premaseem/Client.java

This file was deleted.

60 changes: 60 additions & 0 deletions patternBonus/src/com/premaseem/ClientFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.premaseem;

import java.util.Scanner;

public class ClientFile {

public static void main(String[] args) {

System.out.println("Welcome to Party host example which uses visitor pattern ");
Scanner scan = new Scanner(System.in);
Party party;
CookVisitorI visitorCook;
int repeatRunFlag = 1;
while (repeatRunFlag == 1) {
System.out.println("Which party do you want to host ");
System.out.println("press 1 for Week end party ");
System.out.println("press 2 for Week day party ");
int tvType = scan.nextInt();
if (tvType == 1) {
party = new LoudParty();
} else {
party = new CalmParty();
}

System.out.println("How would you want to manage cooking of food ");
System.out.println(" Press 1 for a visitor Veg Cook ");
System.out.println(" Press 2 for a visitor Non- Veg Cook ");
System.out.println(" Press 3 for in house cooking (no visitor) ");

int type = scan.nextInt();
try {
switch (type) {
case 1:
visitorCook = new VegCookVisitor();
party.accept(visitorCook);
break;
case 2:
visitorCook = new NonVegCookVisitor();
party.accept(visitorCook);
break;
case 3:
party.cookInHouse();
break;
}


} catch (Exception e1) {
e1.printStackTrace();
}
System.out.println("=============================");
System.out.println("Press 1 to Repeat .... ");
try {
repeatRunFlag = scan.nextInt();
} catch (Exception e) {
repeatRunFlag = 0;
}

}
}
}
44 changes: 44 additions & 0 deletions patternBonus/src/com/premaseem/CookVisitorI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.premaseem;

public interface CookVisitorI {
void cook (LoudParty loudParty);
void cook (CalmParty calmParty);
}

class VegCookVisitor implements CookVisitorI{

@Override
public void cook(LoudParty loudParty) {
loudParty.meal = "Spicy Vegetables";
loudParty.drink = "Fruit Beer";
loudParty.music = "Loud music";
loudParty.getPartyDetail();
}

@Override
public void cook(CalmParty calmParty) {
calmParty.meal = "Boiled Vegetables";
calmParty.drink = "Fruit Juice";
calmParty.music = "Meditation Music";
calmParty.getPartyDetail();
}
}

class NonVegCookVisitor implements CookVisitorI{

@Override
public void cook(LoudParty loudParty) {
loudParty.meal = "Spicy chicken";
loudParty.drink = "Beer";
loudParty.music = "High Beat music";
loudParty.getPartyDetail();
}

@Override
public void cook(CalmParty calmParty) {
calmParty.meal = "Non spicy chicken";
calmParty.drink = "Fruit Beer";
calmParty.music = "Meditation music";
calmParty.getPartyDetail();
}
}
46 changes: 46 additions & 0 deletions patternBonus/src/com/premaseem/Party.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.premaseem;

public abstract class Party {

String music = " No Music";
String meal = " empty";
String drink = "plane water ";
CookVisitorI visitor = null;

abstract void accept(CookVisitorI visitor);


String getPartyDetail() {
String detials = visitor!= null ?visitor.getClass().getSimpleName() : "in house cooking" + " organized " + this.getClass().getSimpleName() + " has Music : " + music + " with Drink :"
+ drink + " & Meal : " + meal;
System.out.println(detials);
return detials;
}

public void cookInHouse() {
meal = "Spicy Vegetables";
drink = "in house drink";
music = "DJ music";
getPartyDetail();
}

}

class LoudParty extends Party {

@Override
void accept(CookVisitorI visitor) {
this.visitor = visitor;
visitor.cook(this);
}
}

class CalmParty extends Party {

@Override
void accept(CookVisitorI visitor) {
this.visitor = visitor;
visitor.cook(this);
}
}

0 comments on commit 72268f6

Please # to comment.