In: Computer Science
Create a stimulation using the abstract factory pattern by using any of the topics/ideas you like.to use that subject. The scenario just has to make sense as an application using a factory and the related pieces of template/pattern.
Your simulation should include, at a minimum:
a. A driver class.
b. A factory producer class with a method that returns the
appropriate factory.
c. An abstract factory interface with at least two abstract
methods.
d. At least two factory classes that implement the abstract
factory.
e. At least two other classes per factory from (d) that are
produced by those factories. These classes should also share
interfaces as appropriate (see the PowerPoint notes for examples).
These classes should also have instance variables.
Additional requirements:
a. Your program should provide a menu and accept user input,
looping until the user quits.
b. Your menu should have some sort of welcome message that names
your company or some other label that illustrates your subject
(e.g. “Welcome to Cloud Services”).
c. The user should have several options to request the production
of different objects, and be able to enter some sort of relevant
value(s) for that object that sets an instance variable.
d. After the user selects an item and enters parameter(s), the
program should print a labeled representation of the item with any
instance variable values (i.e. use an appropriate toString
method).
e. The program loops until the user quits.
5. As usual, fully comment on your code, and it should be obvious
what your program does from the output.
The preform should be in java with fully commented.
For all the code given below i have included name in package named as
Package name - trying
Interfaces
AbstractFood.java {interface}
For menu item deleivery description like name quantity
package trying;
public interface AbstractFood {
public abstract String getItemName();
public abstract boolean isDelivered();
public abstract int getQuantity();
}
AbstractFoodFactory.java {Interface}
For placing order with given name and quanity for different types of cuisine
package trying;
public interface AbstractFoodFactory {
public abstract AbstractFood placeOrder(String itemName, int quantity) ;
}
classes implementing AbstarctFood.java class
Every class in these also have a toString() method defined to print object property
ChineseFood.java
package trying;
public class ChineseFood implements AbstractFood {
String itemName;
int quantity;
boolean delivered;
public ChineseFood(String itemName, int quantity) {
super();
this.itemName = itemName;
this.quantity = quantity;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public boolean isDelivered() {
return delivered;
}
public void setDelivered(boolean delivered) {
this.delivered = delivered;
}
@Override
public String toString() {
return "ChineseFood [itemName=" + itemName + ", quantity=" + quantity + "]";
}
}
ItalianFood.java
package trying;
public class ItalianFood implements AbstractFood {
String itemName;
int quantity;
boolean delivered;
public ItalianFood(String itemName, int quantity) {
super();
this.itemName = itemName;
this.quantity = quantity;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public boolean isDelivered() {
return delivered;
}
public void setDelivered(boolean delivered) {
this.delivered = delivered;
}
@Override
public String toString() {
return "ItalianFood [itemName=" + itemName + ", quantity=" + quantity + "]";
}
}
MexicanFood.java
package trying;
public class MexicanFood implements AbstractFood {
String itemName;
int quantity;
boolean delivered;
public MexicanFood(String itemName, int quantity) {
super();
this.itemName = itemName;
this.quantity = quantity;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public boolean isDelivered() {
return delivered;
}
public void setDelivered(boolean delivered) {
this.delivered = delivered;
}
@Override
public String toString() {
return "MexicanFood [itemName=" + itemName + ", quantity=" + quantity + "]";
}
}
classes implementing AbstractFoodFactory.java class
Every class in this is required to return a object of class dynamically like for food we can give chinese mexican or italian
ChineseFoodFactory.java
package trying;
public class ChineseFoodFactory implements AbstractFoodFactory {
@Override
public AbstractFood placeOrder(String itemName, int quantity) {
return new ChineseFood(itemName,quantity);
}
}
ItalianFoodFactory.java
package trying;
public class ItalianFoodFactory implements AbstractFoodFactory {
@Override
public AbstractFood placeOrder(String itemName, int quantity) {
return new ItalianFood(itemName,quantity);
}
}
MexicanFoodFactory.java
package trying;
public class MexicanFoodFactory implements AbstractFoodFactory {
@Override
public AbstractFood placeOrder(String itemName, int quantity) {
return new MexicanFood(itemName,quantity);
}
}
ConsumerClass.java
this class return and decide type of object based on user input like italian chinese etc
this is basically a implementation of abstract factory design pattern
package trying;
//main class implementing abstract factory design patterns
public class ConsumerClass {
public AbstractFood placeOrder(String itemName,int quantity,String itemType) {
AbstractFoodFactory a = null;
if(itemType.equals("italian")) {
a = new ItalianFoodFactory();
}else if(itemType.equals("mexican")) {
a = new MexicanFoodFactory();
}else if(itemType.equals("chinese")) {
a = new ItalianFoodFactory();
}
if(a!=null) {
return a.placeOrder(itemName, quantity);
}else {
return null;
}
}
}
MenuApplication.java
Driver Code
with menu to run a loop unitl user orders with a welcome message and driven on inputs of user
and display the order of user at end
package trying;
import java.util.ArrayList;
import java.util.Scanner;
public class MenuApplication {
//for storing all the orders menu items
ArrayList order;
public static void main(String[] args)
{
System.out.println("WELCOME TO THE FOOD FACTORY");
MenuApplication m = new MenuApplication();
//creating order
m.createOrder();
}
public boolean createOrder() {
Scanner scan = new Scanner(System.in);
ConsumerClass c = new ConsumerClass();
order = new ArrayList();
//menu driven template
while(true)
{
//user driven
System.out.println("To order Italian, Enter value 1");
System.out.println("To order Mexican, Enter value 2");
System.out.println("To order Chinese, Enter value 3");
System.out.println("To Exit, Enter value 0");
System.out.println("Enter your choice::");
int choice = scan.nextInt();//accept user input
int quant;
switch(choice)
{
case 1:
System.out.println("enter quantity of lasagnia you want");
quant=scan.nextInt();
//calling add which decodes type of object base don third paraamter here it is italian and then call methods for itlaianfood
order.add(c.placeOrder("Lazagne", quant, "italian"));;
break;
case 2:
System.out.println("enter quantity of taco you want");
quant=scan.nextInt();
//similary as italian it calls for mexican here
order.add(c.placeOrder("Taco", quant, "mexican"));
break;
case 3:
System.out.println("enter quantity of noodles you want");
quant=scan.nextInt();
//similary as italian it calls for chinese here
order.add(c.placeOrder("Noodles", quant, "chinese"));
break;
case 0:
System.out.println("Exiting the application");
scan.close();
//printin order here using tostring () method which is called implicitly on printing the object
System.out.println("Your order is: ");
System.out.println(order);
System.exit(0);
default:
System.out.println("Incorrect input!!! Please re-enter choice from our menu");
}
}
}
}
Output Screenshot:
codes are well commented for bettr understadning
Hope you are able to understand concept Please rate the answer