1 /*- 2 * Copyright (c) 2009, 2020 Oracle and/or its affiliates. All rights reserved. 3 * 4 * See the file EXAMPLES-LICENSE for license information. 5 * 6 */ 7 using System; 8 using System.Collections.Generic; 9 using System.Collections; 10 using System.Diagnostics; 11 using System.IO; 12 using System.Text; 13 using System.Runtime.Serialization.Formatters.Binary; 14 using BerkeleyDB; 15 16 namespace excs_getting_started { 17 [Serializable] 18 public class Inventory { 19 private string category; 20 private string itemname; 21 private float price; 22 private int quantity; 23 private string sku; 24 private string vendor; 25 26 /* Declare a Category property of type string. */ 27 public string Category { 28 get { return category; } 29 set { category = value; } 30 } 31 32 /* Declare an Itemname property of type string. */ 33 public string Itemname { 34 get { return itemname; } 35 set { itemname = value; } 36 } 37 38 /* Declare a Price property of type string. */ 39 public float Price { 40 get { return price; } 41 set { price = value; } 42 } 43 44 /* Declare a Quantity property of type string. */ 45 public int Quantity { 46 get { return quantity; } 47 set { quantity = value; } 48 } 49 50 /* Declare a Sku property of type string. */ 51 public string Sku { 52 get { return sku; } 53 set { sku = value; } 54 } 55 56 /* Declare a Vendor property of type string. */ 57 public string Vendor { 58 get { return vendor; } 59 set { vendor = value; } 60 } 61 62 /* Default constructor. */ Inventory()63 public Inventory() { 64 itemname = System.String.Empty; 65 category = System.String.Empty; 66 price = 0.0F; 67 quantity = 0; 68 sku = System.String.Empty; 69 vendor = System.String.Empty; 70 } 71 72 /* Constructor for use with data returned from a BDB get. */ Inventory(byte[] buffer)73 public Inventory (byte[] buffer) { 74 /* Fill in the fields from the buffer. */ 75 BinaryFormatter formatter = new BinaryFormatter(); 76 MemoryStream memStream = new MemoryStream(buffer); 77 Inventory tmp = (Inventory) formatter.Deserialize(memStream); 78 79 this.itemname = tmp.itemname; 80 this.sku = tmp.sku; 81 this.price = tmp.price; 82 this.quantity = tmp.quantity; 83 this.category = tmp.category; 84 this.vendor = tmp.vendor; 85 memStream.Close(); 86 } 87 88 /* 89 * Marshall class data members into a single contiguous memory 90 * location for the purpose of storing the data in a database. 91 */ getBytes()92 public byte[] getBytes () { 93 BinaryFormatter formatter = new BinaryFormatter(); 94 MemoryStream memStream = new MemoryStream(); 95 formatter.Serialize(memStream, this); 96 byte [] bytes = memStream.GetBuffer(); 97 memStream.Close(); 98 return bytes; 99 } 100 } 101 } 102