Question

In: Computer Science

Please add comments to this code! Item Class: import java.text.NumberFormat; public class Item {    private...

Please add comments to this code!

Item Class:

import java.text.NumberFormat;
public class Item {

   private String name;
   private double price;
   private int bulkQuantity;
   private double bulkPrice;

   /***
   *
   * @param name
   * @param price
   * @param bulkQuantity
   * @param bulkPrice
   */
   public Item(String name, double price, int bulkQuantity, double bulkPrice) {
       this.name = name;
       this.price = price;
       this.bulkQuantity = bulkQuantity;
       this.bulkPrice = bulkPrice;

   }

   public Item(String name, double price) {
       if (price < 0) {
           throw new IllegalArgumentException();
       }

       this.name = name;
       this.price = price;

   }

   /***
   *
   * @param quantity
   * @return
   */
   public double priceFor(int quantity) {
       double actual = 0;
       if (quantity < 0) {
           throw new IllegalArgumentException();
       } else {
           if (bulkQuantity!=0) {
               actual = (quantity / bulkQuantity) * bulkPrice
                       + (quantity % bulkQuantity) * price;
           } else {
               actual = quantity * price;
           }
       }

       return actual;

   }

   public boolean equals(Item ietm) {
       return this.name.equals(ietm.name);
   }

   @Override
   public String toString() {
       NumberFormat format = (NumberFormat) NumberFormat.getCurrencyInstance();

       format.setMinimumFractionDigits(2);
       format.setMaximumFractionDigits(2);
       String str = "";
       str = name + ", " + format.format(price);
       if (bulkPrice != 0) {
           str += " ( " + bulkQuantity + " for " + format.format(bulkPrice)
                   + " )";
       }
       return str;
   }
}

JAVA Code that needs comments:

public class ItemOrder {
   private final Item item;
   private final int quantity;

   /**
   *
   * @param item
   * @param quantity
   */
   public ItemOrder(Item item, int quantity) {
       this.item = item;
       this.quantity = quantity;
   }

   public double getPrice() {
       return item.priceFor(quantity);
   }

   public Item getItem() {
       return item;
   }

}

Thanks!!

Solutions

Expert Solution

Item.java :

//import java package
import java.text.NumberFormat;
//Java class with name Item
public class Item {
   //fields
   private String name;
   private double price;
   private int bulkQuantity;
   private double bulkPrice;

   /***
   *
   * @param name
   * @param price
   * @param bulkQuantity
   * @param bulkPrice
   */
   //constructor with parameters
   public Item(String name, double price, int bulkQuantity, double bulkPrice) {
       this.name = name;//set name
       this.price = price;//set price
       this.bulkQuantity = bulkQuantity;//set bulk quantity
       this.bulkPrice = bulkPrice;//set bulk price

   }
   //parameterized constructor
   public Item(String name, double price) {
       //checking price
       if (price < 0) {
           //if price is less than 0 then throw exception
           throw new IllegalArgumentException();
       }

       this.name = name;//set name
       this.price = price;//set price

   }

   /***
   *
   * @param quantity
   * @return
   */
   //method priceFor() with one integer parameter named quantity
   public double priceFor(int quantity) {
       double actual = 0;//variable used to store actual price
       //using if else checking quantity
       if (quantity < 0) {
           //if quantity is less than 0 then throw exception
           throw new IllegalArgumentException();
       } else {
           //checking bulk quantity
           if (bulkQuantity != 0) {
               //when bulkQuantity is not 0 then
               //calculate actual price
               actual = (quantity / bulkQuantity) * bulkPrice + (quantity % bulkQuantity) * price;
           } else {
               //when bulkQuantity is zero
               actual = quantity * price;
           }
       }

       return actual;//return variable actual

   }
   //equals() method , this method will compare item name
   public boolean equals(Item ietm) {
       return this.name.equals(ietm.name);
   }
   //method toString()
   @Override
   public String toString() {
       //instance of NumberFormat class
       NumberFormat format = (NumberFormat) NumberFormat.getCurrencyInstance();
       //setting format for fractional digit
       format.setMinimumFractionDigits(2);
       format.setMaximumFractionDigits(2);
       String str = "";//declaring variable for concatenating name and price
       str = name + ", " + format.format(price);
       //checking bulkPrice
       if (bulkPrice != 0) {
           //when bulkPrice is not zero then
           str += " ( " + bulkQuantity + " for " + format.format(bulkPrice) + " )";
       }
       return str;//return variable str
   }
}

**************************************************

ItemOrder.java :

//Java class named ItemOrder
public class ItemOrder {
   //private final fields
private final Item item;
private final int quantity;

/**
*
* @param item
* @param quantity
*/
//constructor with parameters
public ItemOrder(Item item, int quantity) {
this.item = item;//set item
this.quantity = quantity;//set quantity
}
//method to return price
public double getPrice() {
   //calling static method from item class
return item.priceFor(quantity);
}
//This is getter method for the item
public Item getItem() {
return item;
}

}


Related Solutions

Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog { String catalog_name; ArrayList<Item> list; Catalog(String cs_Gift_Catalog) { list=new ArrayList<>(); catalog_name=cs_Gift_Catalog; } String getName() { int size() { return list.size(); } Item get(int i) { return list.get(i); } void add(Item item) { list.add(item); } } Thanks!
public class StringNode { private String item; private StringNode next; } public class StringLL { private...
public class StringNode { private String item; private StringNode next; } public class StringLL { private StringNode head; private int size; public StringLL(){ head = null; size = 0; } public void add(String s){ add(size,s); } public boolean add(int index, String s){ ... } public String remove(int index){ ... } } In the above code add(int index, String s) creates a StringNode and adds it to the linked list at position index, and remove(int index) removes the StringNode at position...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
Fill in the following blanks for java code::: import java.util.NoSuchElementException; public class CircularQueue<E> {    private...
Fill in the following blanks for java code::: import java.util.NoSuchElementException; public class CircularQueue<E> {    private E[] queue;    private int front = 0, rear = 0;    private static final int DEFAULT_CAPACITY = 5;       public CircularQueue(int capacity)    {    queue = (E[]) new Object[capacity + 1];    }       public CircularQueue()    {        this(DEFAULT_CAPACITY); }       //Add a method that will determine if the queue is empty. Recall that the queue is...
blueJ code given: import java.until.ArrayList; public class TestArrayList; { private ArrayList<TestArrays> testArrayList; /** * Complete the...
blueJ code given: import java.until.ArrayList; public class TestArrayList; { private ArrayList<TestArrays> testArrayList; /** * Complete the constructor for the class. Instantiate the testArrayList *and call the fillArrayList method to add objects to the testArrayList. * @param numElements The number of elements in the ArrayList */ public TestArrayLists(int numElements) { } /** * Complete the fillArrayList method. It should fill the testArrayList with * TestArrays objects consisting of 10 element int Array of random numbers. * @param numElements The number of...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main {   public static void main( String[] args ) {     Scanner myInput = new Scanner(System.in); // Create a Scanner object     System.out.println("Enter (3) digits: ");     int W = myInput.nextInt();     int X = myInput.nextInt();     int Y = myInput.nextInt();      } } Use the tools described thus far to create additional code that will sort the integers in either monotonic ascending or descending order. Copy your code and...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT