Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 10-3 Work with an array of product data In this exercise, you’ll work w

ID: 3605301 • Letter: E

Question

Exercise 10-3   Work with an array of product data

In this exercise, you’ll work with a two-dimensional array that stores the data for three products.

Review the application

1.      Open the project named ch10_ex3_ProductArray in the extra_ex_starts directory.

2.      Open the classes and review the code. Note that the ProductDB class defines a two-dimensional array that stores the data for three products. Also, note that this class contains three methods that aren’t implemented.

3.      Run the application. At this point, it should print some messages to the console, but these messages won’t contain any product data.

Add the code that works with the array of product data

4.      In the ProductDB class, add code that implements the getProductByIndex method. This should return a Product object for the product data at the specified index.

5.      In the Main class, add code that uses the getProductByIndex method and displays the data that’s returned by it.

6.      Run the application and make sure it works correctly. At this point, it should print data for the PRODUCT BY INDEX heading.

7.      In the ProductDB class, add code that implements the getProductByCode method. This should return a Product object for the product data with the specified code.

8.      In the Main class, add code that uses the getProductByCode method and displays the data that’s returned by it.

9.      Run the application and make sure it works correctly. At this point, it should print data for the PRODUCT BY CODE heading.

10.   In the ProductDB class, add code that implements the getAllProducts method. This should return an array of Product objects for all of the product data.

11.   In the Main class, add code that uses the getAllProducts method and displays the data that’s returned by it. To do that, you’ll need to loop through the array of Product objects that’s returned.

12.   Run the application and make sure it works correctly. At this point, it should print data for the LIST OF ALL PRODUCTS heading.

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();
return currency.format(price);
}
}

ProductDB.java

package murach.db;

import murach.business.Product;

public class ProductDB {
private static String[][] productsArray = {
{"java", "Murach's Java Programming", "57.50"},
{"jsp", "Murach's Java Servlets and JSP", "57.50"},
{"mysql", "Murach's MySQL", "54.50"}
};
  
public static Product getProductByIndex(int i) {
// TODO: Add code here to return Product object
return null;
}
  
public static Product getProductByCode(String code) {
// TODO: Add code here to return Product object
return null;
}
  
public static Product[] getAllProducts() {
// TODO: Add code here to return array of Product objects
return null;
}   
}

Main.java

package murach.ui;

import murach.business.Product;
import murach.db.ProductDB;

public class Main {

public static void main(String[] args) {
Product product = ProductDB.getProductByCode("jsp");
System.out.println("PRODUCT BY CODE");
System.out.println("Code: ");
System.out.println("Description: ");
System.out.println("Price: ");
System.out.println("----------------------------------------------");
  
product = ProductDB.getProductByIndex(0);
System.out.println("PRODUCT BY INDEX");
System.out.println("Code: ");
System.out.println("Description: ");
System.out.println("Price: ");
System.out.println("----------------------------------------------");
  
Product[] products = ProductDB.getAllProducts();
System.out.println("LIST OF ALL PRODUCTS");
for (int i = 0; i < 3; i++) {
System.out.println("Code: ");
System.out.println("Description: ");
System.out.println("Price: ");   
System.out.println();
}   
System.out.println("----------------------------------------------");   
}   
}

Explanation / Answer

Hi ... I have added the method codes in you file. Please check the below programs and test it.

Main.java

package murach.ui;

import murach.business.Product;

import murach.db.ProductDB;

public class Main {

public static void main(String[] args) {

Product product = ProductDB.getProductByIndex(0);

System.out.println("PRODUCT BY INDEX");

System.out.println("Code: "+product.getCode());

System.out.println("Description: "+product.getDescription());

System.out.println("Price: "+product.getPrice());

System.out.println("----------------------------------------------");

  

product = ProductDB.getProductByCode("jsp");

System.out.println("PRODUCT BY CODE");

System.out.println("Code: "+product.getCode());

System.out.println("Description: "+product.getDescription());

System.out.println("Price: "+product.getPrice());

System.out.println("----------------------------------------------");

  

  

  

Product[] products = ProductDB.getAllProducts();

System.out.println("LIST OF ALL PRODUCTS");

for (int i = 0; i < products.length; i++) {

System.out.println("Code: "+products[i].getCode());

System.out.println("Description: "+products[i].getDescription());

System.out.println("Price: "+products[i].getPrice());

System.out.println();

}   

System.out.println("----------------------------------------------");   

}   

}

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();
return currency.format(price);
}
}

ProductDB.java

package murach.db;

import murach.business.Product;

public class ProductDB {

private static String[][] productsArray = {

{"java", "Murach's Java Programming", "57.50"},

{"jsp", "Murach's Java Servlets and JSP", "57.50"},

{"mysql", "Murach's MySQL", "54.50"}

};

  

public static Product getProductByIndex(int i) {

// TODO: Add code here to return Product object

Product p = null;

for(int i1=i;i1<=i;i1++){

String code = productsArray[i1][0];

String description = productsArray[i1][1];

double rate = Double.parseDouble(productsArray[i1][2]);

p=new Product(code, description, rate);

}

return p;

}

  

public static Product getProductByCode(String code) {

// TODO: Add code here to return Product object

Product p = null;

for(int i1=0;i1<productsArray.length;i1++){

String code1 = productsArray[i1][0];

if(code1.equals(code)){

String description = productsArray[i1][1];

double rate = Double.parseDouble(productsArray[i1][2]);

p=new Product(code, description, rate);

break;

}

}

return p;

}

  

public static Product[] getAllProducts() {

// TODO: Add code here to return array of Product objects

Product p1[] = new Product[3];

for(int i1=0;i1<productsArray.length;i1++){

String code1 = productsArray[i1][0];

String description = productsArray[i1][1];

double rate = Double.parseDouble(productsArray[i1][2]);

p1[i1]=new Product(code1, description, rate);

}

return p1;

}

}

Output:

PRODUCT BY INDEX
Code: java
Description: Murach's Java Programming
Price: 57.5
----------------------------------------------
PRODUCT BY CODE
Code: jsp
Description: Murach's Java Servlets and JSP
Price: 57.5
----------------------------------------------
LIST OF ALL PRODUCTS
Code: java
Description: Murach's Java Programming
Price: 57.5

Code: jsp
Description: Murach's Java Servlets and JSP
Price: 57.5

Code: mysql
Description: Murach's MySQL
Price: 54.5

----------------------------------------------

Please check it once and let me know any queries. Thanks a lot. All the best.