Question

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 Transaction class.

2. Design and build a LineItem 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 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. Using Unified Modeling Language (UML), the class diagram looks like this:

a. 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.
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.

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 "\t".

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

3. 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.

4. 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.

Specific Requirements for the Transaction Class
1. The Transaction 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.
2. There should be a method to allow the addition of a line item to the transcript. The three parameters for the addLineItem method will be (1) the item name, (2) the quantity, and (3) the single item price.
3. There should be a method to allow the updating of a line item already in the transaction. 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 transaction unchanged.

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

5. 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 LineItem 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.

6. The final method needed is a toString method. It should return the transaction 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

Transaction Total $9.96

Notice that a newline character "\n" can be inserted into the middle of a string.

Ex.

int age = 30;

String temp = "John Doe \n is " + age + "\n" + " years old";

The output would be:

John Doe

is 30

years old

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


Here is a UML diagram for the Transaction 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.

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

Code

LineItem.java

public class LineItem {
  
private int quantity;
private double price;
private String itemName;

  

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

  
public String getName()
{
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 double getTotalPrice()
{
return quantity * price;
}


@Override
public String toString(){
return(this.itemName + "\t" + "qty " + this.quantity + "\t" + "@" + this.price + "\t$" +this.price*this.quantity);
}
  
}

Transaction.java class

import java.util.ArrayList;


public class Transaction {
public final ArrayList<LineItem> lineItems;
private int customerID;
private String customerName;
private String LineItem;
private int i;

public Transaction (int customerID, String customerName){
this.customerID = customerID;
this.customerName = customerName;
this.lineItems = new ArrayList<>();
}
  


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));
}
  

private int SearchItem(String itemName)
{
int i=0;
for(LineItem item :lineItems)
{
if(item.getName().equals(itemName))
return i;
i++;
}
return -1;
}
public void updateItem(String itemName, int quantity, double price)
{
int index=SearchItem(itemName);
if(index>=0)
{
lineItems.get(index).setPrice(price);
lineItems.get(index).setQuantity(quantity);
}
}
  
public double getTotalPrice(){
double totalPrice = 0;

for (int i =0;i<lineItems.size(); i++){
LineItem item = lineItems.get(i);
totalPrice = totalPrice + item.getTotalPrice();
}
return totalPrice;
  
}
public String getLineItem(String itemName)
{
int index=SearchItem(itemName);
if(index>=0)
return lineItems.get(index).toString();
return null;
}
@Override

public String toString()
{
String str="Customer ID:" + this.customerID + "\n" + "Customer Name" + this.customerName+"\n\n";
  
for(LineItem item :lineItems)
{
str+=item.toString()+"\n";
}
str+="\nTransaction Total $"+getTotalPrice();
  
return str;
}

}

TransactionTest.java with main function


public class TransactionTest
{
public static void main(String[] args)
{
Transaction t1=new Transaction(12345,"John Doe");
t1.addLineItem("Colgate Toothpaste", 2,2.99);
t1.addLineItem("Bounty Paper Towels", 1, 1.49);
t1.addLineItem("Kleenex Tissue Paper", 1, 2.49);
  
System.out.println(t1);
  
t1.updateItem("Bounty Paper Towels", 5, 2);
  
System.out.println("\n\nAfter updating the Bounty Paper Towels with quntity 5 and price 2 Transaction line is:\n");
  
System.out.println(t1);
}
}
output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Java 1. Develop a program to emulate a purchase transaction at a retail store. This program...
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
  • Develop a program to emulate a purchase transaction at a retail storeThis program will have two...

    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...

  • Write the following program in Java using Eclipse. A cash register is used in retail stores...

    Write the following program in Java using Eclipse. A cash register is used in retail stores to help clerks enter a number of items and calculate their subtotal and total. It usually prints a receipt with all the items in some format. Design a Receipt class and Item class. In general, one receipt can contain multiple items. Here is what you should have in the Receipt class: numberOfItems: this variable will keep track of the number of items added to...

  • Here is a sample run of the tester program: Make sure your program follows the Java...

    Here is a sample run of the tester program: Make sure your program follows the Java Coding Guidelines. Consider the following class: /** * A store superclass with a Name and Sales Tax Rate */ public class Store {      public final double SALES_TAX_RATE = 0.06;      private String name;           /**      * Constructor to create a new store      * @param newName      */      public Store(String newName)      {           name = newName;      }     ...

  • Java Question 3 Implement a program to store the applicant's information for a visa office. You...

    Java Question 3 Implement a program to store the applicant's information for a visa office. You need to implement the following: A super class called Applicant, which has the following instance variables: A variable to store first name of type String. A variable to store last name of type String. A variable to store date of birth, should be implemented as another class to store day, month and year. A variable to store number of years working of type integer...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super...

    Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super class. The class includes four private String instance variables: first name, last name, social security number, and state. Write a constructor and get methods for each instance variable. Also, add a toString method to print the details of the person. After that create a class Teacher which extends the class, Person. Add a private instance variable to store number of courses. Write a constructor...

  • Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and...

    Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and Bird 2.      A Bird class that is a descendant of Animal 3.      A Dog class that is a descendant of Animal 4.      A “driver” class named driver that instantiates an Animal, a Dog, and a Bird object. The Animal class has: ·        one instance variable, a private String variable named   name ·        a single constructor that takes one argument, a String, used to set...

  • C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (Strin...

    C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (String) edition ( int) published year (int) Constructor that takes all of the above variables as input parameters. set/get methods ToString method// that return sting representation of Book object. 2-Create the sub class New_Book that is derived from the base class Book and has the following instance variables, constructor, and methods: title...

  • This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).

    Ch 7 Program: Online shopping cart (continued) (Java)This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).(1) Extend the ItemToPurchase class per the following specifications:Private fieldsstring itemDescription - Initialized in default constructor to "none"Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt)Public member methodssetDescription() mutator & getDescription() accessor (2 pts)printItemCost() - Outputs the item name followed by the quantity, price, and subtotalprintItemDescription() - Outputs the...

  • JAVA :The following are descriptions of classes that you will create. Think of these as service...

    JAVA :The following are descriptions of classes that you will create. Think of these as service providers. They provide a service to who ever want to use them. You will also write a TestClass with a main() method that fully exercises the classes that you create. To exercise a class, make some instances of the class, and call the methods of the class using these objects. Paste in the output from the test program that demonstrates your classes’ functionality. Testing...

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