tags:

views:

76

answers:

0
+1  Q: 

decorator pattern

I have a program that converts currency using a specific design pattern. I now need to take my converted result and using the decorator pattern allow the result to be converted to 3 different formats: 1 - exponential notation, rounded to 2 decimal points.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Converter
{
   public partial class Form1 : Form
   {
 // Setup Chain of Responsibility
    Handler h1 = new USDHandler();
    Handler h2 = new CADHandler();
    Handler h3 = new AUDHandler();

    public string reqCurName;
    public int reqAmt;
    public string results;
     public string requestID;

public Form1()
{
  InitializeComponent();
  h1.SetSuccessor(h2);
  h2.SetSuccessor(h3);
}

 // "Handler"
  private void button1_Click_1(object sender, EventArgs e)
  {

  reqCurName = txtInput.Text;
  reqAmt = Convert.ToInt32(txtAmt.Text.ToString());



  results = h1.HandleRequest(reqCurName, reqAmt);
  if (results != "")
  {
    lblResult.Text = results;
    lblResult.Visible = true;
  }
}

abstract class Handler
{
protected Handler successor;

public string retrn;

public void SetSuccessor(Handler successor)
{
  this.successor = successor;
}

public abstract string HandleRequest(string requestID, int reqAmt);

}

// "USD Handler"

class USDHandler : Handler
{
  public override string HandleRequest(string requestID, int reqAmt)
  {

  if (requestID == "USD")
  {
    retrn = "Request handled by " +
     this.GetType().Name + " \nConversion from Euro to USD is " + reqAmt/0.630479;
    return (retrn);
  }
  else if (successor != null)
  {
    retrn = successor.HandleRequest(requestID, reqAmt);
  }
  return (retrn);
}

}

// "CAD Handler"

class CADHandler : Handler
{
  public override string HandleRequest(string requestID, int reqAmt)
  {
  if (requestID == "CAD")
  {
    retrn = "Request handled by " +
     this.GetType().Name + " \nConversion from Euro to CAD is " + reqAmt /0.617971;
    return (retrn);
  }
  else if (successor != null)
  {
    retrn = successor.HandleRequest(requestID, reqAmt);
  }
  return (retrn);
}

}

 // "AUD Handler"

 class AUDHandler : Handler
 {
   public override string HandleRequest(string requestID, int reqAmt)
   {
    if (requestID == "AUD")
    {
      requestID = "Request handled by " +
       this.GetType().Name + " \nConversion from Euro to AUD is " + reqAmt / 0.585386;
      return (requestID);
    }
  else if (successor != null)
  {
    retrn = successor.HandleRequest(requestID, reqAmt);
  }
  return (requestID);
}

} } }