Class Orders C# Exercise - C# Programming Course

 Exercise

Class Orders

 Objetive

Create a project and the corresponding classes (using several files) for this classes diagram.

Each class must include the attributes and methods shown in the diagram. Consider that all cardinalities are 1:1.

 Example Code

// Import the System namespace for basic functionality
using System;

// Define the main class containing all required classes for Orders
public class OrdersDemo
{
    // Define the Customer class with a name attribute and related methods
    public class Customer
    {
        // Declare a string field to store the customer's name
        private string name;

        // Constructor to initialize the customer’s name
        public Customer(string customerName)
        {
            // Set the initial value of the name
            name = customerName;
        }

        // Public method to get the customer's name
        public string GetName()
        {
            // Return the customer's name
            return name;
        }

        // Public method to set the customer's name
        public void SetName(string newName)
        {
            // Update the name with a new value
            name = newName;
        }
    }

    // Define the Product class with a description attribute and related methods
    public class Product
    {
        // Declare a string field to store the product's description
        private string description;

        // Constructor to initialize the product’s description
        public Product(string productDescription)
        {
            // Set the initial value of the description
            description = productDescription;
        }

        // Public method to get the product description
        public string GetDescription()
        {
            // Return the product's description
            return description;
        }

        // Public method to set the product description
        public void SetDescription(string newDescription)
        {
            // Update the description with a new value
            description = newDescription;
        }
    }

    // Define the OrderDetails class with a quantity attribute and related methods
    public class OrderDetails
    {
        // Declare an integer field to store the quantity of the order
        private int quantity;

        // Constructor to initialize the order quantity
        public OrderDetails(int orderQuantity)
        {
            // Set the initial value of the quantity
            quantity = orderQuantity;
        }

        // Public method to get the quantity of the order
        public int GetQuantity()
        {
            // Return the order quantity
            return quantity;
        }

        // Public method to set the quantity of the order
        public void SetQuantity(int newQuantity)
        {
            // Update the quantity with a new value
            quantity = newQuantity;
        }
    }

    // Define the Order class which includes Customer, Product, and OrderDetails
    public class Order
    {
        // Field to hold the customer associated with this order
        private Customer customer;
        // Field to hold the product associated with this order
        private Product product;
        // Field to hold the order details associated with this order
        private OrderDetails orderDetails;

        // Constructor to initialize the order with customer, product, and order details
        public Order(Customer orderCustomer, Product orderProduct, OrderDetails orderDetailsInfo)
        {
            // Set the customer for this order
            customer = orderCustomer;
            // Set the product for this order
            product = orderProduct;
            // Set the order details for this order
            orderDetails = orderDetailsInfo;
        }

        // Public method to get the customer of this order
        public Customer GetCustomer()
        {
            // Return the customer associated with this order
            return customer;
        }

        // Public method to get the product of this order
        public Product GetProduct()
        {
            // Return the product associated with this order
            return product;
        }

        // Public method to get the order details of this order
        public OrderDetails GetOrderDetails()
        {
            // Return the order details associated with this order
            return orderDetails;
        }

        // Method to display the details of the order on the console
        public void DisplayOrder()
        {
            // Print the customer's name
            Console.WriteLine("Customer Name: " + customer.GetName());
            // Print the product's description
            Console.WriteLine("Product Description: " + product.GetDescription());
            // Print the quantity ordered
            Console.WriteLine("Quantity Ordered: " + orderDetails.GetQuantity());
        }
    }

    // Define the Main entry point of the program for testing
    public static void Main()
    {
        // Create a Customer object with a specified name
        Customer customer = new Customer("John Doe");
        // Create a Product object with a specified description
        Product product = new Product("Laptop Computer");
        // Create an OrderDetails object with a specified quantity
        OrderDetails orderDetails = new OrderDetails(2);

        // Create an Order object with the created customer, product, and order details
        Order order = new Order(customer, product, orderDetails);

        // Display the order details by calling DisplayOrder on the Order object
        order.DisplayOrder();
    }
}

Juan A. Ripoll - Programming Tutorials and Courses © 2025 All rights reserved.  Legal Conditions.