Exemplo n.º 1
0
  /**
   * Create a new Customer instance.
   *
   * @return A Customer instance
   */
  private static Customer createCustomer(PropertyChangeListener listener) {
    Customer customer = GeneralFactory.createCustomer();
    customer.addPropertyChangeListener(listener);

    // define the customer
    customer.setId("1");
    customer.setName("Matt Gilman");
    customer.setStreet("123 Street Rd");
    customer.setCity("City");
    customer.setZip("12345");
    customer.setPhone("123-4567");
    customer.setPassword("1234");
    return customer;
  }
Exemplo n.º 2
0
  /**
   * Main entry point of homework 1.
   *
   * @param args Command line ares - ignored
   */
  public static void main(String[] args) {
    // create the catalog
    Catalog catalog = GeneralFactory.createCatalog();
    catalog.addPropertyChangeListener(GeneralFactory.createPropertyListener("Catalog"));

    // seed the catalog and populate the inventory
    seedCatalog(catalog, GeneralFactory.createPropertyListener("Product"));

    // create the subjects...
    ProductHolder cart = GeneralFactory.createProductHolder();
    cart.setCatalog(catalog);

    ProductHolder inventory = GeneralFactory.createProductHolder();
    inventory.setCatalog(catalog);

    ProductHolder boughtItems = GeneralFactory.createProductHolder();
    boughtItems.setCatalog(catalog);

    // register property listener
    cart.addPropertyChangeListener(GeneralFactory.createPropertyListener("Cart"));
    inventory.addPropertyChangeListener(GeneralFactory.createPropertyListener("Inventory"));
    boughtItems.addPropertyChangeListener(GeneralFactory.createPropertyListener("BoughtItems"));

    // create the customer
    Customer customer = createCustomer(GeneralFactory.createPropertyListener("Customer"));
    customer.setCart(cart);
    customer.setBoughtItems(boughtItems);

    // populate the inventory
    populateInventory(inventory);

    // add the catalog to the product holders
    cart.setCatalog(catalog);
    inventory.setCatalog(catalog);
    boughtItems.setCatalog(catalog);

    // create the CommandListener's
    CommandListener cartUpdater = CommandListenerFactory.createCartUpdater(cart);
    CommandListener customerUpdater = CommandListenerFactory.createCustomerUpdater(customer);
    CommandListener customerCartUpdater =
        CommandListenerFactory.createCustomerCartUpdater(cartUpdater, customerUpdater);
    CommandListener inventoryUpdater = CommandListenerFactory.createInventoryUpdater(inventory);

    // create the Client and add each listener
    Client client = GeneralFactory.createClient();
    client.addCommandListener(customerCartUpdater);
    client.addCommandListener(inventoryUpdater);
    client.addCommandListener(GeneralFactory.createLogHandler(catalog));

    // verify empty state
    System.out.println("Cart is empty: " + cart.isEmpty());
    System.out.println("Bought items is empty: " + boughtItems.isEmpty());

    // ensure inventory quantity is correct
    System.out.println(
        String.format(
            INVENTORY_EXPECTED_MSG,
            catalog.getProduct(TEST_PRODUCT_ID),
            2,
            inventory.getQuantity(TEST_PRODUCT_ID)));

    // add some products to the cart
    client.addToCart("1", 2);
    client.addToCart("9", 1);
    client.addToCart(TEST_PRODUCT_ID, 2);

    // verify empty state
    System.out.println("Cart is empty: " + cart.isEmpty());
    System.out.println("Bought items is empty: " + boughtItems.isEmpty());

    // ensure the cart quantity is correct
    System.out.println(
        String.format(
            CART_EXPECTED_MSG,
            catalog.getProduct(TEST_PRODUCT_ID),
            2,
            cart.getQuantity(TEST_PRODUCT_ID)));

    // ensure the cart total is correct
    System.out.println(String.format(CART_TOTAL_EXPECTED_MSG, cart.getTotalValue(), 3000));

    // ensure the inventory total is correct
    System.out.println(
        String.format(
            INVENTORY_EXPECTED_MSG,
            catalog.getProduct(TEST_PRODUCT_ID),
            0,
            inventory.getQuantity(TEST_PRODUCT_ID)));

    // purchase the cart
    client.purchaseCart();

    // verify empty state
    System.out.println("Cart is empty: " + cart.isEmpty());
    System.out.println("Bought items is empty: " + boughtItems.isEmpty());

    // ensure the cart is now empty
    System.out.println(
        String.format(
            CART_EXPECTED_MSG,
            catalog.getProduct(TEST_PRODUCT_ID),
            0,
            cart.getQuantity(TEST_PRODUCT_ID)));

    // ensure the bought items is correct
    System.out.println(
        String.format(
            BOUGHT_ITEMS_EXPECTED_MSG,
            catalog.getProduct(TEST_PRODUCT_ID),
            2,
            boughtItems.getQuantity(TEST_PRODUCT_ID)));

    // return the bought items
    Map<String, Integer> itemsToReturn = GeneralFactory.createMap(boughtItems.getQuantities());
    client.returnPurchaseToCart(itemsToReturn);

    // ensure the bought items is correct
    System.out.println(
        String.format(
            BOUGHT_ITEMS_EXPECTED_MSG,
            catalog.getProduct(TEST_PRODUCT_ID),
            0,
            boughtItems.getQuantity(TEST_PRODUCT_ID)));

    // ensure the cart has items again
    System.out.println(
        String.format(
            CART_EXPECTED_MSG,
            catalog.getProduct(TEST_PRODUCT_ID),
            2,
            cart.getQuantity(TEST_PRODUCT_ID)));

    // remove one of product id 5
    client.removeFromCart(TEST_PRODUCT_ID, 1);

    // ensure the cart now only has 1 of product id 5
    System.out.println(
        String.format(
            CART_EXPECTED_MSG,
            catalog.getProduct(TEST_PRODUCT_ID),
            1,
            cart.getQuantity(TEST_PRODUCT_ID)));

    // verify inventory state
    System.out.println(
        String.format(
            INVENTORY_EXPECTED_MSG,
            catalog.getProduct(TEST_PRODUCT_ID),
            1,
            inventory.getQuantity(TEST_PRODUCT_ID)));
  }