Home > Articles > Software Development & Management > Object Technology

  • Print
  • + Share This
This chapter is from the book

Example Code Used in This Chapter

The following code is presented in C# .NET and VB .NET. These examples correspond to the Java code that is listed inside the chapter itself.

The TestShape Example: C# .NET

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestShop
{
    class TestShop
    {
        public static void Main()
        {
            Shop shop = null;

            Console.WriteLine("Instantiate the PizzaShop class:" + "\n");

            try
            {
                // new pizzaShop();
                shop = new PizzaShop();
            }
            catch (Exception e)
            {
            }

            string[] inventory = shop.getInventory();

            // list the inventory

            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Argument" + i + " = " + inventory[i]);
            }

            // buy an item
            shop.buyInventory(inventory[1]);

        }
    }

    public abstract class Shop
    {
        CustList customerList;

        public void CalculateSaleTax()
        {
            Console.WriteLine("Calculate Sales Tax");
        }

        public abstract string[] getInventory();
        public abstract void buyInventory(string item);

    }

    public interface Nameable
    {
        String Name
        {
            get;
            set;
        }
    }

    public class PizzaShop : Shop, Nameable
    {
        string companyName;

        string[] foodOfferings = {
            "Pizza",
            "Spaghetti",
            "Garden Salad",
            "Anitpasto",
            "Calzone"
        };

        public override string[] getInventory()
        {
            return foodOfferings;
        }

        public override void buyInventory(string item)
        {
            Console.WriteLine("\nYou have just purchased " + item);
        }

        public String Name
        {
            get { return companyName; }
            set { companyName = value; }
        }
    }

    public class DonutShop : Shop, Nameable
    {
        string companyName;

        string[] menuItems = {
            "Donuts",
            "Muffins",
            "Danish",
            "Coffee",
            "Tea"
        };

        public override string[] getInventory()
        {
            return menuItems;
        }

        public override void buyInventory(string item)
        {
            Console.WriteLine("\nYou have just purchased " + item);
        }

        public String Name
        {
            get { return companyName; }
            set { companyName = value; }
        }
    }

    public class CustList
    {
        string name;

        public string findCust() { return name; }
        public void addCust(string Name) { }
    }
}

The TestShape Example: VB .NET

Module Module1

    Sub Main()

        Dim myShop As New DonutShop()
        Dim inventory() As String
        Dim ival As Integer

        System.Console.WriteLine("Instantiate the DonutShop class")

        inventory = myShop.getInventory()

        For ival = 0 To 4

            System.Console.Write("Argument ")
            System.Console.Write(ival)
            System.Console.Write(" = ")
            System.Console.WriteLine(inventory(ival))

        Next

        myShop.buyInventory(inventory(1))

        System.Console.ReadLine()

    End Sub

End Module

Public MustInherit Class Shop

    Dim myCustList As New CustList()

    Public Function CalculateSaleTax()

        System.Console.WriteLine("Calculate Sales Tax")
        Return Nothing

    End Function

    Public MustOverride Function getInventory() As String()
    Public MustOverride Function buyInventory(ByVal i As String)

End Class

Interface Nameable

    Property Name() As String

End Interface

Public Class DonutShop

    Inherits Shop
    Implements Nameable

    Dim companyName As String

    Dim menuItems() As String = {"Donuts", "Muffins", "Danish", "Coffee", "Tea"}

    Public Overrides Function getInventory() As String()

        Return menuItems

    End Function

    Public Overrides Function buyInventory(ByVal item As String)

        System.Console.WriteLine("You have just purchased " + item)

        Return Nothing

    End Function

    Private strName As String
    Public Property Name() As String Implements Nameable.Name

        Get
            Return strName
        End Get
        Set(ByVal value As String)
            strName = value
        End Set
    End Property

End Class

Public Class CustList

    Dim name As String

    Public Function findCust() As String

        Return name

    End Function

    Public Function addCust(ByVal c As String)

        Return Nothing

    End Function

End Class
  • + Share This
  • 🔖 Save To Your Account