Question

Develop a program to emulate a purchase transaction at a retail storeThis program will have two classes, a Lineltem class and a Transaction class. The Lineltem class will represent an individua 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 get Quantity(): int getPrice( double getTotal Price(): double setQuantity (int) setPrice( double) toString(): String

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Output

R Markers Properties Servers膼Data Source Explorer la Snippets 旦Console X Search <terminated> TransctionDemo [Java Application

Program LineItem.java

====================================================

public class LineItem {

   // Declare all required variables
   private String itemName;
   private int quantity;
   private double price;

   // Parameterized constructor
   public LineItem(String itemName, int quantity, double price) {

       this.itemName = itemName;
       this.quantity = quantity;
       this.price = price;
   }

   // Getter method for ItemName
   public String getItemName() {

       return itemName;
   }

   // Getter method for quantity
   public int getQuantity() {

       return quantity;
   }

   // Getter method for price
   public double getPrice() {

       return price;
   }

   // Getter method Total Price
   public double getTotalPrice() {

       double totalPrice;
       totalPrice = quantity * price;
       return totalPrice;
   }

   // Setter method to set quantity
   public void setQuantity(int x) {

       quantity = x;
   }

   // Setter method to set price
   public void setPrice(double y) {

       price = y;
   }

   // toString() to get line item description
   public String toString() {

       String lineItemDescription;
       lineItemDescription = (itemName + "\t\t\tqty " + quantity + " @ $" + price + "\t\t" + "$" + getTotalPrice());
       return lineItemDescription;
   }
}

Program Transaction.java

====================================================

import java.text.DecimalFormat;
import java.util.ArrayList;

public class Transaction {

   // Declare all required variables
   private ArrayList<LineItem> lineItems;
   private int customerID;
   private String customerName;
   DecimalFormat decimalFormat = new DecimalFormat("#.##");

   // Parameterized constructor
   public Transaction(int customerID, String customerName) {

       this.customerID = customerID;
       this.customerName = customerName;

       // Create ArrayList object and assign to lineItems
       lineItems = new ArrayList<LineItem>();
   }

   // Method to add lineItem
   public void addLineItem(String itemName, int quantity, double price) {

       // Create line item object by passing itemName, quantity, price
       LineItem lineItem = new LineItem(itemName, quantity, price);

       // Add lineItem to lineItems
       lineItems.add(lineItem);
   }

   // Method to update line item
   public void updateItem(String itemName, int quantity, double price) {

       boolean isFound = false;
       LineItem lineItem = null;

       // Find the line item by itemName
       for (int i = 0; i < lineItems.size(); i++) {

           lineItem = lineItems.get(i);
           if (lineItem.getItemName().equalsIgnoreCase(itemName)) {
               isFound = true;
               break;
           }
       }

       // Check if line item found or not, if found update quantity
       if (isFound) {

           lineItem.setQuantity(quantity);
       } else {
           System.out.println("Line item not found with given item name!");
       }
   }

   // Method to get Total price
   public double getTotalPrice() {

       double totalPriceOfLineItems = 0;

       // Iterate over all line items
       for (int i = 0; i < lineItems.size(); i++) {

           totalPriceOfLineItems = totalPriceOfLineItems + lineItems.get(i).getTotalPrice();
       }

       return totalPriceOfLineItems;
   }

   // Get Line item by item name
   public String getLineItem(String itemName) {

       boolean isFound = false;
       LineItem lineItem = null;

       // Find the line item by itemName
       for (int i = 0; i < lineItems.size(); i++) {

           lineItem = lineItems.get(i);
           if (lineItem.getItemName().equalsIgnoreCase(itemName)) {
               isFound = true;
               break;
           }
       }

       // Check if line item found or not
       if (isFound) {

           return lineItem.toString();
       } else {
           return "Line item not found with given item name!";
       }
   }

   // toString() method to return transaction description
   public String toString() {

       String transctionDescription;
      
       //Get line items description
       String linItemsDescription = "";
       for(int i=0;i<lineItems.size();i++) {
           linItemsDescription = linItemsDescription + lineItems.get(i).toString() + "\n";
       }
      

       transctionDescription = "Customer ID : " + customerID + "\nCustomer Name : " + customerName + "\n"
               + linItemsDescription + "Transaction Total : \t\t\t$" + decimalFormat.format(getTotalPrice());
      
       return transctionDescription;
   }

}

Program TransctionDemo.java

====================================================

public class TransctionDemo {

   public static void main(String[] args) {
      
       //Create Transaction object

       Transaction transaction = new Transaction(123456, "John Doe");
      
       //Add line items
       transaction.addLineItem("Colgate Toothpaste", 2, 2.99);
       transaction.addLineItem("Bounty Paper Towels", 1, 1.49);
       transaction.addLineItem("Kleenex Tissue ", 1, 2.49);
      
       //Print transaction description using toString() method
       System.out.println(transaction.toString());
      
       //Update line item
       transaction.updateItem("Colgate Toothpaste", 3, 22.50);
      
       System.out.println("\n-------- Updated Transction details ---------");
       System.out.println(transaction.toString());

   }

}

Add a comment
Know the answer?
Add Answer to:
Develop a program to emulate a purchase transaction at a retail storeThis program will have two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java 1. Develop a program to emulate a purchase transaction at a retail store. This program...

    Java 1. Develop a program to emulate a purchase transaction at a retail store. 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...

  • 4.18 Ch 7 Program: Online shopping cart (continued) (C++) This program extends the earlier "Online shopping...

    4.18 Ch 7 Program: Online shopping cart (continued) (C++) This program extends the earlier "Online shopping cart" program. (solution from previous lab assignment is provided in Canvas). (1) Extend the ItemToPurchase class per the following specifications: Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt) Public member functions SetDescription() mutator & GetDescription() accessor (2 pts) PrintItemCost() - Outputs the item name followed by the quantity, price, and subtotal PrintItemDescription() -...

  • Warm up: Online shopping cart (Part 1)

    8.6 LAB*: Warm up: Online shopping cart (Part 1)(1) Create two files to submit:ItemToPurchase.java - Class definitionShoppingCartPrinter.java - Contains main() methodBuild the ItemToPurchase class with the following specifications:Private fieldsString itemName - Initialized in default constructor to "none"int itemPrice - Initialized in default constructor to 0int itemQuantity - Initialized in default constructor to 0Default constructorPublic member methods (mutators & accessors)setName() & getName() (2 pts)setPrice() & getPrice() (2 pts)setQuantity() & getQuantity() (2 pts)(2) In main(), prompt the user for two items and create...

  • in java PART ONE ======================================= Below is the "RetailItem" class definition that we used in Chapter 6. Write a "CashRegister" class that leverages this "Ret...

    in java PART ONE ======================================= Below is the "RetailItem" class definition that we used in Chapter 6. Write a "CashRegister" class that leverages this "RetailItem" class which simulates the sale of a retail item. It should have a constructor that accepts a "RetailItem" object as an argument. The constructor should also accept an integer that represents the quantity of items being purchased. Your class should have these methods: getSubtotal: returns the subtotal of the sale, which is quantity times price....

  • JAVA Use the class, Invoice, provided to create an array of Invoice objects. Class Invoice includes...

    JAVA Use the class, Invoice, provided to create an array of Invoice objects. Class Invoice includes four instance variables; partNumber (type String), partDescription (type String), quantity of the item being purchased (type int0, and pricePerItem (type double). Perform the following queries on the array of Invoice objects and display the results: Use streams to sort the Invoice objects by partDescription, then display the results. Use streams to sort the Invoice objects by pricePerItem, then display the results. Use streams to...

  • I need help with my homework please. I should write this program in C++ language and...

    I need help with my homework please. I should write this program in C++ language and use Inventory.h file (header file), Inventory.cpp file (implementation file), and main.cpp file (main program). This is the program: Demonstrate the class in a driver program. Input validation, don’t accept negative values for item number, quantity, or cost. 6. Inventory Class Design an Inventory class that can hold information and calculate data for items ma retail store's inventory. The class should have the following private...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • Need three seperate files. ShoppingCartManager.java ShoppingCart.java ItemsToPurchase.java These are from part 1 what do you mean...

    Need three seperate files. ShoppingCartManager.java ShoppingCart.java ItemsToPurchase.java These are from part 1 what do you mean by deep study 7.25 LAB*: Program: Online shopping cart (Part 2) Note: Creating multiple Scanner objects for the same input stream yields unexpected behavior. Thus, good practice is to use a single Scanner object for reading input from System.in. That Scanner object can be passed as an argument to any methods that read input This program extends the earlier Online shopping cart program (Consider...

  • Update your Assignment 4 as follows (Assignment 4 codes will be on the bottom) Create a...

    Update your Assignment 4 as follows (Assignment 4 codes will be on the bottom) Create a new method called displayAll, that takes an ArrayList (of your base class) as a parameter, and doesn't return anything [25 pts] The displayAll method will loop through the ArrayList, and call the display (or toString) method on each object   [25 pts] In the main method, create an ArrayList containing objects of both your base class and subclass. (You should have at least 4 objects)  [25...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT