JAVAWITHNETBEANS Product class package jobone import javatex
<*>*<>J>A>V>A,//W/*I*T*H__N^E^T&B*E)A*N*S)()()(((&(&*&*^*&^%*%*^&^$$%
***************************Product class********************************************
package job.one;
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 class*****************************************
package job.two;
import job.one.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;
}
}
*****************************ProductDB2 class***********************************************
package job.two;
import job.one.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;
}
}
**************************Console class********************************************************
package job.three;
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;
}
}
******************************Main class*************************************************
package job.three;
import job.two.ProductDB;
import job.one.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);
}
}
****************************StringUtil class**************************************
package job.three;
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);
}
}
}
Solution
Hello there ,
please find below code and its output. You can remove \"getProductByCode\" method from ProductDB class. It is not used now.
I am putting IProductDBInterface and modified ProductDB and Main class.
IProductDB====
package job.two;
import job.one.Product;
/**
*
* @author welcome
*/
public interface IProductDB {
public abstract Product get(String productCode);
}
=====
======ProductDB=========
package job.two;
import job.one.Product;
public class ProductDB implements IProductDB {
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;
}
@Override
public Product get(String productCode) {
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;
}
}
===================
=========Main=========
package job.three;
import job.two.ProductDB;
import job.one.Product;
import job.two.IProductDB;
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);
}
}
=======
===O/P=====
run:
Welcome to the Product Lister
Enter product code: java
Another product? (y/n): y
Enter product code: jsp
Another product? (y/n): n
Code Description Price
========= ================================= =========
java Murach\'s Java Programming $57.50
jsp Murach\'s Java Servlets and JSP $57.50
BUILD SUCCESSFUL (total time: 9 seconds)
Let me know if you have any doubts.






