Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create an interface and implement it In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method: public abstract Product

Create an interface and implement it

  1. In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method:

public abstract Product get(String productCode);

  1. Modify the ProductDB class so it implements the IProductDB interface. Write the code for the new get method. Then remove the getProductByCode method.
  2. 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();

This shows that the ProductDB class implements the IProductDB interface.

  1. Run the application to make sure it works correctly.

Modify another class so it implements the interface

  1. In the ProductDB2 class, modify the code so it implements the IProductDB interface. Write the code for the new get method. Remove the getProduct method.
  2. In the Main class, modify the code so it uses ProductDB2, not ProductDB. This should be easy since both classes implement the IProductDB interface.
  3. Run the application to make sure it works correctly.

Create an interface that contains constants and use it

  1. In the murach.db package, add an interface named IProductConstants that contains the three constants that are currently in the Main class. To do that, you can move the code that declares and initializes the constants from the Main class to the interface. In the Main class, make sure to remove the code that declares and initializes the three constants.
  2. Attempt to compile the application. This should display an error message that indicates that constants arent available to the Main class.
  3. In the Main class, implement the IProductConstants interface. This should make the constants available to the Main class.
  4. Run the application to make sure that it works correctly.

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.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.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.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; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Antipatterns Avoiding The Pitfalls Of Database Programming

Authors: Bill Karwin

1st Edition

1680508989, 978-1680508987

More Books

Students also viewed these Databases questions