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

Develop a program to emulate a purchase at a store. This program will have two c

ID: 3666054 • Letter: D

Question

Develop a program to emulate a purchase at a store. This program will have two classes, a List class and a Purchase class. The List class will represent an individual line item of merchandise that a customer is purchasing. The Purchase class will combine several List objects and calculate an overall total price for the line item within the Purchase. There will also be two test classes, one for the List class and one for the Purchase class.


Design and build a List class. This class will have three instance variables. There will be an itemName variable that will hold the identification of the line item (such as, "Colgate Toothpaste"); a quantity variable that will hold the quantity of the item being purchased; and a price variable that will hold the retail price of the item. The List class should have a constructor, accessors for the instance variables, a method to compute the total price for the line item, a method to update the quantity, and a method to convert the state of the object to a string. Using Unified Modeling Language (UML), the class diagram looks like this:


List
- itemName : String
- quantity : int
- price : double

------------------------------------
+ List( String, int, double )
+ getName( ) : String
+ getQuantity( ) : int
+ getPrice( ) : double
+ getTotalPrice( ) : double
+ setQuantity( int )
+ setPrice( double )
+ toString( ) : String


The constructor will assign the first parameter to the instance variable itemName, the second parameter to the instance variable quantity, and the third parameter to the instance variable price.

The class will have three accessor methods—getName( ), getQuantity( ), and getPrice( )—that will return the value of each respective instance variable.

The class will have two mutator methods, setQuantity( int ) and setPrice( double ), that will update the quantity and price, respectively, of the item associated with the line of the Purchase.

The method getTotalPrice( ) handles the conversion of the quantity and price into a total price for the line item.

The method toString( ) allows access to the state of the object in a printable or readable form. It converts the variables to a single string that is neatly formatted. Note: Refer to the textbook for a discussion of escape sequences. These are characters that can be inserted into strings and, when printed, will format the display neatly. An escape sequence for the tab character can be inserted to get a tabular form when printing. This tab character is " ". The List class will have a toString( ) method that concatenates itemName, quantity, price, and total price—separated by tab characters—and returns this new string. When printing an object, the toString( ) method will be implicitly called, which in this case, will print a string that will look something like: Colgate Toothpaste qty 2 @ $2.99 $5.98

Build a Purchase class that will store information about the items being purchased in a single Purchase. It should include a customerID and customerName. It should also include an ArrayList to hold information about each item that the customer is purchasing as part of the Purchase. Note: You must use an ArrayList, not an array.

Build a PurchaseTest class to test the application. The test class should not require any interaction with the user. It should verify the correct operation of the constructor and all methods in the Purchase class.


Specific Requirements for the Purchase Class
The Purchase class should have a constructor with two parameters. The first is an integer containing the customer’s ID and the second is a String containing the customer’s name.

There should be a method to allow the addition of a line item to the transcript. The three parameters for the addList method will be (1) the item name, (2) the quantity, and (3) the single item price.

There should be a method to allow the updating of a line item already in the Purchase. Notice that updating an item means changing the quantity or price (or both). The parameters for the updateItem method are also (1) the item name, (2) the quantity, and (3) the single item price.

Notice that the updating of a specific line item requires a search through the ArrayList to find the desired item. Anytime a search is done, the possibility exists that the search will be unsuccessful. It is often difficult to decide what action should be taken when such an "exception" occurs. Since exception handling is not covered until later in this textbook, make some arbitrary decisions for this project. If the item to be updated is not found, take the simplest action possible and do nothing. Do not print an error message to the screen. Simply leave the Purchase unchanged.

The Purchase class needs a method called getTotalPrice to return the total price of the Purchase.

There should also be a method to return information about a specific line item. It should return a single String object in the same format described for the List class: Colgate Toothpaste qty 2 @ $2.99 $5.98 Again, the possibility exists that the search for a specific line item will fail. In this instance, you should return a string containing a message similar to this: Colgate Toothpaste not found.

The final method needed is a toString method. It should return the Purchase information in a single String object. It should use the following format: Customer ID : 12345 Customer Name : John Doe Colgate Toothpaste qty 2 @ $2.99 $5.98 Bounty Paper Towels qty 1 @ $1.49 $1.49 Kleenex Tissue qty 1 @ $2.49 $2.49 Purchase Total $9.96 Notice that a newline character " " can be inserted into the middle of a string. Ex. int age = 30; String temp = "John Doe is " + age + " " + " years old"; The output would be: John Doe is 30 years old

Notice also that " " is a single character and could actually go inside single or double quotes, depending on the circumstances.


Here is a UML diagram for the Purchase class as described above. Notice that private instance variables and methods may be added, as needed. For all public methods use exactly the name given below:


Purchase
- Lists : ArrayList<List>
- customerID : int
- customerName : String

-------------------------------
+ Purchase( int, String )
+ addList( String, int, double )
+ updateItem( String, int, double )
+ getTotalPrice( ) : double
+ getList( String ) : String
+ toString( ) : String

Explanation / Answer

package com.he.capillary;

public class List {

   private String itemName;
   private int quantity;
   private double price;

   public String getItemName() {
       return itemName;
   }

   public int getQuantity() {
       return quantity;
   }

   public void setQuantity(int quantity) {
       this.quantity = quantity;
   }

   public double getPrice() {
       return price;
   }

   public void setPrice(double price) {
       this.price = price;
   }

   public List(String itemName, int quantity, double price) {
       super();
       this.itemName = itemName;
       this.quantity = quantity;
       this.price = price;
   }

   @Override
   public String toString() {
       return ""+this.getItemName()+" qty "+this.getQuantity()+" @ $"+this.getPrice()+" $"+this.getTotalPrice()+" ";
   }
  
   double getTotalPrice(){
       return this.getPrice()*this.getQuantity();
   }

}

package com.he.capillary;

import java.util.ArrayList;

public class Purchase {

   private ArrayList<List> Lists = new ArrayList<List>();
   private int customerID;
   private String customerName;

   public void addList(String itemName, int quantity, double price) {
       this.Lists.add(new List(itemName, quantity, price));
   }

   public void updateItem(String itemName, int quantity, double price) {
       for (List list : Lists) {
           if (list.getItemName().equalsIgnoreCase(itemName)) {
               List temp = new List(itemName, quantity, price);
               list.setPrice(temp.getPrice());
               list.setQuantity(temp.getQuantity());
           }
       }
   }

   public double getTotalPrice() {
       double total = 0.0;
       for (List list : Lists) {
           total += list.getTotalPrice();
       }
       return total;
   }

   public Purchase(int customerID, String customerName) {
       super();
       this.customerID = customerID;
       this.customerName = customerName;
   }

   public String getList(String itemName) {
       for (List list : Lists) {
           if (list.getItemName().equalsIgnoreCase(itemName)) {
               return list.toString();
           }
       }
       return itemName + " not found.";
   }

   @Override
   public String toString() {
       StringBuilder sb = new StringBuilder();
       sb.append("CustomerID=" + customerID + ", CustomerName=" + customerName + " ");
       for (List list : Lists) {
           sb.append(list.toString());
       }
       sb.append("Purchase Total $"+getTotalPrice());
       return sb.toString();
   }

}