...
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
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 | #fff
If the serialization is successful, you are now ready to implement bootstrap_orders_accounts_items(), this is our implementation
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
/*
* 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 | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
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 | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
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());
}
}
}
|
Related articles
Filter by label (Content by label) | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
...