Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class AmazonOrder which keeps track of the items a person orders on Amaz

ID: 3795908 • Letter: W

Question

Write a class AmazonOrder which keeps track of the items a person orders on Amazon. We want to be able to add an item of a specific cost and remove an item of a specific cost. We also want to able able to get the subtotal, total and a string representation of the contents of the AmazonOrder

AmazonOrder has a no-argument constructor which initializes the instance variables (subtotal to 0, contents to the empty string)

It has the following methods:

1. public void add(double cost) adds this cost to the subtotal for this AmazonOrder and records the item added in the contents (see below)
2. public void remove(double cost)subtracts this cost from the subtotal for this AmazonOrder and records the item removed in the contents (see below)
3. public double getSubtotal() gets the subtotal for this AmazonOrder
4. getTotal() Gets the total which is the subtotal plus tax. The tax is 8.5%. Define 8.5 as a constant in the method. (not as an instance variable
5. getContents() returns the string representation of the cost of the items added to and removed from in the order and the total cost. Be sure to call getTotal(). Do not calculate the total in this method.
6. reset() returns the object to its original state where no items are in the order. The string representing the items in the order will be set to the empty string and the subtotal will be set to 0
To add to the order: add the string Add Item or Remove Item, a colon, a space, and then the value of the transaction and a newline

The format of the string returned by getContents:

Add Item: 2.59
Add Item: 8.25
Remove Item: 2.59
Total: 8.95125


The total is displayed at the end.

NOTE: I should get the same output if I call getContents() twice in a row.

You need to provide a Javadoc comment for your all methods, the constructor and the class itself.

the AmazonOrderPrinter is given:

Thank you

Explanation / Answer

AmazonOrder.java

public class AmazonOrder {
   //Declaring instance variables
   private double subtotal;
   private String contents;
  
   //Zero Argumented constructor
   public AmazonOrder() {
       super();
       this.subtotal = 0;;
       this.contents = "";
   }
  
   //This method will add the item
   public void add(double cost)
   {
       subtotal+=cost;
       contents+="Add Item: "+cost+" ";
   }
     
   //This method will remove the item
   public void remove(double cost)
   {
       subtotal-=cost;
       contents+="Remove Item: "+cost+" ";
   }
     
   //This method will return the subtotal
   public double getSubtotal()
   {
       return subtotal;
   }
     
   //This method will return the total (which includes subtotal+tax)
   public double getTotal()
   {
       final double TAX=8.5;
       double total=getSubtotal()+(getSubtotal()*8.5/100);
       return total;
   }
     
   //This method will display the contents
   public String getContents()
   {
       return contents+"Total: "+getTotal()+" ";
   }
     
   //This method will reset the subtotal and contents variables
   public void reset()
   {
       subtotal=0;
       contents="";
   }
}

____________________

AmazonOrderPrinter.java

/**
* Tester for the AmazonOrderPrinter class
*
* @author Kathleen O'Brien
*/
public class AmazonOrderPrinter
{
public static void main(String[] args)
{
AmazonOrder order = new AmazonOrder();
order.add(12.59);
order.add(18.25);
order.remove(12.59);
System.out.println(" Contents");
System.out.println(order.getContents());
order.reset();
  
order.add(10.25);
order.add(11.75);
order.add(15.50);
order.remove(15.50);
order.add(23);
System.out.println("Subtotal: " + order.getSubtotal());
System.out.println("Total: " + order.getTotal());
System.out.println(" Contents");
System.out.println(order.getContents());
System.out.println(" Contents");
System.out.println(order.getContents());
}
}

_______________________

Output:


Contents
Add Item: 12.59
Add Item: 18.25
Remove Item: 12.59
Total: 19.80125

Subtotal: 45.0
Total: 48.825

Contents
Add Item: 10.25
Add Item: 11.75
Add Item: 15.5
Remove Item: 15.5
Add Item: 23.0
Total: 48.825


Contents
Add Item: 10.25
Add Item: 11.75
Add Item: 15.5
Remove Item: 15.5
Add Item: 23.0
Total: 48.825

____________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote