Home > Articles > Programming > Java

  • Print
  • + Share This
Like this article? We recommend

Source Code

Sale Class

/**
 * One user transaction consisting of purchases of potentially many kinds of product
 * @stereotype moment-interval
 */
public class Sale {
  /**
   * calculates the total of the sale from the lineItem subtotals
   * @return total of the sale
   */
  public double calcPayments() {
    paymentsTotal = 0.0;
    Iterator i = payments.iterator();
    while (i.hasNext()) paymentsTotal += ((Payment)i.next()).getAmount();
    return total;
  }

  /**
   * calculates the total of the sale from the lineItem subtotals
   * @return total of the sale
   */
  public double calcTotal() {
    total = 0.0;
    Iterator i = lineItems.iterator();
    while (i.hasNext()) total += ((LineItem)i.next()).calcTotal();
    return total;
  }

  /**
   * retrieves the cached total of the sale
   * @return total of sale, 0.0 if calcTotal() has not yet been called
   */
  public double getTotal() {
    return total;
  }

  /**
   * sets the status of the sale to compltete if payments equal price
   * @exception Exce[tion thrown if payments do not equal price
   */
  public void complete() throws Exception {
    if (calcTotal() != calcPayments()) {
      throw new Exception("Payments do not equal total price");
    }
    status = "Complete";
  }

  /**
   * adds a line item for a quantity of a type of item
   * @param product the type of item being sold
   * @param qty the quantity of the item being sold
   */
  public void addLineItem(Product product, int qty) throws Exception {
    if (status.equals("Incomplete")) {
      lineItems.add(new LineItem(product, qty));
    }
    else {
      throw new Exception("Cannot add items to a completed sale");
    }
  }

  /** status of sale */
  private String status = "Incomplete";

  /**
   * the cost of this quantity of the item the Product is responsible for
   * handling quantity discounting or time based special deals, etc
   */
  private double total;

  /** the total of all the payments made for this sale */
  private double paymentsTotal;

  /**
   * @link aggregation
   * @associates <{LineItem}>
   * @supplierCardinality 1..*
   * @clientCardinality 1
   */
  private Vector lineItems = new Vector();

  /**
   * payments of various kinds
   * @associates <{Payment}>
   * @supplierCardinality 0..*
   * @clientCardinality 1
   */
  private Vector payments = new Vector();
}

SaleItem Class

/**
* one line in a sale of potentially many kinds of product
* @stereotype mi-detail
*/
public class LineItem {
  /**
   * mandatory value constructor requires values for all attributes needed to
   * put the object in a valid state
   * @param product the kind of item being purchased
   * @param qty the quanity of the item being purchased
   */
  public LineItem(Product product, int qty) {
    this.product = product;
    this.quantity = qty;
  }

  /**
   * calculates the cost of this amount of this kind of item
   * @return the cost of this amount of the item
   */
  public double calcTotal() {
    total = product.calcTotal(this);
    return total;
  }

  /**
   * accessor method for cached cost of this lineItem
   * @return price for this quantity of the item at the time of the sale
   */
  public double getTotal() {
    return total;
  }

  /**
   * accessor method for quantity
   * @return the quanity of the item being purchased
   */
  public int getQuantity() {
    return quantity;
  }

  /**
   * accessor method for quantity
   * @param quantity the quanity of the item being purchased
   */
  public void setQuantity(int quantity) {
    this.quantity = quantity;
  }

  /**
   * the kind of ite being purchased
   * @supplierCardinality 1
   * @clientCardinality 0..*
   */
  private Product product;

  /** cached value of this line item */
  private double total;

  /** number of units being purchased */
  private int quantity;
}

Product Class

/**
 * Instances represent entries in the inventory catalog
 * @stereotype description
 */
public class Product {
  /**
   * mandatory values constructor requires values for all attributes needed to
   * leave the object in a valid state
   * @param code the UPC
   * @param name short human friendly name for the product
   * @param amount the price of one unit of the product
   */
  public Product(String code, String name, double amount) {
    this.code = code;
    this.name = name;
    this.amount = amount;
  }

  /**
   * calculates the current cost of a quantity of the product
   * @return cost of the line item supplied
   */
  public double calcTotal(LineItem li) {
    return amount * li.getQuantity();
  }

  /** UPC */
  private String code;

  /** Human friendly product name */
  private String name;

  /** current price of a unit of the product */
  private double amount;
} 

Payment Class

/**
 * represents a payment of some kind
 * @stereotype moment-interval 
 */
abstract public class Payment {

  /**
   * mandatory value constructor requires values for all attributes needed to
   * put the new object into a valid state
   *
   * @param amount the amount of this payment
   */
  public Payment(double amount) {
    this.amount = amount;
  }

  /**
   * accessor for amount property
   * @return the amount property
   */
  public double getAmount() {
    return amount;
  }

  /**
   * accessor for the amount property
   * @param amount the value of the property
   */
  public void setAmount(double amount) {
    this.amount = amount;
  }

  /** amount of the payment */
  private double amount;
}

CashPayment Class

/**
 * a subclass that extends the Payment class to represent cash payments
 * @stereotype moment-interval
 */
public class CashPayment extends Payment {
  /**
   * mandatory value constructor requires values for all attributes needed to
   * put the new object into a valid state
   *
   * @param amount the amount tendered for this payment
   */
  public CashPayment(double amount) {
    super(amount);
  }

	/**
   * amount tendered is what was passed into the constructor
   * @return amount tendered
   */
  public double getAmountTendered() {
    return super.getAmount();
  }

  /**
   * calculate the change to be given for a required amount
   * @return value of change
   */
  public double calcChange( double requiredAmount ) {
    change = super.getAmount() - requiredAmount;
    return change;
  }

  /**
   * override superclass to calc actual amount based on change given
   * @return amountTendered - change given
   */
	public double getAmount() {
    return super.getAmount() - change;
  }

	/** amopunt of change given */
  private double change = 0.0;

}

CreditCardPayment Class

/**
 * a subclass that extends the Payment class to represent credit card payments
 * @stereotype moment-interval
 */
public class CreditCardPayment extends Payment {
  /**
mandatory value constructor requires values for all attributes 
        needed to put the new object into a valid state
   * @param amount the amount of this payment
   * @param cardNumber the number of the credit card
   */
  public CreditCardPayment(double amount, String cardNumber) {
    super(amount);
    this.cardNumber = cardNumber;
  }

  /**
   * accessor for cardNumber property
   * @return value of property
   */
  public String getCardNumber() {
    return cardNumber;
  }

  /**
   * accessor for cardNumber property
   * @param cardNumber value of property
   */
  public void setCardNumber(String cardNumber) {
    this.cardNumber = cardNumber;
  }

  /**
   * authorize the payment using an external system somewhere
   * @return true if payment valid
   */
  public boolean authorize() {
    return false; //not yet implemented
  }

  /** credit card number property */
  private String cardNumber;
}
  • + Share This
  • 🔖 Save To Your Account