Develop a program to emulate a purchase transaction at a retail store.This progr
ID: 3860018 • Letter: D
Question
Develop a program to emulate a purchase transaction at a retail store.This program will have two classes,a Lineltem class and a Transaction class. The Lineltem class will represent an individual line item of merchandise that a customer is purchasing. The Transaction class will combine several Lineltem objects and calculate an overall total price for the line item within the transaction. There will also be two test classes, one for the Lineltem class and one for the Transaction class. Design and build a Lineltem 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 Lineltem 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: Lineltem - itemName String - quantity : int - price: double + Lineltem( String, int, double) + getName) String + getQuantity(): int +getPrice( ): double +getTotalPrice(): double + setQuantity( int) + setPrice( double) + toString) StringExplanation / Answer
/*
* 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 chegg.july;
import java.util.ArrayList;
/**
*
* @author Sam
*/
class LineItem {
private String itemName;
private int quantity;
private double price;
public LineItem(String itemName, int quantity, double price) {
this.itemName = itemName;
this.quantity = quantity;
this.price = price;
}
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 String getItemName() {
return itemName;
}
public double getTotalPrice() {
return quantity * price;
}
@Override
public String toString() {
return getItemName() + " qty " + getQuantity() + " @ " + getPrice() + " " + getTotalPrice() ;
}
}
public class Transaction {
private ArrayList<LineItem> lineItems;
private int customerID;
private String customerName;
public Transaction(int customerID, String customerName) {
this.customerID = customerID;
this.customerName = customerName;
lineItems = new ArrayList<>();
}
public void addItem(String itemName, int quantity, double price) {
lineItems.add(new LineItem(itemName, quantity, price));
}
public void updateItem(String itemName, int quantity, double price) {
int i;
for (i = 0; i < lineItems.size(); i++)
if (lineItems.get(i).getItemName().equals(itemName))
break;
if (i == lineItems.size()) {
System.out.println("Item: " + itemName + " not found");
return;
}
lineItems.get(i).setPrice(price);
lineItems.get(i).setQuantity(quantity);
}
public double getTotalPrice() {
int i;
double totalPrice = 0;
for (i = 0; i < lineItems.size(); i++)
totalPrice += lineItems.get(i).getTotalPrice();
return totalPrice;
}
public String getLineItem(String itemName, int quantity, double price) {
int i;
for (i = 0; i < lineItems.size(); i++)
if (lineItems.get(i).getItemName().equals(itemName))
break;
if (i == lineItems.size()) {
return itemName + " not found";
}
return lineItems.get(i).toString();
}
@Override
public String toString() {
String result = "";
result = "Customer ID: " + customerID + " "
+ "Customer Name: " + customerName + " "
+ " "
+ " ";
String names = "";
String quanrity = "";
String totalPrices = "";
double total = 0;
for (LineItem l : lineItems) {
names += l.getItemName() + " ";
quanrity += "qty " + l.getQuantity() + "@" + l.getPrice() + " ";
totalPrices += l.getTotalPrice()+ " ";
total += l.getTotalPrice();
}
result += names + quanrity + " $" + totalPrices + " $" + total;
return result;
}
}
Ihis should work. Let me know..!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.