File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
import com.premaseem.icecreams.ChocolateIceCream;
11+
import com.premaseem.icecreams.IceCream;
1112
import com.premaseem.icecreams.StrawberryIceCream;
1213

1314
import java.util.Scanner;
@@ -20,28 +21,38 @@ public static void main (String[] args) {
2021
System.out.println("Chocolate");
2122

2223
Scanner scan = new Scanner(System.in);
23-
24-
// Tight coupling
25-
StrawberryIceCream strawberryIceCream =null;
26-
ChocolateIceCream chocolateIceCream = null;
27-
28-
// Sphegati code with if else ladder
2924
String iceCreamChoice = scan.next();
30-
if (iceCreamChoice.equalsIgnoreCase("Strawberry")){
31-
strawberryIceCream = new StrawberryIceCream();
32-
}else if (iceCreamChoice.equalsIgnoreCase("Chocolate")){
33-
chocolateIceCream = new ChocolateIceCream();
34-
}
35-
36-
// Ice cream of your choice is :
37-
System.out.print("Ice cream of your choice is ");
38-
if (strawberryIceCream != null){
39-
System.out.println(strawberryIceCream);
40-
}
41-
42-
if (chocolateIceCream != null){
43-
System.out.println(chocolateIceCream);
44-
}
25+
26+
// Tight coupling
27+
// StrawberryIceCream strawberryIceCream =null;
28+
// ChocolateIceCream chocolateIceCream = null;
29+
30+
// Loose coupling using interface
31+
IceCream iceCream = null;
32+
33+
// Sphegati code with if else ladder
34+
// if (iceCreamChoice.equalsIgnoreCase("Strawberry")){
35+
// strawberryIceCream = new StrawberryIceCream();
36+
// }else if (iceCreamChoice.equalsIgnoreCase("Chocolate")){
37+
// chocolateIceCream = new ChocolateIceCream();
38+
// }
39+
40+
// Crisp, reusable, centralized code using factory
41+
IceCreamFactory iceCreamFactory = new IceCreamFactory();
42+
iceCreamFactory.createIceCream(iceCreamChoice);
43+
44+
// Repeatative code to even print
45+
// System.out.print("Ice cream of your choice is ");
46+
// if (strawberryIceCream != null){
47+
// System.out.println(strawberryIceCream);
48+
// }
49+
//
50+
// if (chocolateIceCream != null){
51+
// System.out.println(chocolateIceCream);
52+
// }
53+
54+
System.out.print("Ice cream of your choice is "+ iceCream.getIceCreamName());
55+
System.out.println(iceCream);
4556

4657
}
4758
}

0 commit comments

Comments
 (0)