Question

Add appropriate descriptive comments to each line of the code, explaining why the code is in...

Add appropriate descriptive comments to each line of the code, explaining why the code is in the application.

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() + "\n" +
"Description: " + product.getDescription() + "\n" +
"Price: " + product.getFormattedPrice() + "\n" +
"Quantity: " + quantity + "\n" +
"Total: " + this.getFormattedTotal() + "\n";
}
// overrides to stop error message
@Override
public Object clone() throws CloneNotSupportedException
{
LineItem li = (LineItem) super.clone();
Product p = (Product) product.clone();
li.setProduct(p);
return li;

}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.text.NumberFormat;

// Declaring a class for representing a lineItem
public class LineItem implements Cloneable {

// instance members of class
private Product product;
private int quantity;
private double total;

// Default Constructor
public LineItem() {
this.product = new Product();
this.quantity = 0;
this.total = 0;
}

// Parameterized constructor
public LineItem(Product product, int quantity) {
this.product = product;
this.quantity = quantity;
}

// method to set the product
public void setProduct(Product product) {
this.product = product;
}

// method to get the product
public Product getProduct() {
return product;
}

// method to set the quantity
public void setQuantity(int quantity) {
this.quantity = quantity;
}

// method to get the quantity
public int getQuantity() {
return quantity;
}

// method to calculate total amount from line item
public double getTotal() {
this.calculateTotal();
return total;
}

// this is a private method, which calculates the total
// price for this lineitem and updates it.
private void calculateTotal() {
total = quantity * product.getPrice();
}

// This method is used to get a prettify string representation
// of the line item's cost with currency symbol
public String getFormattedTotal() {
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(this.getTotal());
}

// This method returns the string representation of the lineItem
// object. It prints the details in proper order.
@Override
public String toString() {
return
"Code: " + product.getCode() + "\n" +
"Description: " + product.getDescription() + "\n" +
"Price: " + product.getFormattedPrice() + "\n" +
"Quantity: " + quantity + "\n" +
"Total: " + this.getFormattedTotal() + "\n";
}

// overrides to stop error message
// Clone method is used to create the exact copy of lineItem object
// along with the instance members
@Override
public Object clone() throws CloneNotSupportedException
{
LineItem li = (LineItem) super.clone();
Product p = (Product) product.clone();
li.setProduct(p);
return li;

}
}

Add a comment
Know the answer?
Add Answer to:
Add appropriate descriptive comments to each line of the code, explaining why the code is in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Course,java import java.util.ArrayList; import java.util.Arrays; /* * To change this license header, choose License Headers in...

    Course,java import java.util.ArrayList; import java.util.Arrays; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author fenghui */ public class Course {     private String cName;     private ArrayList<Subject> cores;     private ArrayList<Major> majors;     private ArrayList<Subject> electives;     private int cCredit;          public Course(String n, int cc){         cName = n;         cCredit = cc;         cores = new ArrayList<Subject>(0);         majors =...

  • Hello everybody. Below I have attached a code and we are supposed to add comments explaining...

    Hello everybody. Below I have attached a code and we are supposed to add comments explaining what each line of code does. For each method add a precise description of its purpose, input and output.  Explain the purpose of each member variable. . Some help would be greaty appreciated. (I have finished the code below with typing as my screen wasnt big enough to fit the whole code image) } public void withdrawal (double amount) { balance -=amount; }...

  • JAVA Use the class, Invoice, provided to create an array of Invoice objects. Class Invoice includes...

    JAVA Use the class, Invoice, provided to create an array of Invoice objects. Class Invoice includes four instance variables; partNumber (type String), partDescription (type String), quantity of the item being purchased (type int0, and pricePerItem (type double). Perform the following queries on the array of Invoice objects and display the results: Use streams to sort the Invoice objects by partDescription, then display the results. Use streams to sort the Invoice objects by pricePerItem, then display the results. Use streams to...

  • Java/LinkedList Need help with a few of the TODO parts, more info below in comments in...

    Java/LinkedList Need help with a few of the TODO parts, more info below in comments in bold. Thanks, package lab4; import java.util.IdentityHashMap; public class IntNode implements Cloneable {    private int data;    private IntNode next;       public IntNode(int d, IntNode n) {        data = d;        next = n;    }       public IntNode getNext() {        return next;    }          /// Override methods from Object       @Override   ...

  • Saw this code online an im trying to understand each step. Can someone add comments to...

    Saw this code online an im trying to understand each step. Can someone add comments to this ? package javaapplication5; interface PairInterface<String>{ void setFirst(String first); void setSecond(String second); String getFirst(); String getSecond();    } class BasicPair<String> implements PairInterface{ String first; String second; BasicPair(String first,String second){ this.first=first; this.second=second; } @Override public String getFirst() { return first; } @Override public String getSecond() { return second; } @Override public void setFirst(Object first ) { this.first=(String)first; } @Override public void setSecond(Object second) { this.second=(String)second;...

  • I need help with adding comments to my code and I need a uml diagram for...

    I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...

  • Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB....

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

  • How to solve and code the following requirements (below) using the JAVA program? 1. Modify the...

    How to solve and code the following requirements (below) using the JAVA program? 1. Modify the FoodProduct Class to implement Edible, add the @Overrride before any methods that were there (get/set methods) that are also in the Edible interface. 2. Modify the CleaningProduct Class to implement Chemical, add the @Overrride before any methods that were there (get/set methods) that are also in the Chemical interface. 3. Create main class to read products from a file, instantiate them, load them into...

  • Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface....

    Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface. Couldn't figure it out. the compare to method should print 0, 1, or -1 import java.util.*; public class RectangleMain {    public static void main(String [] args) throws CloneNotSupportedException    {        /*ComparableRectangleAlsoCloneable obj1 = new ComparableRectangleAlsoCloneable(4, 5);        ComparableRectangleAlsoCloneable obj2 = (ComparableRectangleAlsoCloneable)obj1.clone();               System.out.println(obj1.toString());        System.out.println(obj1 == obj2); //false        System.out.println(obj2.toString());*/               Scanner...

  • Use java and continue stage 2 and 3 stage 1 code public abstract class BabyItem { protected String name;...

    Use java and continue stage 2 and 3 stage 1 code public abstract class BabyItem { protected String name; public BabyItem() { name=""; } public BabyItem(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { } public abstract double getCost(); } ======================================================================================== public class BabyFood extends BabyItem { private int numberOfJars; private double pricePerDozen; public BabyFood() { super(); numberOfJars = 0; pricePerDozen = 0; } public BabyFood(int numberOfJars, double pricePerDozen) {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT