M.Omar M.Omar - 2 months ago 9
C# Question

How to Initialize those entities in C# using OOP?

what is the perfect initialization design pattern for those classes?

First class called client that has name, ID and list of orders "that he bought".

Second class called deliveryReceptionist, his class has his name, his ID and list of orders he bought and the list of persons he serve.

Third class is the items he buy, I have a list of 10 items and each item should have a name and ID

forth class is the order which consists of the item_ID, price of each item, number of items "as each order has one type of items".

it's weird and not applied to real life, but it is not me that put it.

So i've tried that:

public class Information
{
public string Name { get; set;}
public int ID { get; set;}
}

public class Client : Information
{
List<theOrder> orders = new List<theOrder>();
}
public class DelRec : Information
{
List<Client> listOfClients = new List<Client>();
List<theOrder> orders = new List<theOrder>();
}
public static class items
{
public static List<Information> item = new List<Information>();
}
public class theOrder
{
private int itemID{ get; set;}
private double price{ get; set;}
private double number{ get; set;}
}


Is that good? or I should optimize it in a better way?

Answer

There are a few things that i would about this code.

All Properties and classes should be PascalCase in C#. theOrder should be Order as it is type not instance, You can call an instance theOrder if you like. There is no way to instantiate the properties of Order class if you make them private and have empty constructor. Also how will you use the getters?

You will need constructors to initialize your properties. And also it is good OOP to create interfaces and abstract classes. To begin with, my code would be something like:

public interface IPerson
{
    string Name { get; }
    int Id { get; }
}

public interface ISalesSubject : IPerson
{
    IDictionary<int, IOrder> Orders { get; }
}

public abstract class SalesSubject: ISalesSubject
{
    protected SalesSubject(string name, int id)
    {
        this.Name = name;
        this.Id = id;
        this.Orders = new Dictionary<int, IOrder>();
    }

    protected SalesSubject(string name, int id, IDictionary<int, IOrder> orders)
    {
        this.Name = name;
        this.Id = id;
        this.Orders = orders;
    }

    public string Name { get; protected set; }

    public int Id { get; protected set; }

    public IDictionary<int, IOrder> Orders { get; protected set; }

    public virtual void AddOrder(int itemId, IOrder order)
    {
        if (!this.Orders.ContainsKey(itemId))
        {
            this.Orders.Add(itemId, order);
        }
    }

    public virtual void RemoveOrder(int itemId, IOrder order)
    {
        this.Orders.Remove(itemId, order);
    }
}

public class Client : SalesSubject
{

    public Client(string name, int id)
        : base(name, id)
    {
        this.Orders = new Dictionary<int, IOrder>();
    }

    public Client(string name, int id, IDictionary<int, IOrder> orders)
        : base(name, id, orders)
    {
        this.Orders = orders;
    }
}

public class DeliveryReceptionist : SalesSubject
{
    public DeliveryReceptionist(string name, int id)
        : base(name, id)
    {
        this.Clients = new Dictionary<int, Client>();
    }

    public DeliveryReceptionist(string name, int id, IDictionary<int, IOrder> orders)
        : base(name, id, orders)
    {
        this.Clients = new Dictionary<int, Client>();
    }

    public DeliveryReceptionist(string name, int id, IDictionary<int, IOrder> orders, IDictionary<int, Client> clients)
       : base(name, id, orders)
    {
        this.Clients = clients;
    }

    public DeliveryReceptionist(string name, int id, IDictionary<int, Client> clients)
       : base(name, id)
    {
        this.Clients = clients;
    }

    public IDictionary<int, Client> Clients { get; set; }

    public void AddClient(int clientId, Client client)
    {
        if (!this.Clients.ContainsKey(clientId))
        {
            this.Clients.Add(clientId, client);
        }
    }

    public void RemoveClient(int clientId, Client client)
    {
        this.Clients.Remove(clientId, client);
    }
}

public interface IOrder
{
    int ItemID { get; set; }
    double Price { get; set; }
    double Number { get; set; }
}
public class Order : IOrder
{
    public Order(int itemId, double price, double number)
    {
        this.ItemId = itemId;
        this.Price = price;
        this.Number = number;
    }

    public int ItemId { get; set; }
    public double Price { get; set; }
    public double Number { get; set; }
}

Why do you need items class.Static classes are anti-pattern when talking about encapsualtion and good OOP.They can be accessed from anywhere and its content changed if it is with public access modificator.

Comments