Exercise 114 Inherit the Product class In this exercise youl
Exercise 11-4 Inherit the Product class
In this exercise, you’ll modify a version of the Product application so it adds a class named MyProduct that extends the Product class and enhances its functionality.
Review the application
1. Open the project named ch11_ex4_Product.
2. Open the classes and review their code.
3. Run the application to make sure it works correctly.
Create a new subclass and use it
4. In the murach.business package, create a new class named MyProduct that inherits the Product class. This new class should add the following method to the Product class:
public String getPrice(NumberFormat nf)
This method should format the price for the product using the format that’s provided by the NumberFormat object that’s passed as a parameter.
5. In the ProductDB class, modify the getProduct method so it returns a MyProduct object, not a Product object.
6. In the ProductApp class, modify the code so it uses a MyProduct object, not a Product object. This should include using the getPrice method that’s only available from the MyProduct class to apply currency formatting to the price.
7. Run the application to make sure that it still works correctly.
package murach.business;
import java.text.NumberFormat;
public class Product {
protected String code;
 protected String description;
 protected double price;
public Product() {
 code = \"\";
 description = \"\";
 price = 0;
 }
public Product(String code, String description, double price) {
 this.code = code;
 this.description = description;
 this.price = price;
 }
public void setCode(String code) {
 this.code = code;
 }
public String getCode() {
 return code;
 }
public void setDescription(String description) {
 this.description = description;
 }
public String getDescription() {
 return description;
 }
public void setPrice(double price) {
 this.price = price;
 }
public double getPrice() {
 return price;
 }
 
 public String getPriceFormatted() {
 NumberFormat currency = NumberFormat.getCurrencyInstance();
 String priceFormatted = currency.format(price);
 return priceFormatted;
 }
 }
import murach.business.Product;
public class ProductDB {
public Product getProduct(String productCode) {
 // In a more realistic application, this code would
 // get the data for the product from a database
 // For now, this code just uses if/else statements
 // to return the correct product data
// create the Product object
 Product product = new Product();
// fill the Product object with data
 product.setCode(productCode);
 if (productCode.equalsIgnoreCase(\"java\")) {
 product.setDescription(\"Murach\'s Java Programming\");
 product.setPrice(57.50);
 } else if (productCode.equalsIgnoreCase(\"jsp\")) {
 product.setDescription(\"Murach\'s Java Servlets and JSP\");
 product.setPrice(57.50);
 } else if (productCode.equalsIgnoreCase(\"mysql\")) {
 product.setDescription(\"Murach\'s MySQL\");
 product.setPrice(54.50);
 } else {
 product.setDescription(\"Unknown\");
 }
 return product;
 }
 }
package murach.ui;
import java.util.Scanner;
 import murach.business.Product;
 import murach.db.ProductDB;
public class ProductApp {
public static void main(String args[]) {
 // display a welcome message
 System.out.println(\"Welcome to the Product Viewer\");
 System.out.println();
// create 1 or more line items
 Scanner sc = new Scanner(System.in);
 String choice = \"y\";
 while (choice.equalsIgnoreCase(\"y\")) {
 // get input from user
 System.out.print(\"Enter product code: \");
 String productCode = sc.nextLine();
// Use a ProductReader object to get the Product object
 ProductDB db = new ProductDB();
 Product product = db.getProduct(productCode);
   
 // display the output
 String message = \"\ PRODUCT\ \" +
 \"Code: \" + product.getCode() + \"\ \" +
 \"Description: \" + product.getDescription() + \"\ \" +
 \"Price: \" + product.getPriceFormatted() + \"\ \";
 System.out.println(message);
// see if the user wants to continue
 System.out.print(\"Continue? (y/n): \");
 choice = sc.nextLine();
 System.out.println();
 }
 System.out.println(\"Bye!\");
 }
 }
Solution
Product.java
package murach.business;
 import java.text.NumberFormat;
 public class Product {
 protected String code;
 protected String description;
 protected double price;
 public Product() {
 code = \"\";
 description = \"\";
 price = 0;
 }
 public Product(String code, String description, double price) {
 this.code = code;
 this.description = description;
 this.price = price;
 }
 public void setCode(String code) {
 this.code = code;
 }
 public String getCode() {
 return code;
 }
 public void setDescription(String description) {
 this.description = description;
 }
 public String getDescription() {
 return description;
 }
 public void setPrice(double price) {
 this.price = price;
 }
 public double getPrice() {
 return price;
 }
 
 public String getPriceFormatted() {
 NumberFormat currency = NumberFormat.getCurrencyInstance();
 String priceFormatted = currency.format(price);
 return priceFormatted;
 }
 }
_____________________________________________
MyProduct.java
package murach.business;
import java.text.NumberFormat;
public class MyProduct extends Product {
    public String getPrice(NumberFormat nf)
    {
        nf = NumberFormat.getCurrencyInstance();
    String priceFormatted = nf.format(getPrice());
    return priceFormatted;
    }
   
 }
_______________________________________________
ProductDB.java
import murach.business.MyProduct;
 import murach.business.Product;
 public class ProductDB {
 public MyProduct getProduct(String productCode) {
 // In a more realistic application, this code would
 // get the data for the product from a database
 // For now, this code just uses if/else statements
 // to return the correct product data
 // create the Product object
 MyProduct product = new MyProduct();
 // fill the Product object with data
 product.setCode(productCode);
 if (productCode.equalsIgnoreCase(\"java\")) {
 product.setDescription(\"Murach\'s Java Programming\");
 product.setPrice(57.50);
 } else if (productCode.equalsIgnoreCase(\"jsp\")) {
 product.setDescription(\"Murach\'s Java Servlets and JSP\");
 product.setPrice(57.50);
 } else if (productCode.equalsIgnoreCase(\"mysql\")) {
 product.setDescription(\"Murach\'s MySQL\");
 product.setPrice(54.50);
 } else {
 product.setDescription(\"Unknown\");
 }
 return product;
 }
 }
_______________________________________________
ProductApp.java
package murach.ui;
 import java.text.NumberFormat;
 import java.util.Scanner;
import murach.business.MyProduct;
 import murach.business.Product;
 import murach.db.ProductDB;
 public class ProductApp {
 public static void main(String args[]) {
 // display a welcome message
 System.out.println(\"Welcome to the Product Viewer\");
 System.out.println();
 // create 1 or more line items
 Scanner sc = new Scanner(System.in);
 String choice = \"y\";
 while (choice.equalsIgnoreCase(\"y\")) {
 // get input from user
 System.out.print(\"Enter product code: \");
 String productCode = sc.nextLine();
 // Use a ProductReader object to get the Product object
 ProductDB db = new ProductDB();
 MyProduct product =db.getProduct(productCode);
   
 //Creating the number format class object
 NumberFormat currency = NumberFormat.getCurrencyInstance();
   
 // display the output
 String message = \"\ PRODUCT\ \" +
 \"Code: \" + product.getCode() + \"\ \" +
 \"Description: \" + product.getDescription() + \"\ \" +
 \"Price: \" + product.getPrice(currency) + \"\ \";
 System.out.println(message);
 // see if the user wants to continue
 System.out.print(\"Continue? (y/n): \");
 choice = sc.nextLine();
 System.out.println();
 }
 System.out.println(\"Bye!\");
 }
 }
__________________________________________
output:
Welcome to the Product Viewer
Enter product code: java
PRODUCT
 Code: java
 Description: Murach\'s Java Programming
 Price: $57.50
Continue? (y/n): y
Enter product code: jsp
PRODUCT
 Code: jsp
 Description: Murach\'s Java Servlets and JSP
 Price: $57.50
Continue? (y/n): mysql
Bye!
____________________________Thank You






