Developaprogramtoemulateapurchasetransactionataretailstore.This program will hav
ID: 3689927 • Letter: D
Question
Developaprogramtoemulateapurchasetransactionataretailstore.This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual line item of merchandise that a customer is purchasing. The Transaction class will combine several LineItem objects and calculate an overall total price for the line item within the transaction. There will also be two test classes, one for the LineItem class and one for the Transaction class.
DesignandbuildaLineItemclass.Thisclasswillhavethreeinstancevariables. 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 LineItem 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.
a. Theconstructorwillassignthefirstparametertotheinstancevariable itemName, the second parameter to the instance variable quantity, and the third parameter to the instance variable price.
b. The class will have three accessor methods—getName( ), getQuantity( ), and getPrice( )—that will return the value of each respective instance variable.
c. 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 transaction.
d. The method getTotalPrice() handles the conversion of the quantity and price into a total price for the line item.
e. 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.
The LineItem 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 Transaction class that will store information about the items being purchased in a single transaction. 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 transaction.
Note: You must use an ArrayList, not an array.
Build a TransactionTest 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 Transaction class.
Transaction
Here is what I have so far:
/*
* 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.
*/
package programming.project1;
/**
*
* @author lancesimerly
*/
public class LineItem {
private String itemName;
private int quantity;
private double price;
public LineItem(String name, int number, double cost){
itemName = name;
quantity = number;
price = cost;
}
public String getItemName(){
return itemName;
}
public int getQuantity(){
return quantity;
}
public double getPrice(){
return price;
}
public double getTotalPrice(){
double totalPrice;
totalPrice = quantity * price;
return totalPrice;
}
public void setQuantity(int x){
quantity = x;
}
public void setPrice(double y){
price = y;
}
public String toString(){
String str;
str = (itemName + " qty " + quantity + " @ $" + price + " "
+ "$" + getTotalPrice());
return str;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
* 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.
*/
package programming.project1;
/**
*
* @author lancesimerly
*/
import java.util.ArrayList;
public class Transaction {
private final ArrayList<LineItem> lineItems;
private int customerID;
private String customerName;
public Transaction(int customerID, String customerName){
this.customerID = customerID;
this.customerName = customerName;
this.lineItems = new ArrayList<LineItem>();
}
public int getCustomerID(){
return customerID;
}
public void setCustomerID(int customerID){
this.customerID = customerID;
}
public String getCustomerName(){
return customerName;
}
public void setCustomerName(String customerName){
this.customerName = customerName;
}
public void addLineItem(String itemName, int quantity, double price){
lineItems.add(new LineItem(itemName, quantity, price));
}
public void updateItem(String itemName, int quantity, double price){
for(int i = 0; i < lineItems.size(); i++)
if(lineItems.contains(itemName))
lineItems.get(i).setQuantity(quantity);
}
public double getTotalPrice(){
double totalPrice = 0;
for(int i = 1; i < lineItems.size(); i++)
totalPrice += lineItems.get(i).getPrice();
return totalPrice;
}
public String getLineItem(String itemName){
String str = "";
for (int i = 0; i < lineItems.size(); i++){
str = lineItems.get(i).toString();
}
return str;
}
public String toString(){
String str;
str = ("Customer ID : " + this.customerID + " Customer Name : "
+ this.customerName + " " + lineItems.get(0).toString() + " " +
lineItems.get(1).toString() + " " + lineItems.get(2).toString() +
" " + "Transaction Total " + this.getTotalPrice());
return str;
}
}
Transaction
- lineItems : ArrayList<LineItem> - customerID : int - customerName : String
+ Transaction( int, String ) + addLineItem( String, int, double ) + updateItem( String, int, double ) + getTotalPrice( ) : double + getLineItem( String ) : String + toString( ) : String
Here is what I have so far:
/*
* 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.
*/
package programming.project1;
/**
*
* @author lancesimerly
*/
public class LineItem {
private String itemName;
private int quantity;
private double price;
public LineItem(String name, int number, double cost){
itemName = name;
quantity = number;
price = cost;
}
public String getItemName(){
return itemName;
}
public int getQuantity(){
return quantity;
}
public double getPrice(){
return price;
}
public double getTotalPrice(){
double totalPrice;
totalPrice = quantity * price;
return totalPrice;
}
public void setQuantity(int x){
quantity = x;
}
public void setPrice(double y){
price = y;
}
public String toString(){
String str;
str = (itemName + " qty " + quantity + " @ $" + price + " "
+ "$" + getTotalPrice());
return str;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
* 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.
*/
package programming.project1;
/**
*
* @author lancesimerly
*/
import java.util.ArrayList;
public class Transaction {
private final ArrayList<LineItem> lineItems;
private int customerID;
private String customerName;
public Transaction(int customerID, String customerName){
this.customerID = customerID;
this.customerName = customerName;
this.lineItems = new ArrayList<LineItem>();
}
public int getCustomerID(){
return customerID;
}
public void setCustomerID(int customerID){
this.customerID = customerID;
}
public String getCustomerName(){
return customerName;
}
public void setCustomerName(String customerName){
this.customerName = customerName;
}
public void addLineItem(String itemName, int quantity, double price){
lineItems.add(new LineItem(itemName, quantity, price));
}
public void updateItem(String itemName, int quantity, double price){
for(int i = 0; i < lineItems.size(); i++)
if(lineItems.contains(itemName))
lineItems.get(i).setQuantity(quantity);
}
public double getTotalPrice(){
double totalPrice = 0;
for(int i = 1; i < lineItems.size(); i++)
totalPrice += lineItems.get(i).getPrice();
return totalPrice;
}
public String getLineItem(String itemName){
String str = "";
for (int i = 0; i < lineItems.size(); i++){
str = lineItems.get(i).toString();
}
return str;
}
public String toString(){
String str;
str = ("Customer ID : " + this.customerID + " Customer Name : "
+ this.customerName + " " + lineItems.get(0).toString() + " " +
lineItems.get(1).toString() + " " + lineItems.get(2).toString() +
" " + "Transaction Total " + this.getTotalPrice());
return str;
}
}
Explanation / Answer
Hi, Highlighted areas are the code changes.
TransactionTest.java
public class TransactionTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Transaction tx = new Transaction(1111, "Suresh");
tx.addLineItem("Soap", 5, 22.50);
tx.addLineItem("Pen", 4, 12.50);
tx.addLineItem("Pencil", 4, 10.50);
System.out.println(tx.toString());
tx.updateItem("Soap", 6, 22.50);
System.out.println("--------After Update Item---------");
System.out.println(tx.toString());
}
}
Transaction.java
import java.util.ArrayList;
public class Transaction {
private final ArrayList<LineItem> lineItems;
private int customerID;
private String customerName;
public Transaction(int customerID, String customerName){
this.customerID = customerID;
this.customerName = customerName;
this.lineItems = new ArrayList<LineItem>();
}
public int getCustomerID(){
return customerID;
}
public void setCustomerID(int customerID){
this.customerID = customerID;
}
public String getCustomerName(){
return customerName;
}
public void setCustomerName(String customerName){
this.customerName = customerName;
}
public void addLineItem(String itemName, int quantity, double price){
lineItems.add(new LineItem(itemName, quantity, price));
}
public void updateItem(String itemName, int quantity, double price){
for(int i = 0; i < lineItems.size(); i++)
if(lineItems.get(i).getItemName().equals(itemName))
lineItems.get(i).setQuantity(quantity);
}
public double getTotalPrice(){
double totalPrice = 0;
for(int i = 0; i < lineItems.size(); i++)
totalPrice += lineItems.get(i).getTotalPrice();
return totalPrice;
}
public String getLineItem(String itemName){
String str = "";
for (int i = 0; i < lineItems.size(); i++){
str = lineItems.get(i).toString();
}
return str;
}
public String toString(){
String str;
str = ("Customer ID : " + this.customerID + " Customer Name : "
+ this.customerName + " " + lineItems.get(0).toString() + " " +
lineItems.get(1).toString() + " " + lineItems.get(2).toString() +
" " + "Transaction Total " + this.getTotalPrice());
return str;
}
}
LineItem.java
public class LineItem {
private String itemName;
private int quantity;
private double price;
public LineItem(String name, int number, double cost){
itemName = name;
quantity = number;
price = cost;
}
public String getItemName(){
return itemName;
}
public int getQuantity(){
return quantity;
}
public double getPrice(){
return price;
}
public double getTotalPrice(){
double totalPrice;
totalPrice = quantity * price;
return totalPrice;
}
public void setQuantity(int x){
quantity = x;
}
public void setPrice(double y){
price = y;
}
public String toString(){
String str;
str = (itemName + " qty " + quantity + " @ $" + price + " "
+ "$" + getTotalPrice());
return str;
}
}
Output:
Customer ID : 1111
Customer Name : Suresh
Soap qty 5 @ $22.5 $112.5
Pen qty 4 @ $12.5 $50.0
Pencil qty 4 @ $10.5 $42.0
Transaction Total 204.5
--------After Update Item---------
Customer ID : 1111
Customer Name : Suresh
Soap qty 6 @ $22.5 $135.0
Pen qty 4 @ $12.5 $50.0
Pencil qty 4 @ $10.5 $42.0
Transaction Total 227.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.