Question: Need help with exercise 11-4: Create a new subclass and use it 1. In the murach.business package, create a new class named MyProduct that inherits

Need help with exercise 11-4:

Create a new subclass and use it

1. 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 thats provided by the NumberFormat object thats passed as a parameter.

2. In the ProductDB class, modify the getProduct method so it returns a MyProduct object, not a Product object.

3. In the ProductApp class, modify the code so it uses a MyProduct object, not a Product object. This should include using the getPrice method thats only available from the MyProduct class to apply currency formatting to the price.

4. Run the application to make sure that it still works correctly.

The existing code is:

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;

}

}

ProductDB.java:

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;

}

}

ProductApp.java:

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!");

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To solve this exercise you will need to create a new subclass make modifications to existing classes and verify the applications correct functionality Heres a stepbystep guide Step 1 Create the MyProd... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!