Needing help with Exercise 10-3 1. Open the classes and review the code. Note th
ID: 3757414 • Letter: N
Question
Needing help with Exercise 10-3
1. 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.
2. 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
3. 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.
4. In the Main class, add code that uses the getProductByIndex method and displays the data that’s returned by it.
5. Run the application and make sure it works correctly. At this point, it should print data for the PRODUCT BY INDEX heading.
6. 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.
7. In the Main class, add code that uses the getProductByCode method and displays the data that’s returned by it.
8. Run the application and make sure it works correctly. At this point, it should print data for the PRODUCT BY CODE heading.
9. 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.
10. 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.
11. Run the application and make sure it works correctly. At this point, it should print data for the LIST OF ALL PRODUCTS heading.
Existing code is:
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("----------------------------------------------");
}
}
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;
}
}
Explanation / Answer
ProductDB Class:
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
if(i<productsArray.length)
{
Product pro=new Product();
pro.setCode(productsArray[i][0]); //Setting the code
pro.setDescription(productsArray[i][1]); //Setting the description
Double db=Double.parseDouble(productsArray[i][2]);
pro.setPrice(db); //Setting the price
return pro;
}
return null;
}
public static Product getProductByCode(String code) {
// TODO: Add code here to return Product object
Product pro=new Product();
for(int i=0;i<productsArray.length;i++)
{
if(productsArray[i][0].equals(code))
{
pro.setCode(code);; //Setting the code
pro.setDescription(productsArray[i][1]); //Setting the description
Double db=Double.parseDouble(productsArray[i][2]);
pro.setPrice(db); //Setting the price
return pro;
}
}
return null;
}
public static Product[] getAllProducts() {
// TODO: Add code here to return array of Product objects
Product pro[]=new Product[productsArray.length+1]; //Making array of Product data type
for(int i=0;i<productsArray.length;i++)
{
pro[i]=new Product(); //Allocating memory
}
for(int i=0;i<productsArray.length;i++)
{
pro[i].setCode(productsArray[i][0]); //Setting the code
pro[i].setDescription(productsArray[i][1]); //Setting the description
Double db=Double.parseDouble(productsArray[i][2]); //Setting the price
pro[i].setPrice(db);
}
return pro;
// return null;
}
}
Product Class:
import java.util.*;
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);
}
}
Main Class:
import java.util.*;
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: "+product.getCode());
System.out.println("Description: "+product.getDescription());
System.out.println("Price: "+product.getPrice());
System.out.println("----------------------------------------------");
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[] products = ProductDB.getAllProducts();
System.out.println("LIST OF ALL PRODUCTS");
for (int i = 0; i < 3; 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("----------------------------------------------");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.