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 7-1: 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:
1. Open the project named ch07_ex1_Product that's in the ex_starts folder. Then, open the Product, ProductDB, and ProductApp classes and review their code.
2. 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:
3. In the ProductDB class, modify the if/else statement so it includes another product.
4. 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:
5. In the Product class, add a constructor that defines three parameters and uses them to set the values of the three instance variables.
6. 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 if/else clause.
7. Run the application to make sure it still works correctly.
Add a method to the Product class:
8. 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 2 decimal places but no currency symbol.
9. In the ProductApp class, modify the code so it uses the method.
10. Run the application to make sure it still works correctly.
Modify the ProductDB class so it defines an object:
11. IN the ProductDB class, modify the getProduct() method so it's an instance method instead of a static method.
12. 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.
13. 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 =0;
}
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();
return currency.format(price);
}
}
Original ProductDB class code:
public class ProductDB {
public static Product getProduct(String 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 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;
}
}
Orignal ProductApp class code:
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();
// display 1 or more products
Scanner sc = new Scanner(System.in);
String choice ="y";
while (choice.equalsIgnoreCase("y")){
// get the input from the user
System.out.print("Enter product code: ");
String productCode = sc.nextLine(); // read the product code
// get the Product object
Product product = ProductDB.getProduct(productCode);
// display the output
System.out.println();
System.out.println("SELECTED PRODUCT");
System.out.println("Description: "+ product.getDescription());
System.out.println("Price: "+ product.getPriceFormatted());
System.out.println();
// see if the user wants to continue
System.out.print("Continue?(y/n): ");
choice = sc.nextLine();
System.out.println();
}
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock 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 Programming Questions!