Exercise 11-4 Inherit the Product class In this exercise, you’ll modify a versio
ID: 3593188 • 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. Open the project named ch11_ex4_Product in the extra_ex_starts directory.
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.
using Netbeans IDE
ProductDB class
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;
}
}
Product class
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;
}
}
Product app class
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!");
}
}
Explanation / Answer
Product.java
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
import java.text.NumberFormat;
import java.util.Locale;
public class MyProduct extends Product {
public String getPrice(NumberFormat nf) {
return nf.format(getPrice());
}
}
________________
ProductDB.java
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
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Scanner;
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);
//Locale locale = new Locale("en", "US");
Locale locale = Locale.getDefault();
NumberFormat nf=NumberFormat.getCurrencyInstance(locale);
// display the output
String message = " PRODUCT " +
"Code: " + product.getCode() + " " +
"Description: " + product.getDescription() + " " +
"Price: " + product.getPrice(nf) + " ";
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: mysql
PRODUCT
Code: mysql
Description: Murach's MySQL
Price: $54.50
Continue? (y/n): y
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): n
Bye!
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.