Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8861559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:31:06+00:00 2026-06-14T15:31:06+00:00

Hi everyone I am trying to implement the strategy pattern but I can not

  • 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);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:31:07+00:00Added an answer on June 14, 2026 at 3:31 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey everyone, So, I'm currently trying the implement the APNG Specification , but am
Hello everyone i am trying to implement push notifications with urbanairship i followed the
hello everyone I am trying to make a script that steps through a list
Hi everyone trying to translate an older C program that uses array of structures
Hello everyone I been trying get php uploader working but having a lot of
Hy everyone! Im trying to implement file upload with Spring roo. The files path
I'm trying to implement Future.get(long, TimeUnit) in terms of TimeUnit.timedWait(Object, long) . It's not
I'm trying to implement a basic tagging system with hibernate. The problem is that
Hi everyone: I am trying to create a namespace so I can use a
I'm trying to implement a windows service application that downloads files from the FTP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.