Class Orders - C# Programming Exercise

In this exercise, you must create a project and the corresponding classes according to the class diagram. Each class must include the attributes and methods shown in the diagram. It is important to note that all the cardinalities between the classes are 1:1, which means that each instance of one class is related to exactly one instance of another class. The project should be divided into several files for each class, following the organization conventions for C# projects. The attributes and methods should be correctly implemented, respecting the relationships and functionalities described by the diagram.

 Category

OOP Object Oriented Programming

 Exercise

Class Orders

 Objective

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.

 Write Your C# Exercise

// 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();
    }
}

 Share this C# exercise

 More C# Programming Exercises of OOP Object Oriented Programming

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Class Colored Circle

    In this expanded exercise, you must modify the shapes and square project to also include a new class that stores data about colored circles. You need to...

  •  Classes Student + Teacher

    In this exercise in C#, you will need to write a program that includes the Person class you just created. From this class, you will create...

  •  Class Photo Album

    Write a C# class called "PhotoAlbum" with a private attribute "numberOfPages". The class should also have a public method named "GetNumberOfPages", whic...

  •  Class Shapes

    This exercise involves creating a project in C# that implements several classes based on a class diagram. The goal is to organize the code by splitting the ...

  •  Class Vehicles

    This exercise involves creating a project in C# and defining the corresponding classes according to a class diagram. Each class must include the attr...

  •  Class Square

    This exercise consists of completing the project called "Shapes" by adding a class named Square. In this class, we will store the starting X and Y coordinat...