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);
}
}
You don't actually call any methods on the strategy object itself!