0

Hi everyone I am trying to implement the strategy pattern but I can not set the amount in the concrete classes, what i mean is that the amount is remianing the same as the one in the helper class that has a relationship with the interface. I tried with to set the value with the constructor and the setter and getter methods but it is not working if you can have a look and give some feedback it would be graet.Here is the code.

public interface InvoicingAlgorithm 
{
    public void getInvoice(String name, double amount);
}


public class AmericanInvoice implements InvoicingAlgorithm
{



    AmericanInvoice()
    {

    }
    //Uk: america 1 : 1.57
    @Override
    public void getInvoice(String name, double amount) 
    {
        Customer customer = new Customer(name , amount * 1.57);
        customer.setAmount(amount * 1.57);
        customer.getInvoice();
    }

}

public class Customer 
{

    /**
     * @param name represent the name of the Customer
     */
    private String name;

    /**
     * @param amount represent the amount of money
     */
    private double amount;

    /**
     * @param i represents object of InvoicingAlgorithm
     */
    private InvoicingAlgorithm i;

    Customer(String name, double amount)
    {
        this.name = name;
        this.amount = amount;

    }

     public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getAmount() {
        return amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    public InvoicingAlgorithm getI() {
        return i;
    }


    public void setInvoicingAlgorithm(InvoicingAlgorithm i)
    {
        this.i = i;
    }

    public String getInvoice()
    {
        DecimalFormat df = new DecimalFormat("#.00");
        String total = "--------------------------------------TO:   "
              + name + "FROM: Easyflap (UK) AMOUNT"  + ":$" + 
                df.format(amount) 
              + "--------------------------------------";

        return total;
    }
}

So when I test it it is returning the value --------------------------------------TO: OracleFROM: Easyflap (UK) AMOUNT:$500.00-------------------------------------- which is from the method getInvoice in the Customer class when I try to modify the amount in AmericanInvoice it is not working.

The test class for the AmericanInvoice

public class AmericanInvoiceTest {

    /**
     * Test of getInvoice method, of class AmericanInvoice.
     */
    @Test
    public void testGetInvoice() {
        System.out.println("Producing American invoice");
        final int invoiceAmount = 500;
        final Customer c = new Customer("Oracle", invoiceAmount);
        c.setInvoicingAlgorithm(new AmericanInvoice());
        String actualOutput = c.getInvoice();
        final File f = new File("actual-american.txt");
        FileUtility.resetFile(f);
        FileUtility.writeFile(f, actualOutput);
        String expectedOutput = FileUtility.readFile(new File("expected-american.txt"));
        //System.out.println(actualOutput);
        //System.out.println(expectedOutput);
        actualOutput = actualOutput.replaceAll("\\s", "");
        expectedOutput = expectedOutput.replaceAll("\\s", "");
        //System.out.println(actualOutput);
        //System.out.println(expectedOutput);
        assertEquals(actualOutput, expectedOutput);
    }
}
Doesn't Matter
  • 405
  • 4
  • 12
  • 23

2 Answers2

3

You don't actually call any methods on the strategy object itself!

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
1

I don't condone using the strategy pattern this way as current exchange rates does not require using the Strategy Pattern. But the following code is most likely what you intended to do based on your example.

public interface InvoicingAlgorithm {
    public double adjustInvoice(double amount);
}


public class AmericanInvoice implements InvoicingAlgorithm {    
    //Uk: america 1 : 1.57
    @Override
    public double adjustInvoice(double amount) {
        return amount * 1.57;
    }   
}

public class Customer {

    /**
     * @param name represent the name of the Customer
     */
    private String name;

    /**
     * @param amount represent the amount of money
     */
    private double amount;

    /**
     * @param i represents object of InvoicingAlgorithm
     */
    private InvoicingAlgorithm i;

    Customer(String name, double amount) {
        this.name = name;
        this.amount = amount;

    }

     public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getAmount() {
        return amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    public InvoicingAlgorithm getI() {
        return i;
    }

    public void setInvoicingAlgorithm(InvoicingAlgorithm i) {
        this.i = i;
    }

    public String getInvoice() {
        DecimalFormat df = new DecimalFormat("#.00");
        String total = "--------------------------------------TO:   "
              + name + "FROM: Easyflap (UK) AMOUNT"  + ":$" + 
                df.format(i.adjustInvoice(amount)) 
              + "--------------------------------------";

        return total;
    }
}
Andrew T Finnell
  • 13,417
  • 3
  • 33
  • 49
  • why in the InvoicingAlgorithm interface did you change the method's signature – Doesn't Matter Nov 10 '12 at 17:33
  • @Doesn'tMatter Because the name of the invoice has no bearing on the exchange rate. The name of the invoice appears to be attached to the customer. It looks like all your strategy is trying to do is convert the currency. If you need it to "print" your invoke, then move all the print statements from the customer to the American strategy. – Andrew T Finnell Nov 10 '12 at 17:35
  • it is still returning the same result, so can not pass the new amount – Doesn't Matter Nov 10 '12 at 17:49
  • @Doesn'tMatter Can you update your question with all of the actual code you are using? These are just fragments. Where is the code that creates the Customer object and invokes getInvoice. – Andrew T Finnell Nov 10 '12 at 17:50