Exercise 11-4 Inherit the Product class In this exercise, you’ll modify a versio
ID: 3724862 • Letter: E
Question
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. Run the application to make sure it works correctly. Create a new subclass and use it
2. 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.
3.In the ProductDB class, modify the getProduct method so it returns a MyProduct object, not a Product object.
4. 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.
5. Run the application to make sure that it still works correctly.
CODE:
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!");
}
}
package murach.db;
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.business;
import java.text.NumberFormat;
public class Product {
private String code;
private String description;
private 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;
}
}
Explanation / Answer
Product.java
package murach.business;
import java.text.NumberFormat;
public class Product {
private String code;
private String description;
private 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;
import java.util.Locale;
public class MyProduct extends Product {
public MyProduct(String s1,String s2,double price) {
super(s1,s2,price);
}
public String getPrice(NumberFormat nf) {
return nf.format(getPrice());
}
}
____________________
ProductDB.java
package murach.db;
import murach.business.MyProduct;
import murach.business.Product;
public class ProductDB {
private static String[][] productsArray = {
{"java", "Murach's Java Programming", "57.50"},
{"jsp", "Murach's Java Servlets and JSP", "57.50"},
{"mysql", "Murach's MySQL", "54.50"}
};
public static MyProduct getProductByIndex(int i) {
// TODO: Add code here to return Product object
double price = Double.parseDouble(productsArray[i][2]);
MyProduct p = new MyProduct(productsArray[i][0], productsArray[i][1], price);
return p;
}
public static MyProduct getProductByCode(String code) {
// TODO: Add code here to return Product object
for(int i=0;i<3;i++)
{
if(productsArray[i][0].equals(code))
{
return getProductByIndex(i);
}
}
return null;
}
public static MyProduct[] getAllProducts() {
// TODO: Add code here to return array of Product objects
MyProduct[] all = new MyProduct[3];
for(int i=0;i<3;i++)
{
all[i] = getProductByIndex(i);
}
return all;
}
}
___________________
Main.java
package murach.ui;
import java.text.NumberFormat;
import java.util.Locale;
import murach.business.MyProduct;
import murach.business.Product;
import murach.db.ProductDB;
public class Main {
public static void main(String[] args) {
//Locale locale = new Locale("en", "US");
Locale locale = Locale.getDefault();
NumberFormat nf=NumberFormat.getCurrencyInstance(locale);
MyProduct product = ProductDB.getProductByCode("jsp");
System.out.println("PRODUCT BY CODE");
System.out.println("Code:"+product.getCode());
System.out.println("Description:"+ product.getCode());
System.out.println("Price:"+product.getPrice(nf));
System.out.println("----------------------------------------------");
product = ProductDB.getProductByIndex(0);
System.out.println("PRODUCT BY INDEX");
System.out.println("Code:"+ product.getCode());
System.out.println("Description:"+product.getDescription());
System.out.println("Price:"+product.getPrice(nf));
System.out.println("----------------------------------------------");
MyProduct[] products = ProductDB.getAllProducts();
System.out.println("LIST OF ALL PRODUCTS");
for (int i = 0; i < 3; i++) {
System.out.println("Code:"+ products[i].getCode());
System.out.println("Description:"+products[i].getDescription());
System.out.println("Price:"+products[i].getPrice(nf));
System.out.println();
}
System.out.println("----------------------------------------------");
}
}
____________________
Output:
PRODUCT BY CODE
Code:jsp
Description:jsp
Price:$57.50
----------------------------------------------
PRODUCT BY INDEX
Code:java
Description:Murach's Java Programming
Price:$57.50
----------------------------------------------
LIST OF ALL PRODUCTS
Code:java
Description:Murach's Java Programming
Price:$57.50
Code:jsp
Description:Murach's Java Servlets and JSP
Price:$57.50
Code:mysql
Description:Murach's MySQL
Price:$54.50
----------------------------------------------
_______________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.