OK I\'m once again lost this time it seems liek there is less to do but not sure
ID: 3694324 • Letter: O
Question
OK I'm once again lost this time it seems liek there is less to do but not sure even where to start
-Add a class named CompactDisc that inherits the Product clas. This new class should work like the Book and Software classes, but it should include public get and set methods for a private instance variable named artist. In addition, its toString method should append the artist name to the end of the string.
- Next Modify the ProductDB class so it returns a CompactDisc object
Modify the ProductDB class so it creates at least one CompactDisc object.
Foe example
code: sgtp
Description: Sgt. Pepper's Lonely Hearts Club Band
Price: $15oo
Artist: The Beatles
-Last add a protected varrible
Open the product class and change the access modifer for the count varible from public to protected
Here is all the files in the project i can open in netbeans not sure which ones or if all need soemthing changed to do this
this is the book.java
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 + " ";
}
}
this is the product.java
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;
}
}
This is the Product.app.java file
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();
}
}
}
This is the ProductDB.java file
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;
}
}
And last is the software.java file
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;
}
}
Explanation / Answer
ProductApp.java
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.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("sgtp"))
{
CompactDisc c = new CompactDisc();
c.setCode("sgtp");
c.setDescription("sgt. Pepper's Lonely Hearts Club Band");
c.setPrice(15.00);
c.setartist("The Beatles");
p = c; // set Product object equal to the Software object
}
return p;
}
}
Software.java
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 + " ";
}
}
Product.java
import java.text.NumberFormat;
public class Product
{
private String code;
private String description;
private double price;
protected 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;
}
}
Book.java
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 + " ";
}
}
CompactDisc.java
public class CompactDisc extends Product
{
private String artist;
public CompactDisc()
{
super();
artist = "";
count++;
}
public void setartist(String artist)
{
this.artist = artist;
}
public String getartist(){
return artist;
}
@Override
public String toString()
{
return super.toString() +
"Artist: " + artist + " ";
}
}
sample output
Welcome to the Product Selector
Enter product code: 1234
No product matches this product code.
Product count: 0
Continue? (y/n): y
Enter product code: mcb2
Code: mcb2
Description: Murach's Mainframe COBOL
Price: $59.50
Author: Mike Murach
Product count: 1
Continue? (y/n): n
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.