Object Oriented Design and Patterns
Object Oriented Design and Patterns
Department of CSE
Assignment on:
CSE-4122 (Object Oriented Design and Design Pattern Lab)
SUBMITTED To:
Shams Mahmud
Lecturer,
IMPERIAL COLLEGE OF ENGINEERING
SUBMITTED By:
Problem: 1
Write a program to create various shapes like (Circle, Triangle, Rectangle)and calculate the
area of the shape. You should use Factory Pattern for shape creation so that all shapes can
be created using a single factory object.
Each shape should have their defining properties (For example circle should have radius,
triangle should have length of its three side etc.) and a method named getArea() to calculate
the area of the shape.
When creating a particular shape, you should initialize the shapes property with any default
values (ie. for circle, radius = 1; for rectangle width = 2 and height = 1.5 etc.)
Problem: 2
Suppose we want to withdraw cash from our bank account using check. Bank has the
following business rule for cash withdrawal based on the amount written on check.
d) For Tk. 10,000 only cashier's authorization is sufficient to withdraw money from account.
e) For Tk. 10,000 to 10,00,000 authorization from both cashier and senior officer is needed
f) For Tk 10,00,000 authorization from Senior officer and Manager is mandatory.
Implement this business rule using Chain of Responsibility design pattern.
// Shape.java interface
public interface Shape {
double getArea();
}
// Circle.java class
class Circle implements Shape
{
private double radius;
public Circle() {
this.radius = 1.0; // Default radius
}
@Override
public double getArea()
{
return Math.PI * radius * radius;
}
}
//Rectangle.java class
public class Rectangle implements Shape
{
private double width;
private double height;
public Rectangle()
{
this.width = 2.0; // Default width
this.height = 1.5; // Default height
}
@Override
public double getArea()
{
return width * height;
}
}
//Triangle.java class
public class Triangle implements Shape {
private double sideA;
private double sideB;
private double sideC;
public Triangle() {
this.sideA = 1.0; // Default side lengths
this.sideB = 1.0;
this.sideC = 1.0;
}
@Override
public double getArea() {
double s = (sideA + sideB + sideC) / 2;
return Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC));
}
}
//ShapeFactory.java
class ShapeFactory {
public static Shape createShape(String shapeType, double... parameters) {
switch (shapeType.toLowerCase())
{
case "circle":
if (parameters.length == 1) {
return new Circle(parameters[0]);
} else {
return new Circle();
}
case "triangle":
if (parameters.length == 3)
{
return new Triangle(parameters[0], parameters[1], parameters[2]);
} else {
return new Triangle();
}
case "rectangle":
if (parameters.length == 2) {
return new Rectangle(parameters[0], parameters[1]);
} else {
return new Rectangle();
}
default:
return null;
}
}
}
//main.java
interface AuthorizationHandler
{
void authorize(int amount);
void setNextHandler(AuthorizationHandler nextHandler);
}
@Override
public void authorize(int amount) {
if (amount <= 10000) {
System.out.println("Cashier authorizes the withdrawal.");
} else if (nextHandler != null) {
nextHandler.authorize(amount);
} else
{
System.out.println("Authorization denied.");
}
}
@Override
public void setNextHandler(AuthorizationHandler nextHandler)
{
this.nextHandler = nextHandler;
}
}
class SeniorOfficer implements AuthorizationHandler
{
private AuthorizationHandler nextHandler;
@Override
public void authorize(int amount) {
if (amount > 10000 && amount <= 1000000)
{
System.out.println("cashier and senior officer authorizes the withdrawal.");
} else if (nextHandler != null)
{
nextHandler.authorize(amount);
} else {
System.out.println("Authorization denied.");
}
}
@Override
public void setNextHandler(AuthorizationHandler nextHandler)
{
this.nextHandler = nextHandler;
}
}
@Override
public void setNextHandler(AuthorizationHandler nextHandler)
{
// Manager is the last handler in the chain .
throw new UnsupportedOperationException("Manager cannot have a next handler.");
}
}
//Base class
public class CashWithdrawal
{
public static void main(String[] args)
{
AuthorizationHandler cashier = new Cashier();
AuthorizationHandler seniorOfficer = new SeniorOfficer();
AuthorizationHandler manager = new Manager();