0

大家好,我正在尝试实现策略模式,但我无法在具体类中设置数量,我的意思是数量与辅助类中与接口有关系的数量相同。我尝试使用构造函数以及 setter 和 getter 方法设置值,但是如果您可以查看并提供一些反馈,它将无法正常工作。这是代码。

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;
    }
}

因此,当我对其进行测试时,它会返回值 --------------------------TO: OracleFROM: Easyflap (UK) AMOUNT:$500.00---------------------------------------------------- 来自当我尝试修改 AmericanInvoice 中的金额时,Customer 类中的 getInvoice 方法不起作用。

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);
    }
}
4

2 回答 2

3

您实际上并没有在策略对象本身上调用任何方法!

于 2012-11-10T17:24:18.147 回答
1

我不容忍以这种方式使用策略模式,因为当前汇率不需要使用策略模式。但是根据您的示例,以下代码很可能是您打算执行的操作。

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;
    }
}
于 2012-11-10T17:26:59.220 回答