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!
Add code to the Account class and create a new class called BalanceComparator. import java.util.*; public...
Add code to the Account class and create a new class called BalanceComparator. import java.util.*; public final class Account implements Comparable {     private String firstName;     private String lastName;     private int accountNumber;     private double balance;     private boolean isNewAccount;     public Account(             String firstName,             String lastName,             int accountNumber,             double balance,             boolean isNewAccount     ) {         this.firstName = firstName;         this.lastName = lastName;         this.accountNumber = accountNumber;         this.balance = balance;         this.isNewAccount = isNewAccount;     }     /**      * TO DO: override equals      */     @Override     public boolean equals(Object other) {...
With the code that is being tested is: import java.util.Random; public class GVdate { private int...
With the code that is being tested is: import java.util.Random; public class GVdate { private int month; private int day; private int year; private final int MONTH = 1; private final int DAY = 9; private static Random rand = new Random(); /** * Constructor for objects of class GVDate */ public GVdate() { this.month = rand.nextInt ( MONTH) + 1; this.day = rand.nextInt ( DAY );    } public int getMonth() {return this.month; } public int getDay() {return this.day;...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class...
Can someone please convert this java code to C code? import java.util.LinkedList; import java.util.List; public class Phase1 { /* Translates the MAL instruction to 1-3 TAL instructions * and returns the TAL instructions in a list * * mals: input program as a list of Instruction objects * * returns a list of TAL instructions (should be same size or longer than input list) */ public static List<Instruction> temp = new LinkedList<>(); public static List<Instruction> mal_to_tal(List<Instruction> mals) { for (int...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT