Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

#fff
Code Block
languagejava
titletest_serialization_reader_all_customers
linenumberstrue
collapsetrue
    public void test_serialization_reader_all_customers()
    {
        String fname = "c:\\tmp\\all-customers.oos";
        // Read the object from the filesystem
        InputChannel sfr = Reader.getSerializableFileReader(fname);
        sfr.openForReading(fname);

        // Read the entire object network back in
        HashMap< String, Customer> customersIn = (HashMap< String, Customer>) sfr.read();

        // Only process the objects if the read was successful
        if (customersIn != null)
        {
            // Iterate over all the Customers...
            for (Map.Entry<String, Customer> entry : customersIn.entrySet())
            {
                Customer cc = entry.getValue();
                System.out.printf("Customer ==> %s, %s, %s, %s, %s\n",
                        entry.getKey(),
                        cc.getFname(),
                        cc.getLname(),
                        cc.getDob().toString(),
                        cc.getAddress());
                System.out.printf("\tAccount ==> %d, %s\n",
                        cc.getAccount().getAccountId(),
                        cc.getAccount().getDateCreated().toString());

            }
        }
    }
Panel
bgColor


If the serialization is successful, you are now ready to implement bootstrap_orders_accounts_items(), this is our implementation

Code Block
languagejava
titlebootstrap_orders_accounts_items
linenumberstrue
collapsetrue
    /*
     * This method will use an Order object to link the Item and Product objects 
     * to the Customer and Account objects
     */
    public void bootstrap_orders_accounts_items()
    {
        // Get an account and a product
        Account acc = CustomerAccountMgr.getInstance().getAccount(1);
        Product pp = Warehouse.getInstance().getProduct("JDF-001");

        // only proceed if the keys were valid
        if (acc != null && pp != null)
        {
            try
            {
                // create new order giving it the order number of 1
                Order oo = new Order(acc, 1, LocalDate.now());

                // add the order the account
                acc.addOrder(oo);

                // get an item from the product, remember Items represent real things
                // Products are descriptions of thise things
                Item itm = pp.getItem(1);

                // Set the quantity attribute, how many instances of the Item
                itm.setQuantity(3);

                // Only proceed if you used a valid item id
                if (itm != null)
                {
                    oo.addItem(itm);  // this call decrease the stock for this product
                }
            } catch (Exception ex)
            {
                Logger.getLogger(MainUnit.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }


Code Block
languagejava
titletest_serialization_writer_all_objects
linenumberstrue
collapsetrue
    public void test_serialization_writer_all_objects()
    {
        String fname = "c:\\tmp\\all-objects.oos";

        // working with a Serializable File Writer
        SerializableFileWriter sfw = Writer.getSerializableFileWriter(fname);

        // Write the object out
        // HashMap are Serializable, so it can be written straight out
        // Because all objects in the HashMap are Serializable, they will all be
        // written out as well
        sfw.write(CustomerAccountMgr.getInstance().getCustomers());

        sfw.close();
    }


Code Block
languagejava
titletest_serialization_reader_all_objects
linenumberstrue
collapsetrue
    public void test_serialization_reader_all_objects()
    {
        String fname = "c:\\tmp\\all-objects.oos";
        // Read the object from the filesystem
        InputChannel sfr = Reader.getSerializableFileReader(fname);
        sfr.openForReading(fname);

        // Read the entire object network back in
        HashMap< String, Customer> customersIn = (HashMap< String, Customer>) sfr.read();

        // Only process the objects if the read was successful
        if (customersIn != null)
        {
            // Iterate over all the Customers...
            for (Map.Entry<String, Customer> entry : customersIn.entrySet())
            {
                Customer cc = entry.getValue();
                System.out.printf("Customer ==> %s, %s, %s, %s, %s\n",
                        entry.getKey(),
                        cc.getFname(),
                        cc.getLname(),
                        cc.getDob().toString(),
                        cc.getAddress());
                System.out.printf("\tAccount ==> %d, %s\n",
                        cc.getAccount().getAccountId(),
                        cc.getAccount().getDateCreated().toString());

                displayAccountOrderDetais(cc.getAccount());
            }
        }
    }

    public void displayAccountOrderDetais(Account acc)
    {
        HashMap< Integer, Order> orders = acc.getOrders();
        for (Map.Entry<Integer, Order> entry : orders.entrySet())
        {
            Order ord = entry.getValue();
            System.out.printf("Order ==> %d, %f, %s\n",
                    ord.getOrderNo(),
                    ord.getTotalCost(),
                    ord.getDateCreated().toString());

            displayOrderDetails(ord);
        }
    }

    public void displayOrderDetails(Order ord)
    {
        for (Item itm : ord.getItems())
        {
            System.out.printf("Item ==> %d, %f, %d\n",
                    itm.getUniqueId(),
                    itm.getItemCost(),
                    itm.getQuantity());
            System.out.printf("Product ==> %s has %d in stock\n",
                    itm.getProduct().getDescription(),
                    itm.getProduct().getQuantity());
        }
    }
}


Filter by label (Content by label)
showLabelsfalse
max5
spacescom.atlassian.confluence.content.render.xhtml.model.resource.identifiers.SpaceResourceIdentifier@2007c5
showSpacefalse
sortmodified
reversetrue
typepage
cqllabel = "kb-troubleshooting-article" and type = "page" and space = "DJFE"
labelskb-troubleshooting-article

...