summaryrefslogtreecommitdiff
path: root/examples/csharp/excs_getting_started/Inventory.cs
blob: 26b837183bfa9bdebe4ae2c385926895a7276e4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2009, 2015 Oracle and/or its affiliates.  All rights reserved.
 *
 */
using System;
using System.Collections.Generic;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using BerkeleyDB;

namespace excs_getting_started {
    [Serializable]
    public class Inventory {
        private string category;
        private string itemname;
        private float price;
        private int quantity;
        private string sku;
        private string vendor;

        /* Declare a Category property of type string. */
        public string Category {
            get { return category; }
            set { category = value; }
        }

        /* Declare an Itemname property of type string. */
        public string Itemname {
            get { return itemname; }
            set { itemname = value; }
        }

        /* Declare a Price property of type string. */
        public float Price {
            get { return price; }
            set { price = value; }
        }

        /* Declare a Quantity property of type string. */
        public int Quantity {
           get { return quantity; }
           set { quantity = value; }
        }

        /* Declare a Sku property of type string. */
        public string Sku {
            get { return sku; }
            set { sku = value; }
        }

        /* Declare a Vendor property of type string. */
        public string Vendor {
            get { return vendor; }
            set { vendor = value; }
        }

        /* Default constructor. */
        public Inventory() {
            itemname = System.String.Empty;
            category = System.String.Empty;
            price = 0.0F;
            quantity = 0;
            sku = System.String.Empty;
            vendor = System.String.Empty;
        }

        /* Constructor for use with data returned from a BDB get. */
        public Inventory (byte[] buffer) {
            /* Fill in the fields from the buffer. */
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream memStream = new MemoryStream(buffer);
            Inventory tmp = (Inventory) formatter.Deserialize(memStream);

            this.itemname = tmp.itemname;
            this.sku = tmp.sku;
            this.price = tmp.price;
            this.quantity = tmp.quantity;
            this.category = tmp.category;
            this.vendor = tmp.vendor;
            memStream.Close();
        }

        /* 
         * Marshall class data members into a single contiguous memory 
         * location for the purpose of storing the data in a database.
         */
        public byte[] getBytes () { 
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream memStream = new MemoryStream();
            formatter.Serialize(memStream, this);
            byte [] bytes = memStream.GetBuffer();
            memStream.Close();
            return bytes;
        }
    }
}