File tree Expand file tree Collapse file tree
patternBonus/src/com/premaseem
pattern/src/com/premaseem Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1010public class Client {
1111 public static void main (String [] args ) {
1212 System .out .println ("Alexa welcomes you to Artificial intelligence app designed using Interpreter Design pattern :-) " );
13- Scanner scanner = new Scanner (System .in );
13+ Scanner scanner1 = new Scanner (System .in );
14+ Scanner scanner2 = new Scanner (System .in );
1415
1516 // User input block
1617 String repeatRunFlag = "yes" ;
1718 while (!repeatRunFlag .equalsIgnoreCase ("no" )) {
1819 System .out .println ("\n Please ask your question related to voting ..." );
19- String question = scanner .nextLine ();
20- Boolean result =true ;
21- String voteEligible = result ? "Yes ! can vote :-) " :"No ! cannot vote :-)" ;
20+ String question = scanner1 .nextLine ();
21+
22+ // get Voting context
23+ Context votingContext = new VotingContext ();
24+
25+ // get Rules or grammar around context
26+ Expression ruleExpression = votingContext .getRuleExpression ();
27+
28+ // question is interpreted based on context and rules
29+ Boolean result = ruleExpression .interpret (question );
2230
31+ // print Results
32+ String voteEligible = result ? "Yes ! can vote :-) " :"No ! cannot vote :-)" ;
2333 System .out .println ("Alexa answered: " + voteEligible );
34+
35+ // Ask from another questions
2436 System .out .println ("Do you have another questions ? yes / no " );
25- repeatRunFlag = scanner .next ();
37+ repeatRunFlag = scanner2 .next ();
2638 }
2739 }
2840}
41+
42+
Original file line number Diff line number Diff line change 1+ package com .premaseem ;
2+
3+ /*
4+ @author: Aseem Jain
5+ @title: Design Patterns with Java 9
6+ @link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+ */
8+ interface Context {
9+ Expression getRuleExpression ();
10+ }
11+
12+ class VotingContext implements Context {
13+
14+ @ Override
15+ public Expression getRuleExpression () {
16+ Expression condition_1 = new TerminalExpression ("citizen" );
17+ Expression condition_2 = new TerminalExpression ("18" );
18+ return new AndExpression (condition_1 , condition_2 );
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ package com .premaseem ;
2+
3+ /*
4+ @author: Aseem Jain
5+ @title: Design Patterns with Java 9
6+ @link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+ */
8+ interface Expression {
9+ public boolean interpret (String context );
10+ }
11+
12+ /**
13+ * Terminal Expression is used in other expressions for interpretation
14+ */
15+ class TerminalExpression implements Expression {
16+
17+ private String data ;
18+
19+ public TerminalExpression (String data ){
20+ this .data = data ;
21+ }
22+
23+ @ Override
24+ public boolean interpret (String context ) {
25+ if (context .contains (data )){
26+ return true ;
27+ }
28+ return false ;
29+ }
30+ }
31+
32+ class AndExpression implements Expression {
33+
34+ private Expression expr1 = null ;
35+ private Expression expr2 = null ;
36+
37+ public AndExpression (Expression expr1 , Expression expr2 ) {
38+ this .expr1 = expr1 ;
39+ this .expr2 = expr2 ;
40+ }
41+
42+ @ Override
43+ public boolean interpret (String context ) {
44+ return expr1 .interpret (context ) && expr2 .interpret (context );
45+ }
46+ }
47+
48+ class OrExpression implements Expression {
49+
50+ private Expression expr1 = null ;
51+ private Expression expr2 = null ;
52+
53+ public OrExpression (Expression expr1 , Expression expr2 ) {
54+ this .expr1 = expr1 ;
55+ this .expr2 = expr2 ;
56+ }
57+
58+ @ Override
59+ public boolean interpret (String context ) {
60+ return expr1 .interpret (context ) || expr2 .interpret (context );
61+ }
62+ }
63+
Load diff This file was deleted.
You can’t perform that action at this time.
0 commit comments