You need the source code to the Murach\'s Java Programming book found here: http
ID: 3550645 • Letter: Y
Question
You need the source code to the Murach's Java Programming book found here:
http://www.murach.com/downloads/javp.htm
Explanation / Answer
/*
OUTPUT for ProductCloneApp
Welcome to the Product Clone Test
SUCCESS: The clone method of the Product class is cloning data.
*/
/* OUTPUT for LineItemCloneApp
Weclome to the Line Item Clone Test
Code: java
Description: Murach's Beginning Java
Price: $44.50
Quantity: 3
Total: $133.50
Code: java
Description: Murach's Beginning Java
Price: $44.50
Quantity: 2
Total: $89.00
*/
import java.text.NumberFormat;
public class LineItem implements Cloneable
{
private Product product;
private int quantity;
private double total;
public LineItem()
{
this.product = new Product();
this.quantity = 0;
this.total = 0;
}
public LineItem(Product product, int quantity)
{
this.product = product;
this.quantity = quantity;
}
public void setProduct(Product product)
{
this.product = product;
}
public Product getProduct()
{
return product;
}
public void setQuantity(int quantity)
{
this.quantity = quantity;
}
public int getQuantity()
{
return quantity;
}
public double getTotal()
{
this.calculateTotal();
return total;
}
private void calculateTotal()
{
total = quantity * product.getPrice();
}
public String getFormattedTotal()
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(this.getTotal());
}
@Override
public String toString()
{
return
"Code: " + product.getCode() + " " +
"Description: " + product.getDescription() + " " +
"Price: " + product.getFormattedPrice() + " " +
"Quantity: " + quantity + " " +
"Total: " + this.getFormattedTotal() + " ";
}
public Object clone() throws CloneNotSupportedException
{
return super.clone();
}
}
import java.text.NumberFormat;
public class Product implements Cloneable
{
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 getFormattedPrice()
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(price);
}
@Override
public String toString()
{
return "Code: " + code + " " +
"Description: " + description + " " +
"Price: " + this.getFormattedPrice() + " ";
}
public Object clone() throws CloneNotSupportedException
{
return super.clone();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.