1. In the murach.db package, create an interface named IProductDB. This interfac
ID: 3598778 • Letter: 1
Question
1. In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method:
public abstract Product get(String productCode);
2. Modify the ProductDB class so it implements the IProductDB interface. Write the code for the new ‘get’ method. Then remove the getProductByCode method.
3. In the Main class, modify the code so it works with the new ProductDB class. This code should create an instance of the IProductDB interface like this:
IProductDB db = new ProductDB();
4. This shows that the ProductDB class implements the IProductDB interface.
5. Run the application to make sure it works correctly.
Code Provided:
-----------
package murach.ui;
import murach.db.ProductDB;
import murach.business.Product;
public class Main {
public static void main(String args[]) {
ProductDB db = new ProductDB();
System.out.println("Welcome to the Product Lister ");
final int CODE_WIDTH = 10;
final int DESC_WIDTH = 34;
final int PRICE_WIDTH = 10;
// set up display string
StringBuilder list = new StringBuilder();
list.append(StringUtil.pad("Code", CODE_WIDTH));
list.append(StringUtil.pad("Description", DESC_WIDTH));
list.append(StringUtil.pad("Price", PRICE_WIDTH));
list.append(" ");
list.append(
StringUtil.pad("=========", CODE_WIDTH));
list.append(
StringUtil.pad("=================================", DESC_WIDTH));
list.append(
StringUtil.pad("=========", PRICE_WIDTH));
list.append(" ");
String choice = "y";
while (choice.equalsIgnoreCase("y")) {
// get the input from the user
String productCode = Console.getString("Enter product code: ");
Product product = db.getProductByCode(productCode);
list.append(
StringUtil.pad(product.getCode(), CODE_WIDTH));
list.append(
StringUtil.pad(product.getDescription(), DESC_WIDTH));
list.append(
StringUtil.pad(product.getPriceFormatted(), PRICE_WIDTH));
list.append(" ");
// see if the user wants to continue
choice = Console.getString("Another product? (y/n): ");
System.out.println();
}
System.out.println(list);
}
}
package murach.ui;
import java.util.Scanner;
public class Console {
private static Scanner sc = new Scanner(System.in);
public static void displayLine() {
System.out.println();
}
public static void displayLine(String s) {
System.out.println(s);
}
public static String getString(String prompt) {
System.out.print(prompt);
String s = sc.nextLine();
return s;
}
public static int getInt(String prompt) {
int i = 0;
while (true) {
System.out.print(prompt);
try {
i = Integer.parseInt(sc.nextLine());
break;
} catch (NumberFormatException e) {
System.out.println("Error! Invalid integer. Try again.");
}
}
return i;
}
public static double getDouble(String prompt) {
double d = 0;
while (true) {
System.out.print(prompt);
try {
d = Double.parseDouble(sc.nextLine());
break;
} catch (NumberFormatException e) {
System.out.println("Error! Invalid decimal. Try again.");
}
}
return d;
}
}
package murach.ui;
public class StringUtil {
public static String pad(String s, int length) {
if (s.length() < length) {
StringBuilder sb = new StringBuilder(s);
while (sb.length() < length) {
sb.append(" ");
}
return sb.toString();
} else {
return s.substring(0, length);
}
}
}
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);
}
}
package murach.db;
import murach.business.Product;
public class ProductDB {
public Product getProductByCode(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
// 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.db;
import murach.business.Product;
public class ProductDB2 {
private String[][] productsArray = {
{"java", "Murach's Java Programming", "57.50"},
{"jsp", "Murach's Java Servlets and JSP", "57.50"},
{"android", "Murach's Android Programming", "57.50"},
{"mysql", "Murach's MySQL", "54.50"}
};
public Product getProduct(String productCode) {
for (String[] row : productsArray) {
String code = row[0];
if (productCode.equals(code)) {
String description = row[1];
String price = row[2];
Product p = new Product(
code, description, Double.parseDouble(price));
return p;
}
}
return null;
}
}
Explanation / Answer
// Main.java
package murach.ui;
import murach.business.Product;
import murach.db.IProductDB;
import murach.db.ProductDB;
public class Main {
public static void main(String args[]) {
IProductDB db = new ProductDB();
System.out.println("Welcome to the Product Lister ");
final int CODE_WIDTH = 10;
final int DESC_WIDTH = 34;
final int PRICE_WIDTH = 10;
// set up display string
StringBuilder list = new StringBuilder();
list.append(StringUtil.pad("Code", CODE_WIDTH));
list.append(StringUtil.pad("Description", DESC_WIDTH));
list.append(StringUtil.pad("Price", PRICE_WIDTH));
list.append(" ");
list.append(
StringUtil.pad("=========", CODE_WIDTH));
list.append(
StringUtil.pad("=================================", DESC_WIDTH));
list.append(
StringUtil.pad("=========", PRICE_WIDTH));
list.append(" ");
String choice = "y";
while (choice.equalsIgnoreCase("y")) {
// get the input from the user
String productCode = Console.getString("Enter product code: ");
Product product = db.get(productCode);
list.append(
StringUtil.pad(product.getCode(), CODE_WIDTH));
list.append(
StringUtil.pad(product.getDescription(), DESC_WIDTH));
list.append(
StringUtil.pad(product.getPriceFormatted(), PRICE_WIDTH));
list.append(" ");
// see if the user wants to continue
choice = Console.getString("Another product? (y/n): ");
System.out.println();
}
System.out.println(list);
}
}
//console.java
package murach.ui;
import java.util.Scanner;
public class Console {
private static Scanner sc = new Scanner(System.in);
public static void displayLine() {
System.out.println();
}
public static void displayLine(String s) {
System.out.println(s);
}
public static String getString(String prompt) {
System.out.print(prompt);
String s = sc.nextLine();
return s;
}
public static int getInt(String prompt) {
int i = 0;
while (true) {
System.out.print(prompt);
try {
i = Integer.parseInt(sc.nextLine());
break;
} catch (NumberFormatException e) {
System.out.println("Error! Invalid integer. Try again.");
}
}
return i;
}
public static double getDouble(String prompt) {
double d = 0;
while (true) {
System.out.print(prompt);
try {
d = Double.parseDouble(sc.nextLine());
break;
} catch (NumberFormatException e) {
System.out.println("Error! Invalid decimal. Try again.");
}
}
return d;
}
}
//StringUtil.java
package murach.ui;
public class StringUtil {
public static String pad(String s, int length) {
if (s.length() < length) {
StringBuilder sb = new StringBuilder(s);
while (sb.length() < length) {
sb.append(" ");
}
return sb.toString();
} else {
return s.substring(0, length);
}
}
}
//ProductDB2.java
package murach.db;
import murach.business.Product;
public class ProductDB2 {
private String[][] productsArray = {
{"java", "Murach's Java Programming", "57.50"},
{"jsp", "Murach's Java Servlets and JSP", "57.50"},
{"android", "Murach's Android Programming", "57.50"},
{"mysql", "Murach's MySQL", "54.50"}
};
public Product getProduct(String productCode) {
for (String[] row : productsArray) {
String code = row[0];
if (productCode.equals(code)) {
String description = row[1];
String price = row[2];
Product p = new Product(
code, description, Double.parseDouble(price));
return p;
}
}
return null;
}
}
//ProductDB.java
package murach.db;
import murach.business.Product;
public class ProductDB implements IProductDB {
@Override
public Product get(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
// 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;
}
}
//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);
}
}
//IProductDB.java Interface
package murach.db;
import murach.business.Product;
public interface IProductDB {
public abstract Product get(String productCode);
}
Sample Output:
Welcome to the Product Lister
Enter product code: java
Another product? (y/n): y
Enter product code: mysql
Another product? (y/n): n
Code Description Price
========= ================================= =========
java Murach's Java Programming $57.50
mysql Murach's MySQL $54.50
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.