Week 6 Java Project Transaction.java

 /*



/**
 *
 * @author AMARE
 */
import java.util.ArrayList;

public class Transaction   
 {
    
/**
 * Holds line items
 */

    private ArrayList<LineItem> lineItems;

/**
 * Holds customer ID
 */
    private int customerID;

/**
 * Holds customer name
 */
    private String customerName;

/**
 * The parameterized constructor accepts arguments
 * @param ID
 * @param name 
 */
    public Transaction(int ID, String name) 
    {
        this.customerID = ID;
        this.customerName = name;
        this.lineItems = new ArrayList<LineItem>();
    }

//set addLineItem method
    public void addLineItem(String name, int quantity, double price) 
    {
        lineItems.add(new LineItem(name, quantity, price));
    }

/**
 * The parameterized constructor accepts arguments
 * @param name
 * @param quantity
 * @param price 
 */
    public void updateItem(String name, int quantity, double price) 
    {

        if (name != null) 
        {
            for (int i = 0; i < lineItems.size(); i++) 
            {
                LineItem temp = lineItems.get(i);
                if (temp.getName().equals(name))
                 {
                    lineItems.add(i, new LineItem(name, quantity, price));

                }
            }
        }

    }
/**
 * getTotalPrice method
 * @return 
 */
    public double getTotalPrice()
     {
        double totalPrice = 0;
        for (LineItem item : lineItems) 
        {
            totalPrice += item.getTotalPrice();
        }
        return totalPrice;
    }
/**
 * getLineItem method
 * @param name
 * @return 
 */
    public String getLineItem(String name) 
    {
        if (name != null) 
        {
            for (int i = 0; i < lineItems.size(); i++) 
            {
                LineItem temp = lineItems.get(i);
                if (temp.getName().equals(name)) 
                {
                    return temp.toString();
                }
            }
        }

        return name + " not found";

    }
/**
 * toString method
 * @return 
 */
    public String toString() 
    {
        String str = "Customer ID : " + this.customerID + "\n"
                + "Customer Name : " + this.customerName + "\n";
        for (LineItem item : lineItems)
         {
            str += item.toString() + "\n";
        }
        return str;
    }
}

Comments

Popular posts from this blog

Case Study 2: Wireless and Mobile Technologies

Assignment 3: Apple versus Samsung

Week 8 Java Project [Ticket.java]