Skip to content

Commit 9c1b402

Browse files
committed
Interpreter Pattern - The main cause of Y2K issue was date format, in this exmaple we made an interpreter which can intrepret any date format
1 parent d1eb93e commit 9c1b402

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

InterpreterPattern/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.premaseem.interpreterPattern;
2+
3+
import java.util.Date;
4+
5+
public abstract class AbstractExpression {
6+
7+
public abstract void evaluate(Context context);
8+
}
9+
10+
class DayExpression extends AbstractExpression
11+
{
12+
13+
@Override
14+
public void evaluate( Context context )
15+
{
16+
String expression = context.getExpression();
17+
Date date = context.getDate();
18+
Integer day = new Integer(date.getDate());
19+
String tempExpression = expression.replaceAll("DD", day.toString());
20+
context.setExpression(tempExpression);
21+
}
22+
}
23+
24+
class MonthExpression extends AbstractExpression
25+
{
26+
27+
@Override
28+
public void evaluate( Context context )
29+
{
30+
String expression = context.getExpression();
31+
Date date = context.getDate();
32+
Integer month = new Integer(date.getMonth()+1);
33+
String tempExpression = expression.replaceAll("MM", month.toString());
34+
context.setExpression(tempExpression);
35+
}
36+
37+
}
38+
39+
class YearExpression extends AbstractExpression
40+
{
41+
42+
@Override
43+
public void evaluate( Context context )
44+
{
45+
String expression = context.getExpression();
46+
Date date = context.getDate();
47+
Integer year = new Integer(date.getYear() + 1900);
48+
String tempExpression = expression.replaceAll("YYYY", year.toString());
49+
context.setExpression(tempExpression);
50+
}
51+
52+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.premaseem.interpreterPattern;
2+
3+
import java.util.ArrayList;
4+
import java.util.Date;
5+
import java.util.Scanner;
6+
7+
public class ClientFile {
8+
9+
public static void main(String[] args) {
10+
11+
System.out.println("Welcome to Interpreter Design pattern which interpretes your date format accross all languages ");
12+
Scanner scan = new Scanner(System.in);
13+
14+
// Object initialization block
15+
16+
// User input block
17+
String repeatRunFlag = "yes";
18+
while (!repeatRunFlag.equalsIgnoreCase("no")) {
19+
20+
System.out.println("Please select the Expression : 'MM-DD-YYYY' or 'YYYY-MM-DD'");
21+
Scanner scanner = new Scanner(System.in);
22+
String expression = scanner.next();
23+
24+
Context context = new Context();
25+
context.setExpression(expression);
26+
context.setDate(new Date());
27+
28+
ArrayList<AbstractExpression> expressionOrderList = getExpressionOrder(context);
29+
30+
System.out.println("Input : " + context.getExpression() + " : " + new Date());
31+
32+
for (AbstractExpression abstractExpression : expressionOrderList) {
33+
abstractExpression.evaluate(context);
34+
System.out.println(abstractExpression.getClass().getSimpleName() + " Evaluated : " + context.getExpression());
35+
36+
}
37+
38+
System.out.println("Output : " + context.getExpression());
39+
40+
System.out.println("Press No to Exit and any other character to repeat .... ");
41+
try {
42+
repeatRunFlag = scan.next();
43+
} catch (Exception e) {
44+
repeatRunFlag = "no";
45+
}
46+
47+
}
48+
49+
System.out.println("\n $$$$$$$$$$$$$$$$$$$$ Thanks by Prem Aseem $$$$$$$$$$$$$$$$$$$$$$ \n ");
50+
System.out.println("\n $$$$$$$$$$$$$$$$$$$$$ www.premaseem.com $$$$$$$$$$$$$$$$$$$$$$ \n ");
51+
}
52+
53+
private static ArrayList<AbstractExpression> getExpressionOrder(Context context) {
54+
ArrayList<AbstractExpression> expressionOrderList = new ArrayList<AbstractExpression>();
55+
String[] strArray = context.getExpression().split("-");
56+
for (String string : strArray) {
57+
if (string.equalsIgnoreCase("MM")) {
58+
expressionOrderList.add(new MonthExpression());
59+
} else if (string.equalsIgnoreCase("DD")) {
60+
expressionOrderList.add(new DayExpression());
61+
} else {
62+
expressionOrderList.add(new YearExpression());
63+
}
64+
65+
}
66+
return expressionOrderList;
67+
}
68+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.premaseem.interpreterPattern;
2+
3+
import java.util.Date;
4+
5+
public class Context {
6+
public String expression;
7+
public Date date;
8+
9+
public String getExpression() {
10+
return expression;
11+
}
12+
13+
public void setExpression(String expression) {
14+
this.expression = expression;
15+
}
16+
17+
public Date getDate() {
18+
return date;
19+
}
20+
21+
public void setDate(Date date) {
22+
this.date = date;
23+
}
24+
25+
}

0 commit comments

Comments
 (0)