Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Depenency Injection The Java statements given below define code that will implem

ID: 3715006 • Letter: D

Question

Depenency Injection

The Java statements given below define code that will implement dependency injection. However, there are important statements missing (e.g., method definitions, annotations, imports).

Add the missing statements. (Please highlight your additions with bold or color characters.)

//************************************************************************* public class Product {     private String title;     private Float price;

    public Product(String title) {          this.title = title;     }

    // Assume GET and SET methods for title and price defined here

}

//************************************************************************* public class ProductService {

private PriceManager priceMgr;

public Product createProduct(String title, Float price) {         Product product = new Product(title);

        product.setPrice( priceMgr.                              );         return product;

}

}

//************************************************************************* public interface PriceManager {

        

}

//************************************************************************* public class priceCalculator implements PriceManager {

public Float getSalesPrice(Float price) {         return (price * 0.8);

}

}

Explanation / Answer

Answer:

//*************************************************************************
public class Product {   
private String title;
private Float price;

public Product(String title) {
this.title = title;   
  
}
// Assume GET and SET methods for title and price defined here
public void setTitle(String t) {
title = t;
}
public void setPrice(Float t) {
price = p;
}
public Float getPrice() {
return price;
}
public String getTitle() {
return title;
}

}


//*************************************************************************
public class ProductService {
private PriceManager priceMgr;

public Product createProduct(String title, Float price) {   
Product product = new Product(title);
product.setPrice( priceMgr.getSalesPrice() );
return product;
}
}


//*************************************************************************
public interface PriceManager {
public Float getSalesPrice(Float price);
}


//*************************************************************************
public class priceCalculator implements PriceManager {

public Float getSalesPrice(Float price) { return (price * 0.8);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote