Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exercise 11-4 Inherit the Product class In this exercise, youll modify a version of the Product application so it adds a class named MyProduct that

Exercise 11-4 Inherit the Product class In this exercise, youll 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 thats provided by the NumberFormat object thats 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 thats 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; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Visual Basic 2017 For Windows Web And Database Applications

Authors: Corinne Hoisington

1st Edition

1337102113, 978-1337102117

More Books

Students also viewed these Databases questions

Question

Question 1 (a2) What is the reaction force Dx in [N]?

Answered: 1 week ago