In: Computer Science
EDIT*****
I figured it out. I will post the solution in a bit...
I could use some help with the following problem that implements the Abstract Factory Pattern:
Consider 2 SALES RECEIPT classes: RECEIPT1 and RECEIPT2.
RECEIPT1 has a method that displays the message “Receipt Body 1”.
RECEIPT2 has a method that displays the message “Receipt Body 2”. A SALES RECEIPT object has exactly 1 header and 1 footer.
There are 2 possible HEADER classes (HEADER1 and HEADER2) and 2 possible FOOTER classes (FOOTER1 and FOOTER2). HEADER1 class has a method that displays the message “Header 1”. HEADER2 class has a method that displays the message “Header 2”. FOOTER1 class has a method that displays the message “Footer 1”. FOOTER2 class has a method that displays the message “Footer 2”. HEADER1 should be used with RECEIPT1 and FOOTER1. HEADER2 should be used with RECEIPT2 and FOOTER2.
You must use the abstract factory pattern to create sales receipts.
1) Draw the UML class diagram.
2) Provide the implementation code of the UML class diagram given in question 1 in Java. The object created by the client code should display the following message: ”Header 1 Receipt Body 1 Footer 1”.
1.Required UML class diagram is given here:
2.Required implementation code is as follows:
package RECEIPT;
import java.util.Scanner;
public class Footer1 {
public void f1_body()
{
System.out.print("Footer 1");
}
}
public class Footer2 {
public void f2_body()
{
System.out.print("Footer 2");
}
}
public class Header1 {
public void h1_body()
{
System.out.print("Header 1");
}
}
public class Header2 {
public void h2_body()
{
System.out.print("Header 2");
}
}
public class Receipt1 extends Header1{
Footer1 f;
public void r1_body()
{
h1_body();
System.out.print("Receipt body 1");
f.f1_body();
}
}
public class Receipt2 extends Header2 {
Footer2 f;
public void r2_body()
{
h2_body();
System.out.print("Receipt body 2");
f.f2_body();
}
}
public class SalesReceipt {
public static void main(String[] args) {
Scanner sn=new Scanner(System.in);
Receipt1 r1=new Receipt1();
Receipt2 r2=new Receipt2();
do
{
System.out.println("\t\t\tMenu");
System.out.println("\t\t\t----");
System.out.println("1.Receipt 1 ");
System.out.println("2.Receipt 2");
System.out.println("3.Exit");
System.out.println("\tEnter Your Choice:");
int ch=sn.nextInt();
switch(ch)
{
case 1: r1.r1_body();
break;
case 2: r2.r2_body();
break;
case 3: System.exit(0);
break;
default: System.out.print("Enter Valid Choice:");
}
}while(true);
}
}