Exemplo n.º 1
0
 /**
  * String representation of the OrderItemList object.
  *
  * @return It returns all the available OrderItem data in a String format. It uses the OrderItem
  *     object's toString() method.
  */
 @Override
 public String toString() {
   StringBuilder orderItemListBuilder = new StringBuilder();
   java.util.List<OrderItem> orderItemList = getOrderItem();
   for (OrderItem orderItem : orderItemList) {
     orderItemListBuilder.append("OrderItem: ");
     orderItemListBuilder.append(orderItem.toString());
     orderItemListBuilder.append("\n");
   }
   return orderItemListBuilder.toString();
 }
  /**
   * Test driver for class <code>OrderItem</code>.
   *
   * @param args not used.
   */
  public static void main(String[] args) {

    stdOut.println("");
    stdOut.println("Testing class OrderItem...");

    String code = "C001";
    String description = "Colombia, Whole, 1 lb";
    double price = 17.99;
    int quantity = 5;
    int newQuantity = 10;
    Product product = new Product(code, description, price);

    // Test accessors
    OrderItem item = new OrderItem(product, quantity);

    assertTrue("1: testing method getProduct", product.equals(item.getProduct()));
    assertTrue("2: testing method getQuantity", quantity == item.getQuantity());

    // Test mutator
    item = new OrderItem(product, quantity);
    item.setQuantity(newQuantity);

    assertTrue("3: testing method setQuantity", newQuantity == item.getQuantity());

    // Test method valueOf
    item = new OrderItem(product, quantity);

    double value = price * (double) quantity;

    assertTrue("4: testing method valueOf", value == item.getValue());

    // Test method toString
    item = new OrderItem(product, quantity);

    String result = quantity + " " + code + " " + price;

    assertTrue("5: testing method toString", result.equals(item.toString()));

    stdOut.println("done");
  }