Question: Exercise 7 - 1 : Modify the Product Viewer application This exercise guides you through the process of testing and modifying the Product Viewer application
Exercise : Modify the Product Viewer application
This exercise guides you through the process of testing and modifying the Product Viewer application that's presented in this chapter.
Review and test the project:
Open the project named chexProduct that's in the exstarts folder. Then, open the Product, ProductDB, and ProductApp classes and review their code.
Run the application and test it with valid product codes like "java", jsp and mysql to make sure this application works correctly. Then, test it with an invalid code to see how that works.
Modify the ProductDB class:
In the ProductDB class, modify the ifelse statement so it includes another product.
Run the application and test it to make sure the new product code works. This shows that you can modify the code for a class without needing to modify the other classes that use it
Add a constructor to the Product class:
In the Product class, add a constructor that defines three parameters and uses them to set the values of the three instance variables.
In the ProductDB class, modify the code so it uses the new constructor to set the data in the Product object instead of using the setCode setDescription and setPrice methods. To do that, you can assign a new Product object to the Product variable within each ifelse clause.
Run the application to make sure it still works correctly.
Add a method to the Product class:
In the product class, add a method named getPriceNumberFunction that returns the price with number formatting not currency formatting This method should return the number with decimal places but no currency symbol.
In the ProductApp class, modify the code so it uses the method.
Run the application to make sure it still works correctly.
Modify the ProductDB class so it defines an object:
IN the ProductDB class, modify the getProduct method so it's an instance method instead of a static method.
In the ProductApp class, modify the code so it creates a ProductDB object named db Then, use this object to call the getProduct method of the ProductDB class.
Run the application to make sure it still works correctly.
Original Product class code:
import java.text.NumberFormat;
public class Product
private String code;
private String description;
private double price;
public Product
code ;
description ;
price ;
public void setCodeString code
this.code code;
public String getCode
return code;
public void setDescriptionString description
this.description description;
public String getDescription
return description;
public void setPricedouble price
this.price price;
public double getPrice
return price;
public String getPriceFormatted
NumberFormat currency NumberFormat.getCurrencyInstance;
return currency.formatprice;
Original ProductDB class code:
public class ProductDB
public static Product getProductString productCode
In a more realistic application, this code would
get the data for the product from a file or database
For now, this code just uses ifelse statements
to return the correct product data
create the Product object
Product product new Product;
fill the Product object with data
product.setCodeproductCode;
if productCodeequalsIgnoreCasejava
product.setDescriptionMurachs Java Programming";
product.setPrice;
else if productCodeequalsIgnoreCasejsp
product.setDescriptionMurachs Java Servlets and JSP;
product.setPrice;
else if productCodeequalsIgnoreCasemysql
product.setDescriptionMurachs MySQL;
product.setPrice;
else
product.setDescriptionUnknown;
return product;
Orignal ProductApp class code:
import java.util.Scanner;
public class ProductApp
public static void mainString args
display a welcome message
System.out.printlnWelcome to the Product Viewer";
System.out.println;
display or more products
Scanner sc new ScannerSystemin;
String choice y;
while choiceequalsIgnoreCasey
get the input from the user
System.out.printEnter product code: ;
String productCode scnextLine; read the product code
get the Product object
Product product ProductDB.getProductproductCode;
display the output
System.out.println;
System.out.printlnSELECTED PRODUCT";
System.out.printlnDescription: product.getDescription;
System.out.printlnPrice: product.getPriceFormatted;
System.out.println;
see if the user wants to continue
System.out.printContinueyn: ;
choice scnextLine;
System.out.println;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
