Changes that need to be made Create CompactDisc class as sub
Changes that need to be made:
Create CompactDisc class as subclass of Product
CompactDisc class works like Book and Software classes
CompactDisc class has private artist variable with accessor and mutator methods
CompactDisc class overrides toString method of Product by appending artist
Modify ProductDB to create at least one CompactDisc object
Modify Product to use protected access for the count variable
Insure that all classes compile
Insure that count is properly maintained when running ProductApp
Book code:
public class Book extends Product
{
private String author;
public Book()
{
super();
author = \"\";
count++;
}
public void setAuthor(String author)
{
this.author = author;
}
public String getAuthor(){
return author;
}
@Override
public String toString()
{
return super.toString() +
\"Author: \" + author + \"\ \";
}
}
Product code:
import java.text.NumberFormat;
public class Product
{
private String code;
private String description;
private double price;
public static int count = 0;
public Product()
{
code = \"\";
description = \"\";
price = 0;
}
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 getFormattedPrice()
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(price);
}
@Override
public String toString()
{
return \"Code: \" + code + \"\ \" +
\"Description: \" + description + \"\ \" +
\"Price: \" + this.getFormattedPrice() + \"\ \";
}
public static int getCount()
{
return count;
}
}
ProductApp Code:
import java.util.Scanner;
public class ProductApp
{
public static void main(String args[])
{
// display a weclome message
System.out.println(\"Welcome to the Product Selector\ \");
// perform 1 or more selections
Scanner sc = new Scanner(System.in);
String choice = \"y\";
while (choice.equalsIgnoreCase(\"y\"))
{
System.out.print(\"Enter product code: \");
String productCode = sc.next(); // read the product code
sc.nextLine(); // discard any other data entered on the line
// get the Product object
Product p = ProductDB.getProduct(productCode);
// display the output
System.out.println();
if (p != null)
System.out.println(p.toString());
else
System.out.println(\"No product matches this product code.\ \");
System.out.println(\"Product count: \" + Product.getCount() + \"\ \");
// see if the user wants to continue
System.out.print(\"Continue? (y/n): \");
choice = sc.nextLine();
System.out.println();
}
}
}
ProductDB Code:
public class ProductDB
{
public static Product getProduct(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 data
Product p = null;
if (productCode.equalsIgnoreCase(\"java\") ||
productCode.equalsIgnoreCase(\"jsps\") ||
productCode.equalsIgnoreCase(\"mcb2\"))
{
Book b = new Book();
if (productCode.equalsIgnoreCase(\"java\"))
{
b.setCode(productCode);
b.setDescription(\"Murach\'s Beginning Java\");
b.setPrice(49.50);
b.setAuthor(\"Andrea Steelman\");
}
else if (productCode.equalsIgnoreCase(\"jsps\"))
{
b.setCode(productCode);
b.setDescription(\"Murach\'s Java Servlets and JSP\");
b.setPrice(49.50);
b.setAuthor(\"Andrea Steelman\");
}
else if (productCode.equalsIgnoreCase(\"mcb2\"))
{
b.setCode(productCode);
b.setDescription(\"Murach\'s Mainframe COBOL\");
b.setPrice(59.50);
b.setAuthor(\"Mike Murach\");
}
p = b; // set Product object equal to the Book object
}
else if (productCode.equalsIgnoreCase(\"txtp\"))
{
Software s = new Software();
s.setCode(\"txtp\");
s.setDescription(\"TextPad\");
s.setPrice(27.00);
s.setVersion(\"4.7.3\");
p = s; // set Product object equal to the Software object
}
return p;
}
}
Software Code:
public class Software extends Product
{
private String version;
public Software()
{
super();
version = \"\";
count++;
}
public void setVersion(String version)
{
this.version = version;
}
public String getVersion(){
return version;
}
@Override
public String toString()
{
return super.toString() +
\"Version: \" + version + \"\ \";
}
}
If it wouldnt be to much trouble to paste back the sections where the code was changed so I can understand and learn that\'d be great. Thanks
Solution
Product.java
protected static int count = 0;
CompactDisc.java
public class CompactDisc extends Product {
public CompactDisc()
{
super();
count++;
}
private String artist;
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
@Override
public String toString()
{
return super.toString() +
\"Artist: \" + artist + \"\ \";
}
}
ProductDB.java
public class ProductDB
{
public static Product getProduct(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 data
Product p = null;
if (productCode.equalsIgnoreCase(\"java\") ||
productCode.equalsIgnoreCase(\"jsps\") ||
productCode.equalsIgnoreCase(\"mcb2\"))
{
Book b = new Book();
if (productCode.equalsIgnoreCase(\"java\"))
{
b.setCode(productCode);
b.setDescription(\"Murach\'s Beginning Java\");
b.setPrice(49.50);
b.setAuthor(\"Andrea Steelman\");
}
else if (productCode.equalsIgnoreCase(\"jsps\"))
{
b.setCode(productCode);
b.setDescription(\"Murach\'s Java Servlets and JSP\");
b.setPrice(49.50);
b.setAuthor(\"Andrea Steelman\");
}
else if (productCode.equalsIgnoreCase(\"mcb2\"))
{
b.setCode(productCode);
b.setDescription(\"Murach\'s Mainframe COBOL\");
b.setPrice(59.50);
b.setAuthor(\"Mike Murach\");
}
p = b; // set Product object equal to the Book object
}
else if (productCode.equalsIgnoreCase(\"txtp\"))
{
Software s = new Software();
s.setCode(\"txtp\");
s.setDescription(\"TextPad\");
s.setPrice(27.00);
s.setVersion(\"4.7.3\");
p = s; // set Product object equal to the Software object
}
else if (productCode.equalsIgnoreCase(\"cd\"))
{
CompactDisc compactDisc = new CompactDisc();
compactDisc.setArtist(\"Michael Jackson\");
compactDisc.setCode(\"cd\");
compactDisc.setDescription(\"abcd\");
compactDisc.setPrice(10.00);
p = compactDisc; //set Product object equal to CompactDisc object
}
return p;
}
}






